acts_as_enumeration 0.1.13 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/active_record/acts/enumeration.rb +20 -8
- data/test/enumeration_test.rb +4 -2
- metadata +1 -1
@@ -2,7 +2,7 @@ module ActiveRecord
|
|
2
2
|
module Acts
|
3
3
|
module Enumeration
|
4
4
|
|
5
|
-
VERSION="0.1.
|
5
|
+
VERSION="0.1.14"
|
6
6
|
class << self
|
7
7
|
|
8
8
|
def included(base)
|
@@ -64,22 +64,30 @@ module ActiveRecord
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
+
def allowed_chars(string)
|
68
|
+
string.to_s=~/^[a-z_]/
|
69
|
+
end
|
70
|
+
|
71
|
+
def check_string(string, &block)
|
72
|
+
allowed_chars(string) && (yield string)
|
73
|
+
end
|
74
|
+
|
67
75
|
def _key(string)
|
68
|
-
(
|
76
|
+
check_string(string) {|y| !respond_to?(y)} ? string : "_#{string}"
|
69
77
|
end
|
70
78
|
|
71
79
|
def _camelized_key(string)
|
72
|
-
(
|
80
|
+
check_string(string) {|y| !const_defined?(string.camelize)} ? string.camelize : "_#{string.camelize}"
|
73
81
|
end
|
74
82
|
|
75
83
|
def _camelized_upcase_key(string)
|
76
|
-
(
|
84
|
+
check_string(string) {|y| !const_defined?(string.camelize.upcase)} ? string.camelize.upcase : "_#{string.camelize.upcase}"
|
77
85
|
end
|
78
86
|
|
79
87
|
portable_select(field).map { |x| normalize_intern(x.send(field)) }.each do |y|
|
80
|
-
key=_key(y)
|
81
|
-
camelized_key=_camelized_key(y)
|
82
|
-
camelized_upcase_key=_camelized_upcase_key(y)
|
88
|
+
key=_key(y.to_s)
|
89
|
+
camelized_key=_camelized_key(y.to_s)
|
90
|
+
camelized_upcase_key=_camelized_upcase_key(y.to_s)
|
83
91
|
define_method(:as_key) { self.class.normalize_intern(send(field)) }
|
84
92
|
define_method("is_#{y}?") { is?(y) }
|
85
93
|
alias_method "#{key}?", "is_#{y}?"
|
@@ -92,9 +100,13 @@ module ActiveRecord
|
|
92
100
|
end
|
93
101
|
begin
|
94
102
|
self.const_set(camelized_key,self.send("id_for_#{field}",y))
|
103
|
+
rescue Exception=>e
|
104
|
+
puts("Warning: Skipping constant definition for #{camelized_key}")
|
105
|
+
end
|
106
|
+
begin
|
95
107
|
self.const_set(camelized_upcase_key,self.send("id_for_#{field}",y))
|
96
108
|
rescue Exception=>e
|
97
|
-
puts("Warning: Skipping constant definition for #{
|
109
|
+
puts("Warning: Skipping constant definition for #{camelized_upcase_key}")
|
98
110
|
end
|
99
111
|
end
|
100
112
|
end
|
data/test/enumeration_test.rb
CHANGED
@@ -88,8 +88,10 @@ end)
|
|
88
88
|
assert EnumerateAll.FIRSTFIELD,Enumerate.first.id
|
89
89
|
assert EnumerateAll.FirstField,Enumerate.first.id
|
90
90
|
assert BrokenEnumeration._33108,BrokenEnumeration.first.id
|
91
|
-
assert BrokenEnumeration._all
|
92
|
-
assert BrokenEnumeration::
|
91
|
+
assert BrokenEnumeration._all,BrokenEnumeration.find_by_name('all')
|
92
|
+
assert BrokenEnumeration::All,BrokenEnumeration.find_by_name('all').id
|
93
|
+
assert BrokenEnumeration::ALL,BrokenEnumeration.find_by_name('all').id
|
94
|
+
assert BrokenEnumeration::ALL,BrokenEnumeration.id_for_name(:all)
|
93
95
|
end
|
94
96
|
|
95
97
|
def test_sti
|