13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/ussd_engine/controller/storable.rb', line 13
def stores(field, accessor = nil, &block)
instance_prop = "@#{field}" key_method_name = "#{field}_key" getter_method_name = field setter_method_name = "#{field}="
define_method key_method_name do
File.join ussd_request_id, field.to_s
end
define_method getter_method_name do
session[send(key_method_name)]
end
define_method setter_method_name do |value|
session[send(key_method_name)] = value
end
if accessor.present?
raise "A block is required if you pass an accessor" unless block_given?
cache_method_name = "#{accessor}_cache" cache_instance_prop = "@#{cache_method_name}"
define_method cache_method_name do
unless instance_variable_defined? cache_instance_prop
instance_variable_set cache_instance_prop, {}
end
instance_variable_get cache_instance_prop
end
define_method accessor do
field_value = send(getter_method_name)
return if field_value.blank?
send(cache_method_name)[field_value] ||= instance_exec(field_value, &block)
end
end
end
|