Class: SimpleStruct
- Inherits:
-
Object
show all
- Defined in:
- lib/simple_struct.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of SimpleStruct.
5
6
7
8
|
# File 'lib/simple_struct.rb', line 5
def initialize hash
@origin_hash = hash
@a = hash.inject({}){ |res, (key, value)| res[key] = SimpleStruct.create(value); res }
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
22
23
24
25
26
27
|
# File 'lib/simple_struct.rb', line 22
def method_missing method_name, *args
return self[method_name.to_s.delete("=")] = args.first if method_name[-1] == "="
return self[method_name] if @a.has_key?(real_key(method_name))
super method_name, *args
end
|
Class Method Details
.create(obj) ⇒ Object
35
36
37
|
# File 'lib/simple_struct.rb', line 35
def create obj
obj.is_a?(Hash) ? SimpleStruct.new(obj) : obj
end
|
Instance Method Details
#[](key) ⇒ Object
10
11
12
|
# File 'lib/simple_struct.rb', line 10
def [](key)
@a[real_key(key)]
end
|
#[]=(key, value) ⇒ Object
14
15
16
|
# File 'lib/simple_struct.rb', line 14
def []=(key, value)
@a[real_key(key)] = value
end
|
#to_hash ⇒ Object
18
19
20
|
# File 'lib/simple_struct.rb', line 18
def to_hash
@origin_hash
end
|