Class: Slimi::Filters::Attribute

Inherits:
Base
  • Object
show all
Defined in:
lib/slimi/filters/attribute.rb

Overview

Handle ‘[:slimi, :attributes, …]`.

Instance Method Summary collapse

Methods inherited from Base

#on_slimi_control, #on_slimi_embedded, #on_slimi_output, #on_slimi_position, #on_slimi_text

Instance Method Details

#on_html_attr(name, value) ⇒ Array

Parameters:

  • name (String)
  • value (Array)

Returns:

  • (Array)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/slimi/filters/attribute.rb', line 18

def on_html_attr(name, value)
  if value[0] == :slimi && value[1] == :attrvalue && !options[:merge_attrs][name]
    escape = value[2]
    code = value[3]
    case code
    when 'true'
      [:html, :attr, name, [:multi]]
    when 'false', 'nil'
      [:multi]
    else
      tmp = unique_name
      [:multi,
       [:code, "#{tmp} = #{code}"],
       [:if, tmp,
        [:if, "#{tmp} == true",
         [:html, :attr, name, [:multi]],
         [:html, :attr, name, [:escape, escape, [:dynamic, tmp]]]]]]
    end
  else
    @attr = name
    super
  end
end

#on_html_attrs(*expressions) ⇒ Array

Parameters:

  • expressions (Array<Array>)

Returns:

  • (Array)


11
12
13
# File 'lib/slimi/filters/attribute.rb', line 11

def on_html_attrs(*expressions)
  [:multi, *expressions.map { |expression| compile(expression) }]
end

#on_slimi_attrvalue(escape, code) ⇒ Array

Returns .

Parameters:

  • escape (Boolean)
  • code\ (String)

Returns:

  • (Array)

    \



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/slimi/filters/attribute.rb', line 45

def on_slimi_attrvalue(escape, code)
  if (delimiter = options[:merge_attrs][@attr])
    tmp = unique_name
    [:multi,
     [:code, "#{tmp} = #{code}"],
     [:if, "Array === #{tmp}",
      [:multi,
       [:code, "#{tmp} = #{tmp}.flatten"],
       [:code, "#{tmp}.map!(&:to_s)"],
       [:code, "#{tmp}.reject!(&:empty?)"],
       [:escape, escape, [:dynamic, "#{tmp}.join(#{delimiter.inspect})"]]],
      [:escape, escape, [:dynamic, tmp]]]]
  else
    [:escape, escape, [:dynamic, code]]
  end
end