16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/model_pack/class_methods.rb', line 16
def attribute_reader(name, default: nil, as: nil, serialize: nil, predicate: nil)
dv = default.dup rescue default
define_method name do
instance_variable_defined?("@#{name}") ?
instance_variable_get("@#{name}") : instance_variable_set("@#{name}", (as ? as.new : (dv.is_a?(Proc) ? dv.call : dv )))
end
define_method "#{name}?" do
value = (instance_variable_defined?("@#{name}") ?
instance_variable_get("@#{name}") : instance_variable_set("@#{name}", (as ? as.new : (dv.is_a?(Proc) ? dv.call : dv ))))
predicate.is_a?(Proc) ? predicate.call(value) : !!value
end if predicate
define_method "#{name}_hash" do
instance_exec(send(name), &serialize)
end if serialize
end
|