Class: Hash
- Inherits:
-
Object
show all
- Defined in:
- lib/core_extended/hash.rb
Defined Under Namespace
Classes: Inflector
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
53
54
55
56
|
# File 'lib/core_extended/hash.rb', line 53
def method_missing(method, *args, &block)
return get(method) if inflections.include?(method.to_sym)
super
end
|
Class Method Details
.inflect(method, *args) ⇒ Object
22
23
24
|
# File 'lib/core_extended/hash.rb', line 22
def self.inflect(method, *args)
inflectors << Inflector.new(method, args)
end
|
.inflections(key) ⇒ Object
26
27
28
29
30
31
32
33
|
# File 'lib/core_extended/hash.rb', line 26
def self.inflections(key)
[key.to_s, key.to_s.to_sym].tap do |list|
inflectors.each do |i|
k = key.to_s.send i.method, *i.arguments
list.concat [k, k.to_sym]
end
end.uniq
end
|
.inflectors ⇒ Object
18
19
20
|
# File 'lib/core_extended/hash.rb', line 18
def self.inflectors
@inflectors ||= []
end
|
Instance Method Details
#dictionary(parent_key = nil) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/core_extended/hash.rb', line 5
def dictionary(parent_key=nil)
Hash.new.tap do |hash|
each do |k,v|
key = [parent_key, k].compact.join('.')
if v.is_a? Hash
hash.merge! v.dictionary(key)
else
hash[key] = v
end
end
end
end
|
#get(key) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/core_extended/hash.rb', line 39
def get(key)
indiferent_access = [key, key.to_s, key.to_s.to_sym].uniq
indiferent_access.each do |k|
return self[k] if key?(k)
end
Hash[keys.map{ |k| [k, self.class.inflections(k)] }].each do |k,v|
return self[k] if (v & indiferent_access).any?
end
nil
end
|
#inflections ⇒ Object
35
36
37
|
# File 'lib/core_extended/hash.rb', line 35
def inflections
keys.flat_map{ |k| self.class.inflections(k).map(&:to_sym) }.uniq
end
|