Class: Formular::Attributes

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](hash) ⇒ Object



3
4
5
6
# File 'lib/formular/attributes.rb', line 3

def self.[](hash)
  hash ||= {}
  super
end

Instance Method Details

#merge(hash) ⇒ Object



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

def merge(hash)
  dup.merge!(hash)
end

#merge!(hash) ⇒ Object



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

def merge!(hash)
  classes     = self[:class]
  new_classes = hash[:class]
  return super unless classes && new_classes

  hash[:class] += classes
  super
end

#to_htmlObject

converts the hash into a string k1=v1 k2=v2 replaces underscores with - so we can use regular keys allows one layer of nestedhashes so we can define data options as a hash.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/formular/attributes.rb', line 24

def to_html
  map do |key,val|
    if val.is_a?(Hash)
      val.map do |k,v|
        %(#{key_to_attr_name(key)}-#{key_to_attr_name(k)}="#{val_to_string(v)}")
      end.join(" ")
    else
       %(#{key_to_attr_name(key)}="#{val_to_string(val)}")
    end
  end.join(" ")
end