Class: HashObject
- Inherits:
-
Object
show all
- Defined in:
- lib/hash_object.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash = {}) ⇒ HashObject
Returns a new instance of HashObject.
4
5
6
|
# File 'lib/hash_object.rb', line 4
def initialize(hash = {})
@hash = HashWithIndifferentAccess.new(hash)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/hash_object.rb', line 27
def method_missing(method_id, *args)
key = method_id.id2name.to_s
if key[-1, 1] == '='
key.slice!(-1, 1)
val = args.shift
@hash[key] = val
else
@hash[key]
end
end
|
Instance Attribute Details
#hash ⇒ Object
Returns the value of attribute hash.
2
3
4
|
# File 'lib/hash_object.rb', line 2
def hash
@hash
end
|
Instance Method Details
#[](key) ⇒ Object
13
|
# File 'lib/hash_object.rb', line 13
def [](key) ; @hash[key.to_s] ; end
|
#[]=(key, val) ⇒ Object
14
|
# File 'lib/hash_object.rb', line 14
def []=(key, val) ; @hash[key.to_s] = val ; end
|
#delete(key) ⇒ Object
16
|
# File 'lib/hash_object.rb', line 16
def delete(key) ; @hash.delete(key) ; end
|
#each(&block) ⇒ Object
23
24
25
|
# File 'lib/hash_object.rb', line 23
def each(&block)
@hash.each { |k,v| yield(k,v) }
end
|
#empty? ⇒ Boolean
8
|
# File 'lib/hash_object.rb', line 8
def empty? ; @hash.keys.empty? ; end
|
#id ⇒ Object
10
|
# File 'lib/hash_object.rb', line 10
def id ; @hash['id'] ; end
|
#id=(val) ⇒ Object
11
|
# File 'lib/hash_object.rb', line 11
def id=(val) ; @hash['id'] = val ; end
|
#rename_key(key, new_key) ⇒ Object
18
19
20
21
|
# File 'lib/hash_object.rb', line 18
def rename_key(key, new_key)
@hash[new_key.to_s] = @hash[key]
@hash.delete(key)
end
|