class IndifferentHash

This class has been permamently borrowed from Sinatra, that repo is MIT licensed and has these copyright notices:

Copyright (c) 2007, 2008, 2009 Blake Mizerany
Copyright (c) 2010-2017 Konstantin Haase
Copyright (c) 2015-2017 Zachary Scott

Public Class Methods

[](*args) click to toggle source
# File lib/triton/indifferent_hash.rb, line 10
def self.[](*args)
  new.merge!(Hash[*args])
end
new(*args) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 14
def initialize(*args)
  super(*args.map(&method(:convert_value)))
end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 38
def [](key)
  super(convert_key(key))
end
[]=(key, value) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 42
def []=(key, value)
  super(convert_key(key), convert_value(value))
end
Also aliased as: store
assoc(key) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 26
def assoc(key)
  super(convert_key(key))
end
default(*args) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 18
def default(*args)
  super(*args.map(&method(:convert_key)))
end
default=(value) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 22
def default=(value)
  super(convert_value(value))
end
delete(key) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 66
def delete(key)
  super(convert_key(key))
end
dig(key, *other_keys) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 70
def dig(key, *other_keys)
  super(convert_key(key), *other_keys)
end
fetch(key, *args) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 34
def fetch(key, *args)
  super(convert_key(key), *args.map(&method(:convert_value)))
end
fetch_values(*keys) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 74
def fetch_values(*keys)
  super(*keys.map(&method(:convert_key)))
end
has_key?(key)
Alias for: key?
has_value?(value)
Alias for: value?
include?(key)
Alias for: key?
key(value) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 48
def key(value)
  super(convert_value(value))
end
key?(key) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 52
def key?(key)
  super(convert_key(key))
end
Also aliased as: has_key?, include?, member?
member?(key)
Alias for: key?
merge(other_hash, &block) click to toggle source
# File lib/triton/indifferent_hash.rb, line 96
def merge(other_hash, &block)
  dup.merge!(other_hash, &block)
end
merge!(other_hash) { |key, self, value| ... } click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 82
def merge!(other_hash)
  return super if other_hash.is_a?(self.class)

  other_hash.each_pair do |key, value|
    key = convert_key(key)
    value = yield(key, self[key], value) if block_given? && key?(key)
    self[key] = convert_value(value)
  end

  self
end
Also aliased as: update
rassoc(value) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 30
def rassoc(value)
  super(convert_value(value))
end
replace(other_hash) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 100
def replace(other_hash)
  super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash])
end
store(key, value)
Alias for: []=
update(other_hash)
Alias for: merge!
value?(value) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 60
def value?(value)
  super(convert_value(value))
end
Also aliased as: has_value?
values_at(*keys) click to toggle source
Calls superclass method
# File lib/triton/indifferent_hash.rb, line 78
def values_at(*keys)
  super(*keys.map(&method(:convert_key)))
end

Private Instance Methods

convert_key(key) click to toggle source
# File lib/triton/indifferent_hash.rb, line 106
def convert_key(key)
  key.is_a?(Symbol) ? key.to_s : key
end
convert_value(value) click to toggle source
# File lib/triton/indifferent_hash.rb, line 110
def convert_value(value)
  case value
  when Hash
    value.is_a?(self.class) ? value : self.class[value]
  when Array
    value.map(&method(:convert_value))
  else
    value
  end
end