Sha256: 3a9c43488561e418738b030068c35344ee7635bc01e57836ba0e1c7dd438f7f9

Contents?: true

Size: 1.62 KB

Versions: 33

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

33 entries across 33 versions & 1 rubygems

Version Path
card-1.94.1 config/initializers/01_core_extensions/hash.rb
card-1.94.0 config/initializers/01_core_extensions/hash.rb
card-1.93.13 config/initializers/01_core_extensions/hash.rb
card-1.93.12 config/initializers/01_core_extensions/hash.rb
card-1.93.11 config/initializers/01_core_extensions/hash.rb
card-1.93.10 config/initializers/01_core_extensions/hash.rb
card-1.93.9 config/initializers/01_core_extensions/hash.rb
card-1.93.8 config/initializers/01_core_extensions/hash.rb
card-1.93.7 config/initializers/01_core_extensions/hash.rb
card-1.93.6 config/initializers/01_core_extensions/hash.rb
card-1.93.5 config/initializers/01_core_extensions/hash.rb
card-1.93.4 config/initializers/01_core_extensions/hash.rb
card-1.93.3 config/initializers/01_core_extensions/hash.rb
card-1.93.2 config/initializers/01_core_extensions/hash.rb
card-1.93.1 config/initializers/01_core_extensions/hash.rb
card-1.93.0 config/initializers/01_core_extensions/hash.rb
card-1.92.2 config/initializers/01_core_extensions/hash.rb
card-1.92.1 config/initializers/01_core_extensions/hash.rb
card-1.92 config/initializers/01_core_extensions/hash.rb
card-1.91 config/initializers/01_core_extensions/hash.rb