Module: Spark::Component::Attribute::ClassMethods

Defined in:
lib/spark/component/attribute.rb

Instance Method Summary collapse

Instance Method Details

#aria_attribute(*args) ⇒ Object

Add attribute(s) and automatically add to tag_attr’s aria hash



278
279
280
# File 'lib/spark/component/attribute.rb', line 278

def aria_attribute(*args)
  tag_attribute(aria: hash_from_args(*args))
end

#aria_attributesObject

Store attributes to be added to tag_attrs aria



250
251
252
# File 'lib/spark/component/attribute.rb', line 250

def aria_attributes
  @aria_attributes ||= []
end

#attribute(*args) ⇒ Object

Sets attributes, accepts an array of keys, pass a hash to set default values

Examples:

- attribute(:foo, :bar, :baz)               => { foo: nil, bar: nil, baz: nil }
- attribute(:foo, :bar, { baz: true })      => { foo: nil, bar: nil, baz: true }
- attribute(foo: true, bar: true, baz: nil) => { foo: true, bar: true, baz: nil }


206
207
208
209
210
211
212
213
214
215
216
# File 'lib/spark/component/attribute.rb', line 206

def attribute(*args)
  args.each do |arg|
    if arg.is_a?(::Hash)
      arg.each do |key, value|
        set_attribute(key.to_sym, default: value)
      end
    else
      set_attribute(arg.to_sym)
    end
  end
end

#attribute_default_group(object) ⇒ Object



287
288
289
# File 'lib/spark/component/attribute.rb', line 287

def attribute_default_group(object)
  attribute_default_groups.merge!(object)
end

#attribute_default_groupsObject



291
292
293
# File 'lib/spark/component/attribute.rb', line 291

def attribute_default_groups
  @attribute_default_groups ||= {}
end

#attributesObject



192
193
194
195
196
197
# File 'lib/spark/component/attribute.rb', line 192

def attributes
  # Set attributes default to a hash using keys defined by BASE_ATTRIBUTES
  @attributes ||= Spark::Component::BASE_ATTRIBUTES.each_with_object({}) do |val, obj|
    obj[val] = nil
  end
end

#data_attribute(*args) ⇒ Object

Add attribute(s) and automatically add to tag_attr’s data hash



283
284
285
# File 'lib/spark/component/attribute.rb', line 283

def data_attribute(*args)
  tag_attribute(data: hash_from_args(*args))
end

#data_attributesObject

Store attributes to be added to tag_attrs data



255
256
257
# File 'lib/spark/component/attribute.rb', line 255

def data_attributes
  @data_attributes ||= []
end

#tag_attribute(*args) ⇒ Object

Add attribute(s) and automatically add to tag_attr



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/spark/component/attribute.rb', line 260

def tag_attribute(*args)
  attr_object = hash_from_args(*args)

  if (aria_object = attr_object.delete(:aria))
    attribute(aria_object)
    aria_attributes.concat(aria_object.keys)
  end

  if (data_object = attr_object.delete(:data))
    attribute(data_object)
    data_attributes.concat(data_object.keys)
  end

  attribute(attr_object)
  tag_attributes.concat(attr_object.keys)
end

#tag_attributesObject

Store attributes to be added to tag_attrs



245
246
247
# File 'lib/spark/component/attribute.rb', line 245

def tag_attributes
  @tag_attributes ||= BASE_ATTRIBUTES.dup
end

#validates_attr(name, options = {}) ⇒ Object

A namespaced passthrough for validating attributes

Option: ‘choices` - easily validate against an array

Essentially a simplification of `inclusion` for attributes.

Examples:

- validates_attr(:size, choices: %i[small medium large])
- validates_attr(:size, choices: SIZES, allow_blank: true)


227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/spark/component/attribute.rb', line 227

def validates_attr(name, options = {})
  name = :"attribute_#{name}"

  if (choices = options.delete(:choices))
    supported_choices = choices.map do |c|
      c.is_a?(String) ? c.to_sym : c.to_s
    end.concat(choices)

    choices = choices.map(&:inspect).to_sentence(last_word_connector: ", or ")
    message = "\"%<value>s\" is not valid. Options include: #{choices}."

    options.merge!(inclusion: { in: supported_choices, message: message })
  end

  validates(name, options)
end