simple_enum 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +7 -0
- data/Gemfile.BACKPORT +7 -0
- data/Gemfile.BACKPORT.lock +16 -0
- data/Gemfile.lock +28 -0
- data/lib/simple_enum.rb +14 -19
- data/test/array_conversions_test.rb +2 -1
- data/test/class_methods_test.rb +15 -17
- data/test/models.rb +6 -1
- data/test/test_helper.rb +2 -0
- metadata +8 -4
data/Gemfile
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
source :rubygems
|
2
|
+
|
3
|
+
gem "activesupport", "~> 3.0.0", :require => "active_support"
|
4
|
+
gem "activerecord", "~> 3.0.0", :require => "active_record"
|
5
|
+
|
6
|
+
gem "sqlite3-ruby", :require => "sqlite3", :platforms => :ruby
|
7
|
+
gem "jdbc-sqlite3", :require => "jdbcsqlite3", :platforms => :jruby
|
data/Gemfile.BACKPORT
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
source :rubygems
|
2
|
+
|
3
|
+
gem "activesupport", "~> 2.3.8", :require => "active_support"
|
4
|
+
gem "activerecord", "~> 2.3.8", :require => "active_record"
|
5
|
+
|
6
|
+
gem "sqlite3-ruby", :require => "sqlite3", :platforms => :ruby
|
7
|
+
gem "jdbc-sqlite3", :require => "jdbcsqlite3", :platforms => :jruby
|
@@ -0,0 +1,16 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activerecord (2.3.10)
|
5
|
+
activesupport (= 2.3.10)
|
6
|
+
activesupport (2.3.10)
|
7
|
+
sqlite3-ruby (1.3.2)
|
8
|
+
|
9
|
+
PLATFORMS
|
10
|
+
ruby
|
11
|
+
|
12
|
+
DEPENDENCIES
|
13
|
+
activerecord (~> 2.3.8)
|
14
|
+
activesupport (~> 2.3.8)
|
15
|
+
jdbc-sqlite3
|
16
|
+
sqlite3-ruby
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.0.1)
|
5
|
+
activesupport (= 3.0.1)
|
6
|
+
builder (~> 2.1.2)
|
7
|
+
i18n (~> 0.4.1)
|
8
|
+
activerecord (3.0.1)
|
9
|
+
activemodel (= 3.0.1)
|
10
|
+
activesupport (= 3.0.1)
|
11
|
+
arel (~> 1.0.0)
|
12
|
+
tzinfo (~> 0.3.23)
|
13
|
+
activesupport (3.0.1)
|
14
|
+
arel (1.0.1)
|
15
|
+
activesupport (~> 3.0.0)
|
16
|
+
builder (2.1.2)
|
17
|
+
i18n (0.4.2)
|
18
|
+
sqlite3-ruby (1.3.2)
|
19
|
+
tzinfo (0.3.23)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
activerecord (~> 3.0.0)
|
26
|
+
activesupport (~> 3.0.0)
|
27
|
+
jdbc-sqlite3
|
28
|
+
sqlite3-ruby
|
data/lib/simple_enum.rb
CHANGED
@@ -21,7 +21,7 @@ require 'simple_enum/validation'
|
|
21
21
|
module SimpleEnum
|
22
22
|
|
23
23
|
# +SimpleEnum+ version string.
|
24
|
-
VERSION = "1.
|
24
|
+
VERSION = "1.3.0".freeze
|
25
25
|
|
26
26
|
class << self
|
27
27
|
|
@@ -176,24 +176,23 @@ module SimpleEnum
|
|
176
176
|
end
|
177
177
|
|
178
178
|
# allow access to defined values hash, e.g. in a select helper or finder method.
|
179
|
-
|
180
|
-
|
179
|
+
attr_name = enum_cd.to_s.pluralize
|
180
|
+
enum_attr = :"#{attr_name.downcase}_enum_hash"
|
181
|
+
write_inheritable_attribute(enum_attr, values)
|
181
182
|
|
182
|
-
class_eval(<<-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
return @#{self_name}[args.first] if args.size == 1
|
188
|
-
args.inject([]) { |ary, sym| ary << @#{self_name}[sym]; ary }
|
183
|
+
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
184
|
+
def self.#{attr_name}(*args)
|
185
|
+
return read_inheritable_attribute(#{enum_attr.inspect}) if args.first.nil?
|
186
|
+
return read_inheritable_attribute(#{enum_attr.inspect})[args.first] if args.size == 1
|
187
|
+
args.inject([]) { |ary, sym| ary << read_inheritable_attribute(#{enum_attr.inspect})[sym]; ary }
|
189
188
|
end
|
190
189
|
|
191
|
-
def self.#{
|
192
|
-
self.#{
|
193
|
-
[block_given? ? yield(k,v) : self.human_enum_name(#{
|
190
|
+
def self.#{attr_name}_for_select(&block)
|
191
|
+
self.#{attr_name}.map do |k,v|
|
192
|
+
[block_given? ? yield(k,v) : self.human_enum_name(#{attr_name.inspect}, k), k]
|
194
193
|
end.sort
|
195
194
|
end
|
196
|
-
|
195
|
+
RUBY
|
197
196
|
|
198
197
|
# only create if :slim is not defined
|
199
198
|
if options[:slim] != true
|
@@ -214,11 +213,7 @@ module SimpleEnum
|
|
214
213
|
|
215
214
|
# allow class access to each value
|
216
215
|
unless options[:slim] === :class
|
217
|
-
|
218
|
-
singleton_class.send(:define_method, "#{prefix}#{sym}", Proc.new { |*args| args.first ? k : code })
|
219
|
-
else
|
220
|
-
metaclass.send(:define_method, "#{prefix}#{sym}", Proc.new { |*args| args.first ? k : code })
|
221
|
-
end
|
216
|
+
singleton_class.send(:define_method, "#{prefix}#{sym}", Proc.new { |*args| args.first ? k : code })
|
222
217
|
end
|
223
218
|
end
|
224
219
|
end
|
@@ -6,10 +6,11 @@ class ArrayConversionsTest < ActiveSupport::TestCase
|
|
6
6
|
end
|
7
7
|
|
8
8
|
test "that conversion of Gender.find(:all).map {...} to enumeration values as symbols works the same as [:male,:female]" do
|
9
|
-
|
9
|
+
class DummyArrayTest1 < ActiveRecord::Base
|
10
10
|
set_table_name 'dummies'
|
11
11
|
as_enum :gender, Gender.find(:all).map { |g| [g.name.to_sym, g.id] }
|
12
12
|
end
|
13
|
+
with_enum = DummyArrayTest1
|
13
14
|
|
14
15
|
assert_equal 0, with_enum.male
|
15
16
|
assert_equal 1, with_enum.female
|
data/test/class_methods_test.rb
CHANGED
@@ -10,9 +10,14 @@ class ClassMethodsTest < ActiveSupport::TestCase
|
|
10
10
|
assert_equal Dummy.genders(:male), Dummy.genders[:male]
|
11
11
|
assert_nil Dummy.genders(:inexistent)
|
12
12
|
assert_nil Dummy.genders[:inexistent]
|
13
|
+
assert_not_nil Dummy.genders[:female]
|
14
|
+
end
|
15
|
+
|
16
|
+
test "inheritance of `genders` to subclasses (#issue/3)" do
|
17
|
+
assert_equal({ :female => 1, :male => 0}, SpecificDummy.genders)
|
13
18
|
end
|
14
19
|
|
15
|
-
test "
|
20
|
+
test "genders reader created" do
|
16
21
|
assert_equal [0, 1], Dummy.genders.values.sort
|
17
22
|
assert_equal %w{female male}, Dummy.genders.keys.map(&:to_s).sort
|
18
23
|
end
|
@@ -35,10 +40,11 @@ class ClassMethodsTest < ActiveSupport::TestCase
|
|
35
40
|
end
|
36
41
|
|
37
42
|
test "that no Klass.shortcut are created if :slim => true" do
|
38
|
-
|
43
|
+
class Dummy1 < ActiveRecord::Base
|
39
44
|
set_table_name 'dummies'
|
40
45
|
as_enum :gender, [:male, :female], :slim => true
|
41
46
|
end
|
47
|
+
with_slim = Dummy1
|
42
48
|
|
43
49
|
assert !with_slim.respond_to?(:male)
|
44
50
|
assert !with_slim.respond_to?(:female)
|
@@ -46,11 +52,12 @@ class ClassMethodsTest < ActiveSupport::TestCase
|
|
46
52
|
end
|
47
53
|
|
48
54
|
test "that no Klass.shortcut's are created if :slim => :class, though instance shortcuts are" do
|
49
|
-
|
55
|
+
class Dummy2 < ActiveRecord::Base
|
50
56
|
set_table_name 'dummies'
|
51
57
|
as_enum :gender, [:male, :female], :slim => :class
|
52
58
|
end
|
53
|
-
|
59
|
+
with_slim_class = Dummy2
|
60
|
+
|
54
61
|
jane = with_slim_class.new
|
55
62
|
|
56
63
|
assert_respond_to jane, :male!
|
@@ -63,10 +70,11 @@ class ClassMethodsTest < ActiveSupport::TestCase
|
|
63
70
|
end
|
64
71
|
|
65
72
|
test "that Klass.shortcut respect :prefix => true and are prefixed by \#{enum_cd}" do
|
66
|
-
|
73
|
+
class Dummy3 < ActiveRecord::Base
|
67
74
|
set_table_name 'dummies'
|
68
75
|
as_enum :gender, [:male, :female], :prefix => true
|
69
76
|
end
|
77
|
+
with_prefix = Dummy3
|
70
78
|
|
71
79
|
assert !with_prefix.respond_to?(:male)
|
72
80
|
assert !with_prefix.respond_to?(:female)
|
@@ -77,10 +85,11 @@ class ClassMethodsTest < ActiveSupport::TestCase
|
|
77
85
|
end
|
78
86
|
|
79
87
|
test "to ensure that Klass.shortcut also work with custom prefixes" do
|
80
|
-
|
88
|
+
class Dummy4 < ActiveRecord::Base
|
81
89
|
set_table_name 'dummies'
|
82
90
|
as_enum :gender, [:male, :female], :prefix => :g
|
83
91
|
end
|
92
|
+
with_custom_prefix = Dummy4
|
84
93
|
|
85
94
|
assert !with_custom_prefix.respond_to?(:male)
|
86
95
|
assert !with_custom_prefix.respond_to?(:female)
|
@@ -90,18 +99,7 @@ class ClassMethodsTest < ActiveSupport::TestCase
|
|
90
99
|
assert_equal 1, with_custom_prefix.g_female
|
91
100
|
assert_respond_to with_custom_prefix, :genders
|
92
101
|
end
|
93
|
-
|
94
|
-
test "new :upcase option for those guys picky with coding guidelines etc." do
|
95
|
-
with_upcase = Class.new(ActiveRecord::Base) do
|
96
|
-
set_table_name 'dummies'
|
97
|
-
as_enum :gender, [:male, :female], :upcase => true
|
98
|
-
end
|
99
102
|
|
100
|
-
assert_respond_to with_upcase, :GENDERS
|
101
|
-
assert_same 0, with_upcase.GENDERS.male
|
102
|
-
assert_same 1, with_upcase.GENDERS[:female]
|
103
|
-
end
|
104
|
-
|
105
103
|
test "that the human_enum_name method returns translated/humanized values" do
|
106
104
|
assert_equal :male.to_s.humanize, Dummy.human_enum_name(:genders, :male)
|
107
105
|
assert_equal "Girl", Dummy.human_enum_name(:genders, :female)
|
data/test/models.rb
CHANGED
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 0
|
10
|
-
version: 1.
|
10
|
+
version: 1.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Lukas Westermann
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-11-19 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -29,6 +29,10 @@ extra_rdoc_files:
|
|
29
29
|
- README.rdoc
|
30
30
|
files:
|
31
31
|
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- Gemfile.BACKPORT
|
34
|
+
- Gemfile.BACKPORT.lock
|
35
|
+
- Gemfile.lock
|
32
36
|
- LICENCE
|
33
37
|
- README.rdoc
|
34
38
|
- Rakefile
|