Sha256: 5ceb8006e4e03683383b6c0ed7ab6dfd7a8a4b0e35e4816e4e44a624dd87b3dc

Contents?: true

Size: 1.25 KB

Versions: 9

Compression:

Stored size: 1.25 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!
      replace(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!
      replace(symbolize_keys)
    end
  end

  def except(*args)
    hash = dup
    args.each {|k| hash.delete(k) }
    hash
  end

  def only(*args)
    hash = {}
    args.each {|k| hash[k] = self[k] if self.has_key?(k) }
    hash
  end

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

  def rename_keys!(*args)
    replace(rename_keys(*args))
  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!
    replace(symbolize_and_underscore_keys)
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
flash_extensions-3.2.3 lib/flash_extensions/extensions/hash_extension.rb
flash_extensions-3.2.2 lib/flash_extensions/extensions/hash_extension.rb
flash_extensions-3.2.1 lib/flash_extensions/extensions/hash_extension.rb
flash_extensions-3.2.0 lib/flash_extensions/extensions/hash_extension.rb
flash_extensions-3.1.1 lib/flash_extensions/extensions/hash_extension.rb
flash_extensions-3.1.0 lib/flash_extensions/extensions/hash_extension.rb
flash_extensions-3.0.0 lib/flash_extensions/extensions/hash_extension.rb
flash_extensions-1.1.0 lib/flash_extensions/extensions/hash_extension.rb
flash_extensions-1.0.0 lib/flash_extensions/extensions/hash_extension.rb