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
|
# File 'lib/stipulate.rb', line 9
def stipulate(opts={})
method = opts[:that].to_s
enumeration = method.pluralize
singleton_class.class_eval <<-"END"
cattr_accessor :#{enumeration}
END
if instance_methods.include? method.to_sym
class_eval <<-"END"
# Ensure fields are converted to string
def #{method}_with_stipulation
v = #{method}_without_stipulation
v.nil? ? nil : v.to_s
end
END
alias_method_chain method.to_sym, :stipulation
else
class_eval <<-"END"
# Ensure fields are converted to string
def #{method}
v = attributes["#{method}"]
v.nil? ? nil : v.to_s
end
END
end
singleton_class.send :"#{enumeration}=", opts[:can_be]
for enum in opts[:can_be] do
class_eval <<-"END"
def #{enum}?
#{method}.to_s.to_sym == :#{enum}
end
END
end
end
|