class Simple::HashWithIndifferentAccess

same as ActiveSupport::HashWithIndifferentAccess except it does deep value conversion github.com/rails/rails/blob/master/activesupport/lib/active_support/hash_with_indifferent_access.rb

Public Class Methods

[](*args) click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 36
def self.[](*args)
  new.merge!(Hash[*args])
end
new(constructor = {}) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 23
def initialize(constructor = {})
  if constructor.respond_to?(:to_hash)
    super()
    update(constructor)

    hash = constructor.to_hash
    self.default = hash.default if hash.default
    self.default_proc = hash.default_proc if hash.default_proc
  else
    super(constructor)
  end
end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 73
def [](key)
  convert_value(super(convert_key(key)))
end
[]=(key, value) click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 43
def []=(key, value)
  regular_writer(convert_key(key), convert_value(value, for: :assignment))
end
Also aliased as: regular_writer, store
assoc(key) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 77
def assoc(key)
  convert_values(super(convert_key(key)))
end
compact() click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 187
def compact
  dup.tap(&:compact!)
end
deep_stringify_keys() click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 141
def deep_stringify_keys; dup end
deep_stringify_keys!() click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 139
def deep_stringify_keys!; self end
deep_symbolize_keys() click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 146
def deep_symbolize_keys; to_hash.deep_symbolize_keys! end
default(*args) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 90
def default(*args)
  super(*args.map { |arg| convert_key(arg) })
end
delete(key) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 128
def delete(key)
  super(convert_key(key))
end
dig(*args) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 85
def dig(*args)
  args[0] = convert_key(args[0]) if args.size > 0
  convert_value(super(*args))
end
dup() click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 102
def dup
  self.class.new(self).tap do |new_hash|
    set_defaults(new_hash)
  end
end
except(*keys) click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 132
def except(*keys)
  slice(*self.keys - keys.map { |key| convert_key(key) })
end
Also aliased as: without
extractable_options?() click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 11
def extractable_options?
  true
end
fetch(key, *extras) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 81
def fetch(key, *extras)
  convert_value(super(convert_key(key), *extras))
end
fetch_values(*indices, &block) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 98
def fetch_values(*indices, &block)
  convert_values(super(*indices.map { |key| convert_key(key) }, &block))
end
has_key?(key)
Alias for: key?
include?(key)
Alias for: key?
key?(key) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 65
def key?(key)
  super(convert_key(key))
end
Also aliased as: include?, has_key?, member?
member?(key)
Alias for: key?
merge(hash, &block) click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 108
def merge(hash, &block)
  dup.update(hash, &block)
end
merge!(other_hash)
Alias for: update
nested_under_indifferent_access() click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 19
def nested_under_indifferent_access
  self
end
regular_update(other_hash)
Alias for: update
regular_writer(key, value)
Alias for: []=
reject(*args, &block) click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 154
def reject(*args, &block)
  return to_enum(:reject) unless block_given?
  dup.tap { |hash| hash.reject!(*args, &block) }
end
replace(other_hash) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 124
def replace(other_hash)
  super(self.class.new(other_hash))
end
reverse_merge(other_hash) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 112
def reverse_merge(other_hash)
  super(self.class.new(other_hash))
end
Also aliased as: with_defaults
reverse_merge!(other_hash) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 118
def reverse_merge!(other_hash)
  super(self.class.new(other_hash))
end
Also aliased as: with_defaults!
select(*args, &block) click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 149
def select(*args, &block)
  return to_enum(:select) unless block_given?
  dup.tap { |hash| hash.select!(*args, &block) }
end
slice(*keys) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 177
def slice(*keys)
  keys.map! { |key| convert_key(key) }
  self.class.new(super)
end
slice!(*keys) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 182
def slice!(*keys)
  keys.map! { |key| convert_key(key) }
  super
end
store(key, value)
Alias for: []=
stringify_keys() click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 140
def stringify_keys; dup end
stringify_keys!() click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 138
def stringify_keys!; self end
symbolize_keys() click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 144
def symbolize_keys; to_hash.symbolize_keys! end
Also aliased as: to_options
to_hash() click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 191
def to_hash
  _new_hash = Hash.new
  set_defaults(_new_hash)

  each do |key, value|
    _new_hash[key] = convert_value(value, for: :to_hash)
  end
  _new_hash
end
to_options()
Alias for: symbolize_keys
to_options!() click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 147
def to_options!; self end
transform_keys(*args, &block) click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 164
def transform_keys(*args, &block)
  return to_enum(:transform_keys) unless block_given?
  dup.tap { |hash| hash.transform_keys!(*args, &block) }
end
transform_keys!() { |key| ... } click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 169
def transform_keys!
  return enum_for(:transform_keys!) { size } unless block_given?
  keys.each do |key|
    self[yield(key)] = delete(key)
  end
  self
end
transform_values(*args, &block) click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 159
def transform_values(*args, &block)
  return to_enum(:transform_values) unless block_given?
  dup.tap { |hash| hash.transform_values!(*args, &block) }
end
update(other_hash) { |convert_key(key), self, value| ... } click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 49
def update(other_hash)
  if other_hash.is_a?(Simple::HashWithIndifferentAccess)
    super(other_hash)
  else
    other_hash.to_hash.each_pair do |key, value|
      if block_given? && key?(key)
        value = yield(convert_key(key), self[key], value)
      end
      regular_writer(convert_key(key), convert_value(value))
    end
    self
  end
end
Also aliased as: regular_update, merge!
values_at(*keys) click to toggle source
Calls superclass method
# File lib/simple/hash_with_indifferent_access.rb, line 94
def values_at(*keys)
  convert_values(super(*keys.map { |key| convert_key(key) }))
end
with_defaults(other_hash)
Alias for: reverse_merge
with_defaults!(other_hash)
Alias for: reverse_merge!
with_indifferent_access() click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 15
def with_indifferent_access
  dup
end
without(*keys)
Alias for: except

Private Instance Methods

convert_key(key) click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 207
def convert_key(key)
  key.kind_of?(Symbol) ? key.to_s : key
end
convert_keys(keys) click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 203
def convert_keys(keys)
  keys.map { |key| convert_key(key) }
end
convert_value(value, options = {}) click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 215
def convert_value(value, options = {})
  if value.is_a? Hash
    if options[:for] == :to_hash
      value.to_hash
    else
      value.nested_under_indifferent_access
    end
  elsif value.is_a?(Array)
    if options[:for] != :assignment || value.frozen?
      value = value.dup
    end
    value.map! { |e| convert_value(e, options) }
  else
    value
  end
end
convert_values(values) click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 211
def convert_values(values)
  values.map { |value| convert_value(value) }
end
set_defaults(target) click to toggle source
# File lib/simple/hash_with_indifferent_access.rb, line 232
def set_defaults(target)
  if default_proc
    target.default_proc = default_proc.dup
  else
    target.default = default
  end
end