Sha256: dcef1996ca0e26540c27621dc5aa8acd186f87d4f66f464dfdfc90516adc2c49
Contents?: true
Size: 1.62 KB
Versions: 2
Compression:
Stored size: 1.62 KB
Contents
module CoreExtensions module Hash module ClassMethods module Nesting # create hash with default nested structures # @example # h = Hash.new_nested Hash, Array # h[:a] # => {} # h[:b][:c] # => [] def new_nested *classes initialize_nested classes.unshift ::Hash end def initialize_nested classes, level=0 return classes[level].new if level == classes.size - 1 classes[level].new do |h, k| h[k] = initialize_nested classes, level + 1 end end end end module Merging # attach CSS classes # @example # {}.css_merge({:class => "btn"}) # => {:class=>"btn"} # # h = {:class => "btn"} # => {:class=>"btn"} # h.css_merge({:class => "btn-primary"}) # => {:class=>"btn # btn-primary"} def css_merge other_hash, separator=' ' merge(other_hash) do |key, old, new| key == :class ? old.to_s + separator + new.to_s : new end end def css_merge! other_hash, separator=' ' merge!(other_hash) do |key, old, new| key == :class ? old.to_s + separator + new.to_s : new end end # merge string values with `separator` def string_merge other_hash, separator=' ' merge(other_hash) do |_key, old, new| old.is_a?(String) ? old + separator + new.to_s : new end end def string_merge! other_hash, separator=' ' merge!(other_hash) do |_key, old, new| old.is_a?(String) ? old + separator + new.to_s : new end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
card-1.18.6 | config/initializers/01_core_extensions/hash.rb |
card-1.18.5 | config/initializers/01_core_extensions/hash.rb |