Class: Spark::Component::TagAttr

Inherits:
Attr
  • Object
show all
Defined in:
lib/spark/component/tag_attr.rb

Instance Method Summary collapse

Methods inherited from Attr

#initialize, #to_s

Constructor Details

This class inherits a constructor from Spark::Component::Attr

Instance Method Details

#add(hash) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/spark/component/tag_attr.rb', line 9

def add(hash)
  return self if hash.nil? || hash.keys.empty?

  deep_compact!(hash)

  # Extract keys for special properties
  extracted = {
    aria: hash.delete(:aria),
    data: hash.delete(:data),
    classname: hash.delete(:class)
  }

  # If extracted values exist, add to tag
  extracted.each do |method, val|
    send(method).add(val) unless val.nil? || val.empty?
  end

  # Add html using recursion, this ensures that nested data, aria, or class
  # attributes are properly merged as the right data types
  add(hash.delete(:html))

  super(hash)
end

#aria(*args) ⇒ Object



33
34
35
# File 'lib/spark/component/tag_attr.rb', line 33

def aria(*args)
  self[:aria] ||= Component::Attr.new(*args, prefix: :aria)
end

#classname(*args, base: nil) ⇒ Object



41
42
43
# File 'lib/spark/component/tag_attr.rb', line 41

def classname(*args, base: nil)
  self[:class] ||= Component::Classname.new(*args, base: base)
end

#data(*args) ⇒ Object



37
38
39
# File 'lib/spark/component/tag_attr.rb', line 37

def data(*args)
  self[:data] ||= Component::Attr.new(*args, prefix: :data)
end