Class: Hashugar
- Inherits:
-
Object
show all
- Defined in:
- lib/hashugar.rb,
lib/hashugar/version.rb
Constant Summary
collapse
- VERSION =
"1.0.1"
Instance Method Summary
collapse
Constructor Details
#initialize(hash) ⇒ Hashugar
Returns a new instance of Hashugar.
4
5
6
7
8
9
10
11
12
|
# File 'lib/hashugar.rb', line 4
def initialize(hash)
@table = {}
@table_with_original_keys = {}
hash.each_pair do |key, value|
hashugar = value.to_hashugar
@table_with_original_keys[key] = hashugar
@table[stringify(key)] = hashugar
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
14
15
16
17
18
19
20
21
|
# File 'lib/hashugar.rb', line 14
def method_missing(method, *args, &block)
method = method.to_s
if method.chomp!('=')
@table[method] = args.first
else
@table[method]
end
end
|
Instance Method Details
23
24
25
|
# File 'lib/hashugar.rb', line 23
def [](key)
@table[stringify(key)]
end
|
#[]=(key, value) ⇒ Object
27
28
29
|
# File 'lib/hashugar.rb', line 27
def []=(key, value)
@table[stringify(key)] = value
end
|
#each(&block) ⇒ Object
35
36
37
|
# File 'lib/hashugar.rb', line 35
def each(&block)
@table_with_original_keys.each(&block)
end
|
#empty? ⇒ Boolean
46
47
48
|
# File 'lib/hashugar.rb', line 46
def empty?
@table.empty?
end
|
#respond_to?(key, include_all = false) ⇒ Boolean
31
32
33
|
# File 'lib/hashugar.rb', line 31
def respond_to?(key, include_all=false)
super(key) || @table.has_key?(stringify(key))
end
|
39
40
41
42
43
44
|
# File 'lib/hashugar.rb', line 39
def to_hash
hash = @table_with_original_keys.to_hash
hash.each do |key, value|
hash[key] = value.to_hash if value.is_a?(Hashugar)
end
end
|