acts_as_enumeration 0.1.11 → 0.1.12
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 +21 -7
- data/test/enumeration_test.rb +3 -0
- metadata +2 -2
@@ -2,7 +2,7 @@ module ActiveRecord
|
|
2
2
|
module Acts
|
3
3
|
module Enumeration
|
4
4
|
|
5
|
-
VERSION="0.1.
|
5
|
+
VERSION="0.1.12"
|
6
6
|
class << self
|
7
7
|
|
8
8
|
def included(base)
|
@@ -64,8 +64,22 @@ module ActiveRecord
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
+
def _key(string)
|
68
|
+
((string.to_s=~/^[a-z_]/) && !respond_to?(string.to_s)) ? string.to_s : "_#{string.to_s}"
|
69
|
+
end
|
70
|
+
|
71
|
+
def _camelized_key(string)
|
72
|
+
(((string.to_s=~/^[a-z_]/) && !const_defined?(string.to_s.camelize)) ? string.to_s : "_#{string.to_s}").camelize
|
73
|
+
end
|
74
|
+
|
75
|
+
def _camelized_upcase_key(string)
|
76
|
+
(((string.to_s=~/^[a-z_]/) && !const_defined?(string.to_s.camelize.upcase)) ? string.to_s : "_#{string.to_s}").camelize.upcase
|
77
|
+
end
|
78
|
+
|
67
79
|
portable_select(field).map { |x| normalize_intern(x.send(field)) }.each do |y|
|
68
|
-
key
|
80
|
+
key=_key(y)
|
81
|
+
camelized_key=_camelized_key(y)
|
82
|
+
camelized_upcase_key=_camelized_upcase_key(y)
|
69
83
|
define_method(:as_key) { self.class.normalize_intern(send(field)) }
|
70
84
|
define_method("is_#{y}?") { is?(y) }
|
71
85
|
alias_method "#{key}?", "is_#{y}?"
|
@@ -73,14 +87,14 @@ module ActiveRecord
|
|
73
87
|
self;
|
74
88
|
end.instance_eval do
|
75
89
|
define_method(key) { self.send("for_#{field}", y) }
|
76
|
-
define_method(
|
77
|
-
define_method(
|
90
|
+
define_method(camelized_key) { self.send("id_for_#{field}", y)}
|
91
|
+
define_method(camelized_upcase_key) { self.send("id_for_#{field}", y)}
|
78
92
|
end
|
79
93
|
begin
|
80
|
-
self.const_set(
|
81
|
-
self.const_set(
|
94
|
+
self.const_set(camelized_key,self.send("id_for_#{field}",y))
|
95
|
+
self.const_set(camelized_upcase_key,self.send("id_for_#{field}",y))
|
82
96
|
rescue Exception=>e
|
83
|
-
puts("Warning: Skipping constant definition for #{
|
97
|
+
puts("Warning: Skipping constant definition for #{y}")
|
84
98
|
end
|
85
99
|
end
|
86
100
|
end
|
data/test/enumeration_test.rb
CHANGED
@@ -52,6 +52,7 @@ end)
|
|
52
52
|
end
|
53
53
|
[BrokenEnumeration].each do |klass|
|
54
54
|
klass.create! :name => '33108', :description => '33108 field'
|
55
|
+
klass.create! :name => 'all', :description => 'already defined method'
|
55
56
|
end
|
56
57
|
EnumerateAll.acts_as_enumeration :description
|
57
58
|
EnumerateSome.acts_as_enumeration :description
|
@@ -87,6 +88,8 @@ end)
|
|
87
88
|
assert EnumerateAll.FIRSTFIELD,Enumerate.first.id
|
88
89
|
assert EnumerateAll.FirstField,Enumerate.first.id
|
89
90
|
assert BrokenEnumeration._33108,BrokenEnumeration.first.id
|
91
|
+
assert BrokenEnumeration._all.id,BrokenEnumeration.last.id
|
92
|
+
assert BrokenEnumeration::ALL,Enumerate.last.id
|
90
93
|
end
|
91
94
|
|
92
95
|
def test_sti
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_enumeration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.12
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-11-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|