Sha256: fd54d56ab583fc6ae7e29fd850f86659c8ad7dbc43ff438d226de904c992ca5a

Contents?: true

Size: 843 Bytes

Versions: 2

Compression:

Stored size: 843 Bytes

Contents

# Hash Override taken from Rails
class Hash

  def transform_keys
    return enum_for(:transform_keys) unless block_given?
    result = self.class.new
    each_key do |key|
      result[yield(key)] = self[key]
    end
    result
  end

  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 {|e| _deep_transform_keys_in_object(e, &block) }
    else
      object
    end
  end

  def deep_transform_keys(&block)
    _deep_transform_keys_in_object(self, &block)
  end

  def symbolize_keys
    transform_keys { |key| key.to_sym rescue key }
  end

  def deep_symbolize_keys
    deep_transform_keys { |key| key.to_sym rescue key }
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
api_ai_wrapper-1.0.5 lib/api_ai_wrapper/extensions/hash.rb
api_ai_wrapper-1.0.4 lib/api_ai_wrapper/extensions/hash.rb