Method: Hash#to_class_attr
- Defined in:
- lib/html_attributes/hash.rb
#to_class_attr ⇒ Object
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 |