Class: Arbre::Html::Attributes

Inherits:
Object
  • Object
show all
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 or nil will remove it.

Class Method Summary collapse

Instance Method Summary collapse

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

#classesObject



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

Returns:

  • (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

Returns:

  • (Boolean)


84
85
86
# File 'lib/arbre/html/attributes.rb', line 84

def has_key?(key)
  @attributes.has_key?(key.to_s)
end

#pairsObject



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

#styleObject



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_hashObject



99
100
101
# File 'lib/arbre/html/attributes.rb', line 99

def to_hash
  @attributes
end

#to_sObject



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