Class: SparkComponents::Attributes::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/spark_components/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj = {}) ⇒ Tag

Returns a new instance of Tag.



118
119
120
121
# File 'lib/spark_components/attributes.rb', line 118

def initialize(obj = {})
  @attrs = Attributes::Hash.new
  merge!(obj)
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



116
117
118
# File 'lib/spark_components/attributes.rb', line 116

def attrs
  @attrs
end

Instance Method Details

#add_class(*args) ⇒ Object



146
147
148
# File 'lib/spark_components/attributes.rb', line 146

def add_class(*args)
  classnames.add(*args)
end

#aria(obj = {}) ⇒ Object



128
129
130
131
132
# File 'lib/spark_components/attributes.rb', line 128

def aria(obj = {})
  attrs[:aria] ||= Aria.new
  attrs[:aria].add(obj) unless obj.empty?
  attrs[:aria]
end

#base_class(name) ⇒ Object



150
151
152
153
# File 'lib/spark_components/attributes.rb', line 150

def base_class(name)
  classnames.base = name unless name.nil?
  classnames.base
end

#classnames(*args) ⇒ Object



140
141
142
143
144
# File 'lib/spark_components/attributes.rb', line 140

def classnames(*args)
  attrs[:class] ||= Classname.new
  attrs[:class].add(*args) unless args.empty?
  attrs[:class]
end

#data(obj = {}) ⇒ Object



134
135
136
137
138
# File 'lib/spark_components/attributes.rb', line 134

def data(obj = {})
  attrs[:data] ||= Data.new
  attrs[:data].add(obj) unless obj.empty?
  attrs[:data]
end

#dupObject

Ensure each attribute is distinct



164
165
166
167
168
# File 'lib/spark_components/attributes.rb', line 164

def dup
  new(attrs.each_with_object(Attributes::Hash.new) do |(k, v), obj|
    obj[k] = v.dup
  end)
end

#join_class(*args) ⇒ Object



155
156
157
# File 'lib/spark_components/attributes.rb', line 155

def join_class(*args)
  classnames.join_class(*args)
end

#merge(obj = {}) ⇒ Object



174
175
176
# File 'lib/spark_components/attributes.rb', line 174

def merge(obj = {})
  merge_obj(dup, obj)
end

#merge!(obj = {}) ⇒ Object



170
171
172
# File 'lib/spark_components/attributes.rb', line 170

def merge!(obj = {})
  merge_obj(self, obj)
end

#merge_obj(tag, obj = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/spark_components/attributes.rb', line 178

def merge_obj(tag, obj = {})
  # If merging another Tag, extract attrs to merge
  obj = obj.attrs if obj.is_a?(Tag)

  obj.each do |key, val|
    if val.is_a?(Classname)
      # preserve object state
      tag.attrs[:class] = val
    else
      case key.to_sym
      when :class then tag.classnames.add(val)
      when :data, :aria then tag.send(key).add(val)
      else; tag.attrs[key] = val
      end
    end
  end
  tag
end

#new(obj = {}) ⇒ Object



159
160
161
# File 'lib/spark_components/attributes.rb', line 159

def new(obj = {})
  self.class.new(obj)
end

#root(obj = {}) ⇒ Object



123
124
125
126
# File 'lib/spark_components/attributes.rb', line 123

def root(obj = {})
  attrs.add(obj) unless obj.empty?
  attrs
end

#to_sObject



197
198
199
# File 'lib/spark_components/attributes.rb', line 197

def to_s
  attrs.to_s
end