Class: Slim::Sections Private

Inherits:
Filter
  • Object
show all
Defined in:
lib/slim/sections.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Handle logic-less mode This filter can be activated with the option “sections”

Instance Method Summary collapse

Methods inherited from Filter

#on_slim_attrs, #on_slim_tag, #tmp_var

Constructor Details

#initialize(opts = {}) ⇒ Sections

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Sections.



10
11
12
13
14
15
# File 'lib/slim/sections.rb', line 10

def initialize(opts = {})
  super
  unless [:string, :symbol, :wrapped].include?(options[:dictionary_access])
    raise "Invalid dictionary access #{options[:dictionary_access].inspect}"
  end
end

Instance Method Details

#access(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



70
71
72
73
74
75
76
77
# File 'lib/slim/sections.rb', line 70

def access(name)
  case options[:dictionary_access]
  when :string
    "_slimdict[#{name.to_s.inspect}]"
  else
    "_slimdict[#{name.to_sym.inspect}]"
  end
end

#compile(exp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
18
19
20
21
22
23
24
25
26
# File 'lib/slim/sections.rb', line 17

def compile(exp)
  if options[:sections]
    # Store the dictionary in the _slimdict variable
    [:multi,
     [:block, "_slimdict = #{dictionary}"],
     super]
  else
    exp
  end
end

#dictionaryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



79
80
81
82
83
84
85
# File 'lib/slim/sections.rb', line 79

def dictionary
  if options[:dictionary_access] == :wrapped
    "Slim::Wrapper.new(#{options[:dictionary]})"
  else
    options[:dictionary]
  end
end

#on_slim_control(name, content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Interpret control blocks as sections or inverted sections



29
30
31
32
33
34
35
# File 'lib/slim/sections.rb', line 29

def on_slim_control(name, content)
  if name =~ /^!\s*(.*)/
    on_slim_inverted_section($1, content)
  else
    on_slim_section(name, content)
  end
end

#on_slim_inverted_section(name, content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



37
38
39
40
41
42
43
44
# File 'lib/slim/sections.rb', line 37

def on_slim_inverted_section(name, content)
  tmp = tmp_var('section')
  [:multi,
   [:block, "#{tmp} = #{access name}"],
   [:block, "if !#{tmp} || #{tmp}.respond_to?(:empty) && #{tmp}.empty?"],
              compile!(content),
   [:block, 'end']]
end

#on_slim_output(escape, name, content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



65
66
67
68
# File 'lib/slim/sections.rb', line 65

def on_slim_output(escape, name, content)
  raise 'Output statements with content are forbidden in sections mode' if !empty_exp?(content)
  [:slim, :output, escape, access(name), content]
end

#on_slim_section(name, content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/slim/sections.rb', line 46

def on_slim_section(name, content)
  content = compile!(content)
  tmp1, tmp2 = tmp_var('dict'), tmp_var('dict')

  [:multi,
   [:block, "if #{tmp1} = #{access name}"],
   [:block,   "if #{tmp1} == true"],
                 content,
   [:block,   'else'],
   [:block,     "#{tmp1} = [#{tmp1}] if #{tmp1}.respond_to?(:has_key?) || !#{tmp1}.respond_to?(:map)"],
   [:block,     "#{tmp2} = _slimdict"],
   [:block,     "#{tmp1}.each do |_slimdict|"],
                  content,
   [:block,     'end'],
   [:block,     "_slimdict = #{tmp2}"],
   [:block,   'end'],
   [:block, 'end']]
end