Class: SparkComponents::Attributes::Hash

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

Direct Known Subclasses

Aria, Data

Instance Method Summary collapse

Instance Method Details

#add(obj = nil) ⇒ Object



8
9
10
11
# File 'lib/spark_components/attributes.rb', line 8

def add(obj = nil)
  merge!(obj) unless obj.nil?
  self
end

#collapseObject



21
22
23
24
25
26
27
# File 'lib/spark_components/attributes.rb', line 21

def collapse
  each_with_object({}) do |(name, value), obj|
    name = [prefix, name].compact.join("-")
    name = name.downcase.gsub(/[\W_]+/, "-")
    obj[name] = value unless value.nil? || value.is_a?(String) && value.empty?
  end
end

#prefixObject



6
# File 'lib/spark_components/attributes.rb', line 6

def prefix; end

#to_sObject

Output all attributes as [base-]name=“value”



14
15
16
17
18
19
# File 'lib/spark_components/attributes.rb', line 14

def to_s
  each_with_object([]) do |(name, value), array|
    name = [prefix, name].compact.join("-")
    array << %(#{name.dasherize}="#{value}") unless value.nil?
  end.join(" ").html_safe
end