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
|
# File 'lib/simple_struct.rb', line 5
def initialize 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
17
18
19
20
21
22
|
# File 'lib/simple_struct.rb', line 17
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?(method_name)
super method_name, *args
end
|
Class Method Details
.create(obj) ⇒ Object
30
31
32
|
# File 'lib/simple_struct.rb', line 30
def create obj
obj.is_a?(Hash) ? SimpleStruct.new(obj) : obj
end
|
Instance Method Details
#[](key) ⇒ Object
9
10
11
|
# File 'lib/simple_struct.rb', line 9
def [](key)
@a[real_key(key)]
end
|
#[]=(key, value) ⇒ Object
13
14
15
|
# File 'lib/simple_struct.rb', line 13
def []=(key, value)
@a[real_key(key)] = value
end
|