Module: Awestruct::AStructMixin
- Included in:
- AStruct
- Defined in:
- lib/awestruct/astruct_mixin.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &blk) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/awestruct/astruct_mixin.rb', line 27
def method_missing(sym, *args, &blk)
type = sym.to_s[-1,1]
name = sym.to_s.gsub(/[=!?]$/, '').to_sym
case type
when '='
self[name] = args.first
when '!'
__send__(name, *args, &blk)
when '?'
self[name]
else
if key?(name)
self[name]
elsif @cascade_for_nils
self[name] = AStruct.new.cascade_for_nils!
self[name]
else
nil
end
end
end
|
Class Method Details
.extended(o) ⇒ Object
6
7
8
9
10
11
|
# File 'lib/awestruct/astruct_mixin.rb', line 6
def self.extended(o)
class << o
alias_method :original_entries, :entries
undef entries
end
end
|
Instance Method Details
#[](key) ⇒ Object
22
23
24
25
|
# File 'lib/awestruct/astruct_mixin.rb', line 22
def [](key)
r = super( key ) || super( key.to_sym ) || super( key.to_s )
transform_entry( r )
end
|
#cascade_for_nils! ⇒ Object
13
14
15
16
|
# File 'lib/awestruct/astruct_mixin.rb', line 13
def cascade_for_nils!
@cascade_for_nils = true
self
end
|
#inspect ⇒ Object
65
66
67
|
# File 'lib/awestruct/astruct_mixin.rb', line 65
def inspect
"AStruct{...}"
end
|
#key?(key) ⇒ Boolean
18
19
20
|
# File 'lib/awestruct/astruct_mixin.rb', line 18
def key?(key)
super( key ) || super( key.to_sym ) || super( key.to_s )
end
|
#transform_entry(entry) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/awestruct/astruct_mixin.rb', line 50
def transform_entry(entry)
r = case(entry)
when AStructMixin
entry
when Hash
entry.extend( AStructMixin )
when Array
entry.map!{|i| transform_entry(i)}
else
entry
end
r
end
|