Class: Hash
- Inherits:
-
Object
show all
- Defined in:
- lib/functional_support/core_ext/hash.rb
Defined Under Namespace
Classes: EnforcedKeyMissing, HashProc
Instance Method Summary
collapse
Instance Method Details
#access_map(key, &block) ⇒ Object
accesses a key, and maps over that key unless blank
22
23
24
|
# File 'lib/functional_support/core_ext/hash.rb', line 22
def access_map(key, &block)
clone.access_map! key, &block
end
|
#access_map!(*keys, &block) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
|
# 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) ⇒ Object
73
74
75
|
# 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) ⇒ Object
65
66
67
68
69
70
71
|
# 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) ⇒ Object
38
39
40
41
42
43
44
|
# 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) ⇒ Object
Like standard merge, except it ignores blank? values
9
10
11
12
13
14
15
|
# 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) ⇒ Object
53
54
55
|
# File 'lib/functional_support/core_ext/hash.rb', line 53
def key_map(&block)
clone.key_map!(&block)
end
|
#key_map!(&block) ⇒ Object
57
58
59
60
61
62
63
|
# 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) ⇒ Object
46
47
48
49
50
51
|
# 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) ⇒ Object
17
18
19
|
# File 'lib/functional_support/core_ext/hash.rb', line 17
def values_around(symbol)
self[symbol] || self[symbol.to_s]
end
|