Sha256: c707722bd83bed449c7b9a2ed1d41cb2b744f85b370f3c2298b14907133102c5

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

class Hash

  unless method_defined?("stringify_keys")
    def stringify_keys
      inject({}) do |options, (key, value)|
        options[key.to_s] = value
        options
      end
    end
  end

  unless method_defined?("stringify_keys!")
    def stringify_keys!
      self.replace(self.stringify_keys)
    end
  end

  unless method_defined?("symbolize_keys")
    def symbolize_keys
      inject({}) do |options, (key, value)|
        options[(key.to_sym rescue key) || key] = value
        options
      end
    end
  end

  unless method_defined?("symbolize_keys!")
    def symbolize_keys!
      self.replace(self.symbolize_keys)
    end
  end

  def rename_keys(*args)
    keys = Hash[*args.flatten]
    keys.each { |k, v| self[v] = self.delete(k) if self[k] }
    self
  end

  def symbolize_and_underscore_keys
    inject({}) do |options, (key, value)|
      options[(key.to_s.underscore.to_sym rescue key) || key] = value
      options
    end
  end

  def symbolize_and_underscore_keys!
    self.replace(self.symbolize_and_underscore_keys)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flash_extensions-0.0.1 lib/flash_extensions/extensions/hash_extension.rb