Class: Slimi::Filters::Splat::Builder

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

Constant Summary collapse

INVALID_ATTRIBUTE_NAME_REGEXP =
%r{[ \0"'>/=]}

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Builder

Returns a new instance of Builder.

Parameters:

  • options (Hash)


83
84
85
86
87
88
89
90
# File 'lib/slimi/filters/splat.rb', line 83

def initialize(options)
  @attr_quote = options[:attr_quote]
  @format = options[:format]
  @merge_attrs = options[:merge_attrs] || {}
  @sort_attrs = options.fetch(:sort_attrs, true)
  @use_html_safe = options[:use_html_safe]
  @attributes = {}
end

Instance Method Details

#attribute(name, value) ⇒ Object

Add an attribute value already rendered and escaped by the compiled template.

Parameters:

  • name (String)
  • value (String)


95
96
97
# File 'lib/slimi/filters/splat.rb', line 95

def attribute(name, value)
  store(name, value)
end

#code_attribute(name, escape, value) ⇒ Object

Add an attribute value, applying merge / boolean / nil / escape rules.

Parameters:

  • name (String)
  • escape (Boolean)
  • value (Object)


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/slimi/filters/splat.rb', line 103

def code_attribute(name, escape, value)
  if (delimiter = @merge_attrs[name])
    value = value.is_a?(::Array) ? value.flatten.map(&:to_s).reject(&:empty?).join(delimiter) : value.to_s
    store(name, escape_html(escape, value)) unless value.empty?
  elsif value.is_a?(::Hash)
    value.each do |key, nested_value|
      code_attribute("#{name}-#{key}", escape, nested_value)
    end
  elsif value != false && !value.nil?
    store(name, value == true ? true : escape_html(escape, value.to_s))
  end
end

#splat(hash) ⇒ Object

Parameters:

  • hash (Hash)


117
118
119
120
121
# File 'lib/slimi/filters/splat.rb', line 117

def splat(hash)
  hash.each do |name, value|
    code_attribute(name.to_s, true, value)
  end
end

#to_sString

Returns:

  • (String)


124
125
126
127
# File 'lib/slimi/filters/splat.rb', line 124

def to_s
  attributes = @sort_attrs ? @attributes.sort_by(&:first) : @attributes
  attributes.map { |name, value| render(name, value) }.join
end