Sha256: d73d1992d854627da80f93123cfdbc48e152d0a799909c4bc231c711db4d1f37

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

class Hash

  ##
  ## CONVERTING
  ##

  #
  # convert self into a hash, but only include the specified keys
  #
  def pick(*keys)
    keys.map(&:to_s).inject({}) do |hsh, key|
      if has_key?(key)
        hsh[key] = self[key]
      end
      hsh
    end
  end

  #
  # recursive merging (aka deep merge)
  # taken from ActiveSupport::CoreExtensions::Hash::DeepMerge
  #
  def deep_merge(other_hash)
    self.merge(other_hash) do |key, oldval, newval|
      oldval = oldval.to_hash if oldval.respond_to?(:to_hash)
      newval = newval.to_hash if newval.respond_to?(:to_hash)
      oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? oldval.deep_merge(newval) : newval
    end
  end

  def deep_merge!(other_hash)
    replace(deep_merge(other_hash))
  end

  #
  # A recursive symbolize_keys
  #
  unless Hash.method_defined?(:symbolize_keys)
    def symbolize_keys
      self.inject({}) {|result, (key, value)|
        new_key = case key
                  when String then key.to_sym
                  else key
                  end
        new_value = case value
                    when Hash then symbolize_keys(value)
                    else value
                    end
        result[new_key] = new_value
        result
      }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
leap_cli-1.9.2 lib/leap_cli/core_ext/hash.rb
leap_cli-1.9.1 lib/leap_cli/core_ext/hash.rb
leap_cli-1.9 lib/leap_cli/core_ext/hash.rb