module Midpay::HashExtensions::IndifferentAccess
Public Class Methods
included(base)
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 4 def self.included(base) base.class_eval do alias_method :regular_writer, :[]= alias_method :[]=, :indifferent_writer %w(default update fetch delete key? values_at).each do |m| alias_method "regular_#{m}", m alias_method m, "indifferent_#{m}" end %w(include? member? has_key?).each do |key_alias| alias_method key_alias, :indifferent_key? end end end
inject(hash)
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 24 def self.inject(hash) inject!(hash.dup) end
inject!(hash)
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 19 def self.inject!(hash) (class << hash; self; end).send :include, IndifferentAccess hash.convert! end
Public Instance Methods
convert!()
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 32 def convert! keys.each do |k| regular_writer convert_key(k), convert_value(self.regular_delete(k)) end self end
convert_key(key)
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 28 def convert_key(key) key.to_s end
convert_value(value)
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 39 def convert_value(value) if hash_lacking_indifference?(value) IndifferentAccess.inject(value.dup) elsif value.is_a?(::Array) value.dup.replace(value.map { |e| convert_value(e) }) else value end end
indifferent_access?()
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 67 def indifferent_access?; true end
indifferent_default(key = nil)
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 49 def indifferent_default(key = nil) return self[convert_key(key)] if key?(key) regular_default(key) end
indifferent_delete(key)
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 63 def indifferent_delete(key); regular_delete convert_key(key) end
indifferent_fetch(key, *args)
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 62 def indifferent_fetch(key, *args); regular_fetch convert_key(key), *args end
indifferent_key?(key)
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 64 def indifferent_key?(key); regular_key? convert_key(key) end
indifferent_update(other_hash)
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 54 def indifferent_update(other_hash) return regular_update(other_hash) if hash_with_indifference?(other_hash) other_hash.each_pair do |k,v| self[k] = v end end
indifferent_values_at(*indices)
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 65 def indifferent_values_at(*indices); indices.map{|i| self[i] } end
indifferent_writer(key, value)
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 61 def indifferent_writer(key, value); regular_writer convert_key(key), convert_value(value) end
Protected Instance Methods
hash_lacking_indifference?(other)
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 71 def hash_lacking_indifference?(other) other.is_a?(::Hash) && !(other.respond_to?(:indifferent_access?) && other.indifferent_access?) end
hash_with_indifference?(other)
click to toggle source
# File lib/midpay/hash/indifferent_access.rb, line 77 def hash_with_indifference?(other) other.is_a?(::Hash) && other.respond_to?(:indifferent_access?) && other.indifferent_access? end