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(*args) ⇒ Object



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

def add(*args)
  args.each do |arg|
    arg.is_a?(::Hash) ? merge!(arg) : self[arg.to_sym] = nil
  end
  self
end

#new(*args) ⇒ Object

Easy assess to create a new Attributes::Hash



30
31
32
33
# File 'lib/spark_components/attributes.rb', line 30

def new(*args)
  new_obj = self.class.new
  new_obj.add(*args)
end

#prefixObject



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

def prefix; end

#to_sObject

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



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/spark_components/attributes.rb', line 16

def to_s
  each_with_object([]) do |(name, value), array|
    if value.is_a?(Attributes::Hash)
      # Flatten nested hashs and inject them unless empty
      value = value.to_s
      array << value unless value.empty?
    else
      name = [prefix, name].compact.join("-").gsub(/[\W_]+/, "-")
      array << %(#{name}="#{value}") unless value.nil?
    end
  end.sort.join(" ")
end