class Hash

Monkey patches for the ruby Hash

Public Instance Methods

deep_stringify_keys() click to toggle source

Depp converts all keys to string

@return [Hash<String>]

# File lib/sidekiq_unique_jobs/core_ext.rb, line 30
def deep_stringify_keys
  deep_transform_keys(&:to_s)
end
deep_transform_keys(&block) click to toggle source

Deep transfor all keys by yielding to the caller

@return [Hash<String>]

# File lib/sidekiq_unique_jobs/core_ext.rb, line 42
def deep_transform_keys(&block)
  _deep_transform_keys_in_object(self, &block)
end
slice(*keys) click to toggle source

Returns only the matching keys in a new hash

@param [Array<String>, Array<Symbol>] keys the keys to match

@return [Hash]

# File lib/sidekiq_unique_jobs/core_ext.rb, line 17
def slice(*keys)
  keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
  keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if key?(k) }
end
slice!(*keys) click to toggle source

Removes all keys not provided from the current hash and returns it

@param [Array<String>, Array<Symbol>] keys the keys to match

@return [Hash]

# File lib/sidekiq_unique_jobs/core_ext.rb, line 83
def slice!(*keys)
  keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
  omit = slice(*self.keys - keys)
  hash = slice(*keys)
  hash.default      = default
  hash.default_proc = default_proc if default_proc
  replace(hash)
  omit
end
stringify_keys() click to toggle source

Converts all keys to string

@return [Hash<String>]

# File lib/sidekiq_unique_jobs/core_ext.rb, line 54
def stringify_keys
  transform_keys(&:to_s)
end
transform_keys() { |key| ... } click to toggle source

Transforms all keys by yielding to the caller

@return [Hash]

# File lib/sidekiq_unique_jobs/core_ext.rb, line 66
def transform_keys
  result = {}
  each_key do |key|
    result[yield(key)] = self[key]
  end
  result
end

Private Instance Methods

_deep_transform_keys_in_object(object) { |key| ... } click to toggle source

support methods for deep transforming nested hashes and arrays

# File lib/sidekiq_unique_jobs/core_ext.rb, line 98
def _deep_transform_keys_in_object(object, &block)
  case object
  when Hash
    object.each_with_object({}) do |(key, value), result|
      result[yield(key)] = _deep_transform_keys_in_object(value, &block)
    end
  when Array
    object.map { |element| _deep_transform_keys_in_object(element, &block) }
  else
    object
  end
end