class Hash
Public Instance Methods
access_map(key, &block)
click to toggle source
accesses a key, and maps over that key unless blank
# File lib/functional_support/core_ext/hash.rb, line 22 def access_map(key, &block) clone.access_map! key, &block end
access_map!(*keys) { || ... }
click to toggle source
# File lib/functional_support/core_ext/hash.rb, line 26 def access_map!(*keys, &block) tap do |hash| keys.each do |key| if hash[key].present? hash[key] = yield if 0 == block.arity hash[key] = yield hash[key] if 1 == block.arity hash[key] = yield key, hash[key] if 2 == block.arity end end end end
alter_key_from(from_key)
click to toggle source
# File lib/functional_support/core_ext/hash.rb, line 73 def alter_key_from(from_key) self.clone.alter_key_from! from_key end
alter_key_from!(from_key)
click to toggle source
# File lib/functional_support/core_ext/hash.rb, line 65 def alter_key_from!(from_key) HashProc.new do |to_key| new_value = self.delete from_key self[to_key] = new_value if new_value.present? self end end
enforce!(*keys)
click to toggle source
# File lib/functional_support/core_ext/hash.rb, line 38 def enforce!(*keys) keys.reduce(self.class.new) do |hash, key| raise EnforcedKeyMissing.new("#{key} is blank") if self[key].blank? hash[key] = self[key] hash end end
initializing_merge(hash)
click to toggle source
Like standard merge, except it ignores blank? values
# File lib/functional_support/core_ext/hash.rb, line 9 def initializing_merge(hash) self.clone.tap do |cloned_hash| hash.each do |key, value| cloned_hash[key] = hash[key] if hash[key].present? end end end
key_map(&block)
click to toggle source
# File lib/functional_support/core_ext/hash.rb, line 53 def key_map(&block) clone.key_map!(&block) end
key_map!() { |key| ... }
click to toggle source
# File lib/functional_support/core_ext/hash.rb, line 57 def key_map!(&block) tap do |hash| hash.keys.each do |key| hash[yield(key)] = hash.delete key if hash.has_key? key end end end
permit(*keys)
click to toggle source
# File lib/functional_support/core_ext/hash.rb, line 46 def permit(*keys) keys.reduce(self.class.new) do |hash, key| hash[key] = self[key] if self.has_key?(key) hash end end
values_around(symbol)
click to toggle source
# File lib/functional_support/core_ext/hash.rb, line 17 def values_around(symbol) self[symbol] || self[symbol.to_s] end