5
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
|
# File 'lib/enum_attr_base.rb', line 5
def enum_attr(attr, enums)
raise "The secone param must be a array!" unless enums.is_a? Array
if self.superclass == Object
attr_accessor attr.to_sym
end
attr = attr.to_s
enums.each do |enum|
const_set("#{attr.upcase}_#{enum[2].upcase}", enum[1] )
class_eval(%Q{
def #{attr}_#{enum[2]}?
#{attr} == #{enum[1]}
end
})
end
self.class_eval(%Q{
ENUMS_#{attr.upcase} = enums.collect{|item| [item[0], item[1]]}
def self.#{attr}_name_by(arg)
ENUMS_#{attr.upcase}.find{|option| option[1] == arg }[0] rescue ""
end
def #{attr}_name
ENUMS_#{attr.upcase}.find{|option| option[1] == #{attr}}[0] rescue "" unless #{attr}.nil?
end
})
end
|