Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/html_attributes/hash.rb

Instance Method Summary collapse

Instance Method Details

#flatten(sep = $,, prefix_keys = [], &block) ⇒ Object

Returns a new hash flatting the values (recursively), i.e. if a value of self is a hash again 1) the keys are build by combining the key of self (the one to access that hash value) with the keys of that hash; 2) the values are the values of that hash.

You can either specify a seperator to to build the keys joining them, or a block which gets an array of keys and returns the new key.

Examples

styles = { :padding => { :top => "8px", :right => "15px" }, :font_size => "12pt" }

styles.flatten("_")
# => { "padding_top" => "8px", "padding_right" => "15px", "font_size" => "12pt" }

styles.flatten { |keys| keys.join('-').gsub('_', '-') }
# => { "padding-top" => "8px", "padding-right" => "15px", "font-size" => "12pt" }


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/html_attributes/hash.rb', line 76

def flatten(sep = $,, prefix_keys = [], &block) # :nodoc:
  hash = self.class.new
  self.each do |key, value|
    prefix_keys.push(key)
    case value
    when ::Hash
      value.flatten(sep, prefix_keys, &block).each do |k, v|
        hash[k] = v
      end
    else
      hash[block_given? ? yield(prefix_keys) : prefix_keys.join(sep)] = value
    end
    prefix_keys.pop
  end
  hash
end

#flatten!(sep = $,, prefix_keys = [], &block) ⇒ Object

Flattens self in place.



94
95
96
# File 'lib/html_attributes/hash.rb', line 94

def flatten!(sep = $,, prefix_keys = [], &block) # :nodoc:
  replace(flatten(sep, prefix_keys, &block))
end

#to_class_attrObject

Returns a string containing the class names whose conditions are neighter false nor nil, or returns nil if no class names remains.

Examples

{ :first => true, :second => false, :third => true }.to_class_attr
# => "first third"

{ :foo => false, :bar => false }.to_class_attr
# => nil


14
15
16
17
18
19
# File 'lib/html_attributes/hash.rb', line 14

def to_class_attr
  self.select { |klass, condition| condition }.           # remove classes with nil or false condition
       map { |elem| elem.first.to_s }.                    # throw condition away
       sort.                                              # sort alphabetically
       to_class_attr                                      # join with ' ' or return nil if empty
end

#to_rel_attrObject

Returns a string containing the rel values whose conditions are neighter false nor nil, or returns nil if no rel values remains.

Examples

{ :nofollow => true, :other => false, :me => true }.to_rel_attr
# => "nofollow me"

{ :foo => false, :bar => false }.to_rel_attr
# => nil


32
33
34
35
36
37
# File 'lib/html_attributes/hash.rb', line 32

def to_rel_attr
  self.select { |klass, condition| condition }.           # remove rel values with nil or false condition
       map { |elem| elem.first.to_s }.                    # throw condition away
       sort.                                              # sort alphabetically
       to_rel_attr                                        # join with ' ' or return nil if empty
end

#to_style_attrObject

Returns a string containing a style attribute, or returns nil if no values remains.

Examples

{ :padding => { :top => "8px", :right => "15px" }, :font_size => "12pt" }.to_style_attr
# => "font-size: 12pt; padding-right: 15px; padding-top: 8px;"

visible = true
{ :display => visible ? nil : 'none' }.to_style_attr
# => nil


50
51
52
53
54
55
56
# File 'lib/html_attributes/hash.rb', line 50

def to_style_attr
  self.flatten { |keys| keys.join('_').gsub('_', '-') }.  # build CSS like key names
       select { |key, value| !value.nil? }.               # remove properties with nil value
       map { |key, value| "#{key}: #{value}" }.           # build property
       sort.                                              # sort alphabetically
       to_style_attr                                      # join with '; ' or return nil if empty
end