Class: Slimi::Filters::Splat::Builder
- Inherits:
-
Object
- Object
- Slimi::Filters::Splat::Builder
- Defined in:
- lib/slimi/filters/splat.rb
Constant Summary collapse
- INVALID_ATTRIBUTE_NAME_REGEXP =
%r{[ \0"'>/=]}
Instance Method Summary collapse
-
#attribute(name, value) ⇒ Object
Add an attribute value already rendered and escaped by the compiled template.
-
#code_attribute(name, escape, value) ⇒ Object
Add an attribute value, applying merge / boolean / nil / escape rules.
-
#initialize(options) ⇒ Builder
constructor
A new instance of Builder.
- #splat(hash) ⇒ Object
- #to_s ⇒ String
Constructor Details
#initialize(options) ⇒ Builder
Returns a new instance of Builder.
83 84 85 86 87 88 89 90 |
# File 'lib/slimi/filters/splat.rb', line 83 def initialize() @attr_quote = [:attr_quote] @format = [:format] @merge_attrs = [:merge_attrs] || {} @sort_attrs = .fetch(:sort_attrs, true) @use_html_safe = [:use_html_safe] @attributes = {} end |
Instance Method Details
#attribute(name, value) ⇒ Object
Add an attribute value already rendered and escaped by the compiled template.
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.
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
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_s ⇒ 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 |