Class: HashWithQuickAccess
- Inherits:
-
Object
- Object
- HashWithQuickAccess
show all
- Defined in:
- lib/hash_with_quick_access.rb
Defined Under Namespace
Modules: ConvenienceMethod
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of HashWithQuickAccess.
6
7
8
|
# File 'lib/hash_with_quick_access.rb', line 6
def initialize(hash)
@hash = hash
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key, *args, &block) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/hash_with_quick_access.rb', line 10
def method_missing(key, *args, &block)
value = if key?(key)
fetch_possibly_decorated_value(key)
elsif hash.respond_to?(key)
delegate_and_decorate(key, *args, &block)
else
fail KeyError, "key :#{key} was not found"
end
if !hash.respond_to?(key)
define_singleton_method(key) { value }
end
value
end
|
Class Method Details
.add_convenience_method_to_hash! ⇒ Object
2
3
4
|
# File 'lib/hash_with_quick_access.rb', line 2
def self.add_convenience_method_to_hash!
Hash.send(:include, ConvenienceMethod)
end
|
Instance Method Details
#[](key) ⇒ Object
34
35
36
|
# File 'lib/hash_with_quick_access.rb', line 34
def [](key)
send(key)
end
|
#keys ⇒ Object
30
31
32
|
# File 'lib/hash_with_quick_access.rb', line 30
def keys
hash.keys.map(&:to_sym)
end
|
#respond_to?(method_name, include_private = false) ⇒ Boolean
26
27
28
|
# File 'lib/hash_with_quick_access.rb', line 26
def respond_to?(method_name, include_private = false)
key?(method_name) || super
end
|