Class: ElementFactory::HtmlAttributes
- Inherits:
-
Object
- Object
- ElementFactory::HtmlAttributes
- Defined in:
- lib/element_factory/html_attributes.rb
Constant Summary collapse
- BOOLEAN_ATTRIBUTES =
%w(disabled readonly multiple checked autobuffer autoplay controls loop selected hidden scoped async defer reversed ismap seemless muted required autofocus novalidate formnovalidate open pubdate)
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
Instance Method Summary collapse
- #boolean_attributes ⇒ Object
- #data_attributes ⇒ Object
-
#initialize(attributes = {}) ⇒ HtmlAttributes
constructor
A new instance of HtmlAttributes.
- #to_s ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ HtmlAttributes
Returns a new instance of HtmlAttributes.
10 11 12 |
# File 'lib/element_factory/html_attributes.rb', line 10 def initialize(attributes={}) @attributes = attributes end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
8 9 10 |
# File 'lib/element_factory/html_attributes.rb', line 8 def attributes @attributes end |
Instance Method Details
#boolean_attributes ⇒ Object
20 21 22 23 24 25 |
# File 'lib/element_factory/html_attributes.rb', line 20 def boolean_attributes attributes.each_with_object({}) do |(key, value), new_attributes| next unless BOOLEAN_ATTRIBUTES.include?(key.to_s) new_attributes[key] = key.to_s end end |
#data_attributes ⇒ Object
14 15 16 17 18 |
# File 'lib/element_factory/html_attributes.rb', line 14 def data_attributes (attributes[:data] || {}).each_with_object({}) do |(key,value), new_attributes| new_attributes["data-#{key.to_s.dasherize}"] = value end end |
#to_s ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/element_factory/html_attributes.rb', line 27 def to_s attributes = self.attributes .merge(data_attributes) .merge(boolean_attributes) .stringify_keys attributes.delete("data") attributes.keys.sort.map do |attribute| value = ERB::Util.html_escape(attributes[attribute]) %(#{attribute}="#{value}") end.join(" ").html_safe end |