Class: Arbre::Html::Attributes
- Inherits:
-
Object
- Object
- Arbre::Html::Attributes
- Includes:
- Enumerable
- Defined in:
- lib/arbre/html/attributes.rb
Overview
HTML attributes hash. Behaves like a hash with some minor differences:
-
Indifferent access: everything is stored as strings, but also values.
-
Setting an attribute to
true
sets it to the name of the attribute, as per the HTML standard. -
Setting an attribute to
false
ornil
will remove it.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](attribute) ⇒ Object
- #[]=(attribute, value) ⇒ Object
- #classes ⇒ Object
- #classes=(value) ⇒ Object
- #eql?(other) ⇒ Boolean
- #has_key?(key) ⇒ Boolean
-
#initialize(attributes = {}) ⇒ Attributes
constructor
A new instance of Attributes.
- #pairs ⇒ Object
- #remove(attribute) ⇒ Object
- #style ⇒ Object
- #style=(value) ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
- #update(attributes) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Attributes
Returns a new instance of Attributes.
12 13 14 15 |
# File 'lib/arbre/html/attributes.rb', line 12 def initialize(attributes = {}) @attributes = {} update attributes end |
Class Method Details
.[](*args) ⇒ Object
17 18 19 |
# File 'lib/arbre/html/attributes.rb', line 17 def self.[](*args) Attributes.new(Hash[*args]) end |
Instance Method Details
#==(other) ⇒ Object
76 77 78 |
# File 'lib/arbre/html/attributes.rb', line 76 def ==(other) to_hash == other.to_hash end |
#[](attribute) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/arbre/html/attributes.rb', line 21 def [](attribute) if attribute.to_s == 'class' classes elsif attribute.to_s == 'style' style else @attributes[attribute.to_s] end end |
#[]=(attribute, value) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/arbre/html/attributes.rb', line 30 def []=(attribute, value) if attribute.to_s == 'class' self.classes = value elsif attribute.to_s == 'style' self.style = value elsif value == true @attributes[attribute.to_s] = attribute.to_s elsif value @attributes[attribute.to_s] = value.to_s else remove attribute end end |
#classes ⇒ Object
44 45 46 |
# File 'lib/arbre/html/attributes.rb', line 44 def classes @attributes['class'] ||= ClassList.new end |
#classes=(value) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/arbre/html/attributes.rb', line 48 def classes=(value) if value.present? @attributes['class'] = ClassList.new(value) else remove 'class' end end |
#eql?(other) ⇒ Boolean
80 81 82 |
# File 'lib/arbre/html/attributes.rb', line 80 def eql?(other) other.is_a?(Attributes) && self == other end |
#has_key?(key) ⇒ Boolean
84 85 86 |
# File 'lib/arbre/html/attributes.rb', line 84 def has_key?(key) @attributes.has_key?(key.to_s) end |
#pairs ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/arbre/html/attributes.rb', line 91 def pairs map do |name, value| next if name == 'class' && value.blank? next if name == 'style' && value.blank? "#{html_escape(name)}=\"#{html_escape(value)}\"" end end |
#remove(attribute) ⇒ Object
68 69 70 |
# File 'lib/arbre/html/attributes.rb', line 68 def remove(attribute) @attributes.delete attribute.to_s end |
#style ⇒ Object
56 57 58 |
# File 'lib/arbre/html/attributes.rb', line 56 def style @attributes['style'] ||= StyleHash.new end |
#style=(value) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/arbre/html/attributes.rb', line 60 def style=(value) if value.present? @attributes['style'] = StyleHash.new(value) else remove 'style' end end |
#to_hash ⇒ Object
99 100 101 |
# File 'lib/arbre/html/attributes.rb', line 99 def to_hash @attributes end |
#to_s ⇒ Object
103 104 105 |
# File 'lib/arbre/html/attributes.rb', line 103 def to_s pairs.join(' ').html_safe end |
#update(attributes) ⇒ Object
72 73 74 |
# File 'lib/arbre/html/attributes.rb', line 72 def update(attributes) attributes.each { |k, v| self[k] = v } end |