class Pakyow::Support::IndifferentHash
Creates a Hash-like object can access stored data with symbol or
string keys.
The original hash is converted to symbol keys, which means
that a hash that originally contains a symbol and string key with the same symbold value will conflict. It is not guaranteed which value will be saved.
IndifferentHash
instances have the same api as Hash, but any method
that would return a Hash, will return an IndifferentHash (with the exception of to_h/to_hash).
NOTE: Please lookup Ruby's documentation for Hash to learn what
methods are available.
@example
{ test: "test1", "test" => "test2" } => { test: "test2" }
Public Class Methods
deep(object)
click to toggle source
# File lib/pakyow/support/indifferentize.rb, line 27 def deep(object) hash = object.to_h unless hash.empty? hash = hash.each_with_object({}) { |(key, value), new_hash| new_hash[key] = case value when Hash deep(value) when Array value.map { |value_item| case value_item when Hash deep(value_item) else value_item end } else value end } end self.new(hash) end
new(hash = {})
click to toggle source
# File lib/pakyow/support/indifferentize.rb, line 103 def initialize(hash = {}) self.internal_hash = hash end
Private Class Methods
indifferent_key_method(*methods)
click to toggle source
# File lib/pakyow/support/indifferentize.rb, line 54 def indifferent_key_method(*methods) methods.each do |name| define_method(name) do |key = nil, *args, &block| key = convert_key(key) internal_hash.public_send(name, key, *args, &block) end end end
indifferent_multi_key_method(*methods)
click to toggle source
# File lib/pakyow/support/indifferentize.rb, line 63 def indifferent_multi_key_method(*methods) methods.each do |name| define_method(name) do |*keys, &block| keys = keys.map { |key| convert_key(key) } internal_hash.public_send(name, *keys, &block) end end end
indifferentize_argument_method(*methods)
click to toggle source
# File lib/pakyow/support/indifferentize.rb, line 93 def indifferentize_argument_method(*methods) methods.each do |name| define_method(name) do |*args, &block| args = args.map { |arg| stringify_keys(arg) } internal_hash.public_send(name, *args, &block) end end end
indifferentize_return_method(*methods)
click to toggle source
# File lib/pakyow/support/indifferentize.rb, line 74 def indifferentize_return_method(*methods) methods.each do |name| define_method(name) do |*args, &block| hash = internal_hash.public_send(name, *args, &block) self.class.new(hash) if hash end end end
indifferentize_update_method(*methods)
click to toggle source
# File lib/pakyow/support/indifferentize.rb, line 83 def indifferentize_update_method(*methods) methods.each do |name| define_method(name) do |*args, &block| args = args.map { |arg| stringify_keys(arg) } hash = internal_hash.public_send(name, *args, &block) self if hash end end end
Public Instance Methods
internal_hash()
click to toggle source
@api private
# File lib/pakyow/support/indifferentize.rb, line 114 def internal_hash __getobj__ end
pp(*args)
click to toggle source
Fixes an issue using pp inside a delegator.
# File lib/pakyow/support/indifferentize.rb, line 141 def pp(*args) Kernel.pp(*args) end
to_h()
click to toggle source
# File lib/pakyow/support/indifferentize.rb, line 118 def to_h internal_hash.each_with_object({}) { |(key, value), new_hash| key = case key when String key.to_sym else key end value = case value when IndifferentHash value.to_h else value end new_hash[key] = value } end
Also aliased as: to_hash
Private Instance Methods
convert_key(key)
click to toggle source
# File lib/pakyow/support/indifferentize.rb, line 162 def convert_key(key) case key when Symbol key.to_s else key end end
internal_hash=(other)
click to toggle source
# File lib/pakyow/support/indifferentize.rb, line 147 def internal_hash=(other) __setobj__(stringify_keys(other)) end
stringify_keys(object)
click to toggle source
# File lib/pakyow/support/indifferentize.rb, line 151 def stringify_keys(object) return object unless object.respond_to?(:to_h) converted = {} object.to_h.each do |key, value| converted[convert_key(key)] = value end converted end