module Spark::Component::Attribute::ClassMethods

Public Instance Methods

aria_attribute(*args) click to toggle source

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

# File lib/spark/component/attribute.rb, line 278
def aria_attribute(*args)
  tag_attribute(aria: hash_from_args(*args))
end
aria_attributes() click to toggle source

Store attributes to be added to tag_attrs aria

# File lib/spark/component/attribute.rb, line 250
def aria_attributes
  @aria_attributes ||= []
end
attribute(*args) click to toggle source

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 }
# 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) click to toggle source
# File lib/spark/component/attribute.rb, line 287
def attribute_default_group(object)
  attribute_default_groups.merge!(object)
end
attribute_default_groups() click to toggle source
# File lib/spark/component/attribute.rb, line 291
def attribute_default_groups
  @attribute_default_groups ||= {}
end
attributes() click to toggle source
# 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) click to toggle source

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

# File lib/spark/component/attribute.rb, line 283
def data_attribute(*args)
  tag_attribute(data: hash_from_args(*args))
end
data_attributes() click to toggle source

Store attributes to be added to tag_attrs data

# File lib/spark/component/attribute.rb, line 255
def data_attributes
  @data_attributes ||= []
end
tag_attribute(*args) click to toggle source

Add attribute(s) and automatically add to tag_attr

# 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_attributes() click to toggle source

Store attributes to be added to tag_attrs

# File lib/spark/component/attribute.rb, line 245
def tag_attributes
  @tag_attributes ||= BASE_ATTRIBUTES.dup
end
validates_attr(name, options = {}) click to toggle source

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)
# 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

Private Instance Methods

hash_from_args(*args) click to toggle source

Convert mixed arguments to a hash Example: (:a, :b, c: true) => { a: nil, b: nil, c: true }

# File lib/spark/component/attribute.rb, line 310
def hash_from_args(*args)
  args.each_with_object({}) do |arg, obj|
    arg.is_a?(Hash) ? obj.merge!(arg) : obj[arg] = nil
  end
end
set_attribute(name, default: nil) click to toggle source

Store attributes and define methods for validation

# File lib/spark/component/attribute.rb, line 298
def set_attribute(name, default: nil)
  attributes[name] = default

  # Define a method to access attribute to support validation
  # Namespace attribute methods to prevent collision with methods or elements
  define_method(:"attribute_#{name}") do
    instance_variable_get(:"@#{name}")
  end
end