6
7
8
9
10
11
12
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
|
# File 'lib/itrigga/config/macros.rb', line 6
def self.included(klass)
klass.send(:include, InstanceMethods)
define_method( :params ) do
klass.instance_variables.map{ |v| v.gsub(/@(.+)/, '\1') } - ['inheritable_attributes']
end
define_method(:param) do |arg|
if klass.instance_variables.include? "@#{arg}"
klass.instance_variable_get("@#{arg}")
else
raise ArgumentError.new("no param found called '#{arg}'")
end
end
klass.class_eval {
class << self
def param_getter( name, default_value=nil, &block )
instance_variable_set( "@#{name}", default_value )
define_method( name.to_s ) do
if instance_variables.include? "@#{name}"
instance_variable_get("@#{name}")
else
if block
instance_variable_set("@#{name}", block.call(self) )
else
instance_variable_set("@#{name}", default_value)
end
end
end
end
def param_setter( name )
define_method(:recursive_update) do |p, h|
h.each do |k, v|
prev = p[k.to_sym]
if prev.is_a?(Hash) && v.is_a?(Hash)
recursive_update(prev, v)
else
p[k.to_sym]=v
end
end
end
define_method("#{name}=") do |val|
if val.is_a?(Hash) && self.send("#{name}".to_sym).is_a?(Hash)
recursive_update(self.send("#{name}".to_sym), val)
else
instance_variable_set("@#{name}", val)
end
end
end
def def_param( name, default_value, &block )
param_getter( name, default_value, &block )
param_setter( name )
end
end
}
end
|