simple_enum 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/LICENCE +20 -0
- data/README.rdoc +182 -0
- data/Rakefile +70 -0
- data/VERSION.yml +4 -0
- data/init.rb +1 -0
- data/lib/simple_enum/array_support.rb +15 -0
- data/lib/simple_enum/enum_hash.rb +36 -0
- data/lib/simple_enum/object_support.rb +37 -0
- data/lib/simple_enum/validation.rb +37 -0
- data/lib/simple_enum/version.rb +14 -0
- data/lib/simple_enum.rb +227 -0
- data/locales/en.yml +6 -0
- data/simple_enum.gemspec +74 -0
- data/test/array_conversions_test.rb +22 -0
- data/test/class_methods_test.rb +94 -0
- data/test/enum_hash_test.rb +33 -0
- data/test/finders_test.rb +41 -0
- data/test/models.rb +8 -0
- data/test/object_backed_test.rb +67 -0
- data/test/object_support_test.rb +27 -0
- data/test/prefixes_test.rb +38 -0
- data/test/se_array_support_test.rb +32 -0
- data/test/simple_enum_test.rb +97 -0
- data/test/test_helper.rb +66 -0
- data/test/without_shortcuts_test.rb +40 -0
- metadata +91 -0
data/simple_enum.gemspec
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{simple_enum}
|
8
|
+
s.version = "0.3.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Lukas Westermann"]
|
12
|
+
s.date = %q{2009-10-07}
|
13
|
+
s.email = %q{lukas.westermann@gmail.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"README.rdoc"
|
16
|
+
]
|
17
|
+
s.files = [
|
18
|
+
".gitignore",
|
19
|
+
"LICENCE",
|
20
|
+
"README.rdoc",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION.yml",
|
23
|
+
"init.rb",
|
24
|
+
"lib/simple_enum.rb",
|
25
|
+
"lib/simple_enum/array_support.rb",
|
26
|
+
"lib/simple_enum/enum_hash.rb",
|
27
|
+
"lib/simple_enum/object_support.rb",
|
28
|
+
"lib/simple_enum/validation.rb",
|
29
|
+
"lib/simple_enum/version.rb",
|
30
|
+
"locales/en.yml",
|
31
|
+
"simple_enum.gemspec",
|
32
|
+
"test/array_conversions_test.rb",
|
33
|
+
"test/class_methods_test.rb",
|
34
|
+
"test/enum_hash_test.rb",
|
35
|
+
"test/finders_test.rb",
|
36
|
+
"test/models.rb",
|
37
|
+
"test/object_backed_test.rb",
|
38
|
+
"test/object_support_test.rb",
|
39
|
+
"test/prefixes_test.rb",
|
40
|
+
"test/se_array_support_test.rb",
|
41
|
+
"test/simple_enum_test.rb",
|
42
|
+
"test/test_helper.rb",
|
43
|
+
"test/without_shortcuts_test.rb"
|
44
|
+
]
|
45
|
+
s.homepage = %q{http://github.com/lwe/simple_enum}
|
46
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
47
|
+
s.require_paths = ["lib"]
|
48
|
+
s.rubygems_version = %q{1.3.5}
|
49
|
+
s.summary = %q{Simple enum-like field support for ActiveRecord (including validations and i18n)}
|
50
|
+
s.test_files = [
|
51
|
+
"test/array_conversions_test.rb",
|
52
|
+
"test/class_methods_test.rb",
|
53
|
+
"test/enum_hash_test.rb",
|
54
|
+
"test/finders_test.rb",
|
55
|
+
"test/models.rb",
|
56
|
+
"test/object_backed_test.rb",
|
57
|
+
"test/object_support_test.rb",
|
58
|
+
"test/prefixes_test.rb",
|
59
|
+
"test/se_array_support_test.rb",
|
60
|
+
"test/simple_enum_test.rb",
|
61
|
+
"test/test_helper.rb",
|
62
|
+
"test/without_shortcuts_test.rb"
|
63
|
+
]
|
64
|
+
|
65
|
+
if s.respond_to? :specification_version then
|
66
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
67
|
+
s.specification_version = 3
|
68
|
+
|
69
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
70
|
+
else
|
71
|
+
end
|
72
|
+
else
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ArrayConversionsTest < ActiveSupport::TestCase
|
4
|
+
def setup
|
5
|
+
reload_db :genders => true
|
6
|
+
end
|
7
|
+
|
8
|
+
test "that conversion of Gender.find(:all).map {...} to enumeration values as symbols works the same as [:male,:female]" do
|
9
|
+
with_enum = Class.new(ActiveRecord::Base) do
|
10
|
+
set_table_name 'dummies'
|
11
|
+
as_enum :gender, Gender.find(:all).map { |g| [g.name.to_sym, g.id] }
|
12
|
+
end
|
13
|
+
|
14
|
+
assert_equal 0, with_enum.male
|
15
|
+
assert_equal 1, with_enum.female
|
16
|
+
assert_equal 1, with_enum.genders(:female)
|
17
|
+
|
18
|
+
jane = with_enum.new :gender => :female
|
19
|
+
assert_equal :female, jane.gender
|
20
|
+
assert_equal 1, jane.gender_cd
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ClassMethodsTest < ActiveSupport::TestCase
|
4
|
+
def setup
|
5
|
+
reload_db
|
6
|
+
end
|
7
|
+
|
8
|
+
test "that Klass.genders[:sym] == Klass.genders(:sym)" do
|
9
|
+
assert_equal 0, Dummy.genders(:male)
|
10
|
+
assert_equal Dummy.genders(:male), Dummy.genders[:male]
|
11
|
+
assert_nil Dummy.genders(:inexistent)
|
12
|
+
assert_nil Dummy.genders[:inexistent]
|
13
|
+
end
|
14
|
+
|
15
|
+
test "generation of value shortcuts on class" do
|
16
|
+
g = Dummy.new
|
17
|
+
|
18
|
+
assert_equal 0, Dummy.male
|
19
|
+
assert_equal 1, Dummy.female
|
20
|
+
assert_equal 'alpha', Dummy.alpha
|
21
|
+
assert_respond_to Dummy, :male
|
22
|
+
assert_respond_to Dummy, :female
|
23
|
+
assert_respond_to Dummy, :beta
|
24
|
+
assert_respond_to Dummy, :foobar
|
25
|
+
end
|
26
|
+
|
27
|
+
test "that no Klass.shortcut are created if :slim => true" do
|
28
|
+
with_slim = Class.new(ActiveRecord::Base) do
|
29
|
+
set_table_name 'dummies'
|
30
|
+
as_enum :gender, [:male, :female], :slim => true
|
31
|
+
end
|
32
|
+
|
33
|
+
assert !with_slim.respond_to?(:male)
|
34
|
+
assert !with_slim.respond_to?(:female)
|
35
|
+
assert_respond_to with_slim, :genders
|
36
|
+
end
|
37
|
+
|
38
|
+
test "that no Klass.shortcut's are created if :slim => :class, though instance shortcuts are" do
|
39
|
+
with_slim_class = Class.new(ActiveRecord::Base) do
|
40
|
+
set_table_name 'dummies'
|
41
|
+
as_enum :gender, [:male, :female], :slim => :class
|
42
|
+
end
|
43
|
+
|
44
|
+
jane = with_slim_class.new
|
45
|
+
|
46
|
+
assert_respond_to jane, :male!
|
47
|
+
assert_respond_to jane, :female!
|
48
|
+
assert !with_slim_class.respond_to?(:male)
|
49
|
+
assert !with_slim_class.respond_to?(:female)
|
50
|
+
assert_respond_to with_slim_class, :genders
|
51
|
+
assert_same 0, with_slim_class.genders.male
|
52
|
+
assert_same 1, with_slim_class.genders[:female]
|
53
|
+
end
|
54
|
+
|
55
|
+
test "that Klass.shortcut respect :prefix => true and are prefixed by \#{enum_cd}" do
|
56
|
+
with_prefix = Class.new(ActiveRecord::Base) do
|
57
|
+
set_table_name 'dummies'
|
58
|
+
as_enum :gender, [:male, :female], :prefix => true
|
59
|
+
end
|
60
|
+
|
61
|
+
assert !with_prefix.respond_to?(:male)
|
62
|
+
assert !with_prefix.respond_to?(:female)
|
63
|
+
assert_respond_to with_prefix, :gender_male
|
64
|
+
assert_respond_to with_prefix, :gender_female
|
65
|
+
assert_equal 0, with_prefix.gender_male
|
66
|
+
assert_respond_to with_prefix, :genders
|
67
|
+
end
|
68
|
+
|
69
|
+
test "to ensure that Klass.shortcut also work with custom prefixes" do
|
70
|
+
with_custom_prefix = Class.new(ActiveRecord::Base) do
|
71
|
+
set_table_name 'dummies'
|
72
|
+
as_enum :gender, [:male, :female], :prefix => :g
|
73
|
+
end
|
74
|
+
|
75
|
+
assert !with_custom_prefix.respond_to?(:male)
|
76
|
+
assert !with_custom_prefix.respond_to?(:female)
|
77
|
+
assert !with_custom_prefix.respond_to?(:gender_female)
|
78
|
+
assert_respond_to with_custom_prefix, :g_male
|
79
|
+
assert_respond_to with_custom_prefix, :g_female
|
80
|
+
assert_equal 1, with_custom_prefix.g_female
|
81
|
+
assert_respond_to with_custom_prefix, :genders
|
82
|
+
end
|
83
|
+
|
84
|
+
test "new :upcase option for those guys picky with coding guidelines etc." do
|
85
|
+
with_upcase = Class.new(ActiveRecord::Base) do
|
86
|
+
set_table_name 'dummies'
|
87
|
+
as_enum :gender, [:male, :female], :upcase => true
|
88
|
+
end
|
89
|
+
|
90
|
+
assert_respond_to with_upcase, :GENDERS
|
91
|
+
assert_same 0, with_upcase.GENDERS.male
|
92
|
+
assert_same 1, with_upcase.GENDERS[:female]
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'simple_enum/enum_hash'
|
3
|
+
|
4
|
+
class EnumHashTest < ActiveSupport::TestCase
|
5
|
+
|
6
|
+
test "create new EnumHash instance from array of symbols" do
|
7
|
+
genders = SimpleEnum::EnumHash.new [:male, :female]
|
8
|
+
|
9
|
+
assert_same 0, genders[:male]
|
10
|
+
assert_same 1, genders[:female]
|
11
|
+
assert_same 0, genders.male
|
12
|
+
assert_same :female, genders.female(true)
|
13
|
+
end
|
14
|
+
|
15
|
+
test "create new EnumHash instance from Hash" do
|
16
|
+
status = SimpleEnum::EnumHash.new :inactive => 0, :active => 1, :archived => 99
|
17
|
+
|
18
|
+
assert_same 0, status.inactive
|
19
|
+
assert_same 1, status[:active]
|
20
|
+
end
|
21
|
+
|
22
|
+
test "create new EnumHash instance from ActiveRecord results" do
|
23
|
+
reload_db :genders => true
|
24
|
+
genders = SimpleEnum::EnumHash.new Gender.find(:all)
|
25
|
+
|
26
|
+
male = Gender.find(0)
|
27
|
+
|
28
|
+
assert_same 0, genders[male]
|
29
|
+
assert_same genders[male], genders[:male]
|
30
|
+
assert_same 1, genders.female
|
31
|
+
assert_equal male, genders.send(:male, true)
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FindersTest < ActiveSupport::TestCase
|
4
|
+
def setup
|
5
|
+
reload_db
|
6
|
+
end
|
7
|
+
|
8
|
+
test "find all where :gender = :female" do
|
9
|
+
girls = Dummy.find :all, :conditions => { :gender_cd => Dummy.genders[:female] }, :order => 'name ASC'
|
10
|
+
|
11
|
+
assert_equal 2, girls.length
|
12
|
+
|
13
|
+
assert_equal 'Anna', girls.first.name
|
14
|
+
assert_equal :female, girls.first.gender
|
15
|
+
assert_equal true, girls.first.female?
|
16
|
+
end
|
17
|
+
|
18
|
+
test "find all where :word is 'gamma'" do
|
19
|
+
gammas = Dummy.find :all, :conditions => { :word_cd => Dummy.words(:gamma) }
|
20
|
+
|
21
|
+
assert_equal 1, gammas.length
|
22
|
+
assert_equal 'Chris', gammas.first.name
|
23
|
+
assert_equal true, gammas.first.male?
|
24
|
+
assert_equal 'gamma', gammas.first.word_cd
|
25
|
+
assert_equal :gamma, gammas.first.word
|
26
|
+
end
|
27
|
+
|
28
|
+
test "find with string conditions for all :didum = :foo" do
|
29
|
+
foos = Dummy.find :all, :conditions => ['other = ?', Dummy.didums(:foo)]
|
30
|
+
|
31
|
+
assert_equal 1, foos.length
|
32
|
+
assert_equal false, foos.first.foobar?
|
33
|
+
end
|
34
|
+
|
35
|
+
test "find using insecure inline string conditions" do
|
36
|
+
men = Dummy.find :all, :conditions => "gender_cd = #{Dummy.genders(:male)}"
|
37
|
+
|
38
|
+
assert_equal 1, men.length
|
39
|
+
assert_equal true, men.first.male?
|
40
|
+
end
|
41
|
+
end
|
data/test/models.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ObjectBackedTest < ActiveSupport::TestCase
|
4
|
+
def setup
|
5
|
+
reload_db :genders => true
|
6
|
+
end
|
7
|
+
|
8
|
+
test "how working with object backed columns work..." do
|
9
|
+
# simple object -> not db backed instance
|
10
|
+
simple_obj = Class.new do
|
11
|
+
attr_accessor :name
|
12
|
+
def initialize(name)
|
13
|
+
@name = name
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# create new class by using simple_obj
|
18
|
+
with_object = Class.new(ActiveRecord::Base) do
|
19
|
+
set_table_name 'dummies'
|
20
|
+
as_enum :gender, { simple_obj.new('Male') => 0, simple_obj.new('Female') => 1 }
|
21
|
+
end
|
22
|
+
|
23
|
+
d = with_object.find_by_name('Anna')
|
24
|
+
|
25
|
+
assert_same simple_obj, d.gender.class
|
26
|
+
assert_equal 'Female', d.gender.name
|
27
|
+
assert_same true, d.female?
|
28
|
+
assert_same false, d.male?
|
29
|
+
assert_same 0, with_object.male
|
30
|
+
end
|
31
|
+
|
32
|
+
test "db backed objects, using method described in 'Advanced Rails Recipes - Recipe 61: Look Up Constant Data Efficiently'" do
|
33
|
+
# "cache" as defined in ARR#61
|
34
|
+
genders = Gender.find(:all)
|
35
|
+
# works without mapping... .map { |g| [g, g.id] }
|
36
|
+
|
37
|
+
# use cached array of values
|
38
|
+
with_db_obj = Class.new(ActiveRecord::Base) do
|
39
|
+
set_table_name 'dummies'
|
40
|
+
as_enum :gender, genders
|
41
|
+
end
|
42
|
+
|
43
|
+
d = with_db_obj.find_by_name('Bella');
|
44
|
+
|
45
|
+
assert_respond_to with_db_obj, :female
|
46
|
+
assert_respond_to with_db_obj, :male
|
47
|
+
assert_equal 0, with_db_obj.male
|
48
|
+
end
|
49
|
+
|
50
|
+
test "that accessing keys and values of each enumeration value works as expected" do
|
51
|
+
genders = Gender.find(:all, :order => :id)
|
52
|
+
|
53
|
+
male = genders.first
|
54
|
+
female = genders.last
|
55
|
+
|
56
|
+
with_db_obj = Class.new(ActiveRecord::Base) do
|
57
|
+
set_table_name 'dummies'
|
58
|
+
as_enum :gender, genders
|
59
|
+
end
|
60
|
+
|
61
|
+
assert_same male.id, with_db_obj.male
|
62
|
+
assert_same male, with_db_obj.male(true)
|
63
|
+
|
64
|
+
assert_same :male, Dummy.male(true)
|
65
|
+
assert_same 0, Dummy.male
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class ObjectSupportTest < ActiveSupport::TestCase
|
2
|
+
|
3
|
+
test "ensure that symbols stay symbols" do
|
4
|
+
assert_same :sym, :sym.to_enum_sym
|
5
|
+
end
|
6
|
+
|
7
|
+
test "ensure that strings are just converted to symbols, whatever they look like" do
|
8
|
+
assert_same :sym, 'sym'.to_enum_sym
|
9
|
+
assert_same :OtherSym, 'OtherSym'.to_enum_sym
|
10
|
+
end
|
11
|
+
|
12
|
+
test "convert custom class to symbol, by providing attr_accessor :name" do
|
13
|
+
has_name = Class.new do
|
14
|
+
attr_accessor :name
|
15
|
+
end
|
16
|
+
|
17
|
+
named = has_name.new
|
18
|
+
named.name = 'sym'
|
19
|
+
|
20
|
+
assert_same :sym, named.to_enum_sym
|
21
|
+
|
22
|
+
another_named = has_name.new
|
23
|
+
another_named.name = 'Contains Spaces?'
|
24
|
+
|
25
|
+
assert_same :contains_spaces, another_named.to_enum_sym
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PrefixesTest < ActiveSupport::TestCase
|
4
|
+
def setup
|
5
|
+
reload_db
|
6
|
+
end
|
7
|
+
|
8
|
+
test "set :prefix => true and ensure that 'gender' is prefixed to <symbol>? and <symbol>! methods" do
|
9
|
+
with_prefix = Class.new(ActiveRecord::Base) do
|
10
|
+
set_table_name 'dummies'
|
11
|
+
as_enum :gender, [:male, :female], :prefix => true
|
12
|
+
end
|
13
|
+
|
14
|
+
d = with_prefix.new :gender => :male
|
15
|
+
assert_respond_to d, :gender_male?
|
16
|
+
assert_respond_to d, :gender_male!
|
17
|
+
assert_respond_to d, :gender_female?
|
18
|
+
assert_respond_to d, :gender_female!
|
19
|
+
|
20
|
+
# just ensure that it DOES NOT RESPOND TO good old male!
|
21
|
+
assert !d.respond_to?(:male!)
|
22
|
+
end
|
23
|
+
|
24
|
+
test "set :prefix => 'didum' and ensure that 'didum' is prefix to <symbol>? and <symbol>! methods" do
|
25
|
+
with_string_prefix = Class.new(ActiveRecord::Base) do
|
26
|
+
set_table_name 'dummies'
|
27
|
+
as_enum :gender, [:male, :female], :prefix => 'didum'
|
28
|
+
end
|
29
|
+
|
30
|
+
d = with_string_prefix.new :gender => :female
|
31
|
+
assert_respond_to d, :didum_female?
|
32
|
+
assert_respond_to d, :didum_female!
|
33
|
+
|
34
|
+
# just check wheter the results are still correct :)
|
35
|
+
assert d.didum_female?
|
36
|
+
assert !d.didum_male?
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class SeArraySupportTest < ActiveSupport::TestCase
|
2
|
+
test "that when simple array is merge to hash, array values are the keys and indicies are values" do
|
3
|
+
a = [:a, :b, :c, :d]
|
4
|
+
h = a.to_hash_magic
|
5
|
+
|
6
|
+
assert_same 0, h[:a]
|
7
|
+
assert_same 1, h[:b]
|
8
|
+
assert_same 2, h[:c]
|
9
|
+
assert_same 3, h[:d]
|
10
|
+
end
|
11
|
+
|
12
|
+
test "that an already correct looking array is converted to a hash" do
|
13
|
+
a = [[:a, 5], [:b, 10]]
|
14
|
+
h = a.to_hash_magic
|
15
|
+
|
16
|
+
assert_same 5, h[:a]
|
17
|
+
assert_same 10, h[:b]
|
18
|
+
end
|
19
|
+
|
20
|
+
test "that an array of ActiveRecords are converted to <obj> => obj.id" do
|
21
|
+
reload_db :genders => true # reload db
|
22
|
+
a = Gender.find(:all, :order => :id)
|
23
|
+
|
24
|
+
male = Gender.find(0)
|
25
|
+
female = Gender.find(1)
|
26
|
+
|
27
|
+
h = a.to_hash_magic
|
28
|
+
|
29
|
+
assert_same 0, h[male]
|
30
|
+
assert_same 1, h[female]
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SimpleEnumTest < ActiveSupport::TestCase
|
4
|
+
def setup
|
5
|
+
reload_db
|
6
|
+
end
|
7
|
+
|
8
|
+
test "get the correct integer values when setting to symbol" do
|
9
|
+
d = Dummy.new
|
10
|
+
d.gender = :male
|
11
|
+
assert_equal(0, d.gender_cd)
|
12
|
+
end
|
13
|
+
|
14
|
+
test "get the correct symbol when setting the integer value" do
|
15
|
+
d = Dummy.new
|
16
|
+
d.gender_cd = 1
|
17
|
+
assert_equal(:female, d.gender)
|
18
|
+
end
|
19
|
+
|
20
|
+
test "verify that <symbol>? returns correct result" do
|
21
|
+
d = Dummy.new
|
22
|
+
d.gender = :male
|
23
|
+
assert_equal(true, d.male?)
|
24
|
+
assert_equal(false, d.female?)
|
25
|
+
end
|
26
|
+
|
27
|
+
test "get symbol when rows are fetched from db" do
|
28
|
+
# Anna
|
29
|
+
assert_equal(:female, Dummy.find(1).gender)
|
30
|
+
assert_equal(:alpha, Dummy.find(1).word)
|
31
|
+
assert_equal(:foo, Dummy.find(1).didum)
|
32
|
+
|
33
|
+
# Bella
|
34
|
+
assert_equal(true, Dummy.find(2).female?)
|
35
|
+
assert_equal(true, Dummy.find(2).beta?)
|
36
|
+
assert_equal(:bar, Dummy.find(2).didum)
|
37
|
+
|
38
|
+
# Chris
|
39
|
+
assert_equal(false, Dummy.find(3).female?)
|
40
|
+
assert_equal(:gamma, Dummy.find(3).word)
|
41
|
+
assert_equal(:foobar, Dummy.find(3).didum)
|
42
|
+
end
|
43
|
+
|
44
|
+
test "create and save new record then test symbols" do
|
45
|
+
d = Dummy.create({ :name => 'Dummy', :gender_cd => 0 }) # :gender => male
|
46
|
+
assert_equal(true, d.male?)
|
47
|
+
|
48
|
+
# change :gender_cd to 1
|
49
|
+
d.female!
|
50
|
+
d.save!
|
51
|
+
assert_equal(true, Dummy.find(d.id).female?)
|
52
|
+
end
|
53
|
+
|
54
|
+
test "add validation and test validations" do
|
55
|
+
Dummy.class_eval do; validates_as_enum :gender; end
|
56
|
+
#with_validation = Class.new(Dummy) do
|
57
|
+
# validates_as_enum :gender
|
58
|
+
#end
|
59
|
+
|
60
|
+
d = Dummy.new :gender_cd => 5 # invalid number :)
|
61
|
+
assert_equal(false, d.save)
|
62
|
+
d.gender_cd = 1
|
63
|
+
assert_equal(true, d.save)
|
64
|
+
assert_equal(:female, d.gender)
|
65
|
+
end
|
66
|
+
|
67
|
+
test "raises ArgumentError if invalid symbol is passed" do
|
68
|
+
assert_raise ArgumentError do
|
69
|
+
Dummy.new :gender => :foo
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
test "raises NO ArgumentError if :whiny => false is defined" do
|
74
|
+
not_whiny = Class.new(Dummy) do
|
75
|
+
as_enum :gender, [:male, :female], :whiny => false
|
76
|
+
end
|
77
|
+
|
78
|
+
d = not_whiny.new :gender => :foo
|
79
|
+
assert_nil(d.gender)
|
80
|
+
end
|
81
|
+
|
82
|
+
test "ensure that setting to 'nil' works if :whiny => true and :whiny => false" do
|
83
|
+
d = Dummy.new :gender => :male
|
84
|
+
assert_equal(:male, d.gender)
|
85
|
+
d.gender = nil
|
86
|
+
assert_nil(d.gender)
|
87
|
+
|
88
|
+
not_whiny_again = Class.new(Dummy) do
|
89
|
+
as_enum :gender, [:male, :female], :whiny => false
|
90
|
+
end
|
91
|
+
|
92
|
+
d = not_whiny_again.new :gender => :male
|
93
|
+
assert_equal(:male, d.gender)
|
94
|
+
d.gender = nil
|
95
|
+
assert_nil(d.gender)
|
96
|
+
end
|
97
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# Setup environment for both tests and IRB interactive console
|
2
|
+
#
|
3
|
+
|
4
|
+
$KCODE = 'u' # to make parameterize work...
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
|
8
|
+
gem 'sqlite3-ruby'
|
9
|
+
|
10
|
+
require 'active_support'
|
11
|
+
require 'active_record'
|
12
|
+
|
13
|
+
# setup fake rails env
|
14
|
+
ROOT = File.join(File.dirname(__FILE__), '..')
|
15
|
+
RAILS_ROOT = ROOT
|
16
|
+
RAILS_ENV = 'test'
|
17
|
+
|
18
|
+
# create database connection (in memory db!)
|
19
|
+
ActiveRecord::Base.establish_connection({ :adapter => 'sqlite3', :database => ':memory:'})
|
20
|
+
|
21
|
+
# load simple_enum
|
22
|
+
require File.join(ROOT, 'init')
|
23
|
+
|
24
|
+
# load dummy class
|
25
|
+
require File.join(ROOT, 'test', 'models')
|
26
|
+
|
27
|
+
# Reload database
|
28
|
+
def reload_db(options = {})
|
29
|
+
options = { :fill => true, :genders => false }.merge(options)
|
30
|
+
ActiveRecord::Base.connection.create_table :dummies, :force => true do |t|
|
31
|
+
t.column :name, :string
|
32
|
+
t.column :gender_cd, :integer
|
33
|
+
t.column :word_cd, :string, :limit => 5
|
34
|
+
t.column :other, :integer
|
35
|
+
end
|
36
|
+
|
37
|
+
# Create ref-data table and fill with records
|
38
|
+
ActiveRecord::Base.connection.create_table :genders, :force => true do |t|
|
39
|
+
t.column :name, :string
|
40
|
+
end
|
41
|
+
|
42
|
+
if options[:fill]
|
43
|
+
# fill db with some rows
|
44
|
+
Dummy.create({ :name => 'Anna', :gender_cd => 1, :word_cd => 'alpha', :other => 0})
|
45
|
+
Dummy.create({ :name => 'Bella', :gender_cd => 1, :word_cd => 'beta', :other => 1})
|
46
|
+
Dummy.create({ :name => 'Chris', :gender_cd => 0, :word_cd => 'gamma', :other => 2})
|
47
|
+
end
|
48
|
+
|
49
|
+
if options[:genders]
|
50
|
+
male = Gender.new({ :name => 'male' })
|
51
|
+
male.id = 0;
|
52
|
+
male.save!
|
53
|
+
|
54
|
+
female = Gender.new({ :name => 'female' })
|
55
|
+
female.id = 1;
|
56
|
+
female.save!
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# do some magic to initialze DB for IRB session
|
61
|
+
if Object.const_defined?('IRB')
|
62
|
+
reload_db :fill => true, :genders => true
|
63
|
+
else # and load test classes when in test cases...
|
64
|
+
require 'test/unit'
|
65
|
+
require 'active_support/test_case'
|
66
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SlimDummy < ActiveRecord::Base
|
4
|
+
set_table_name 'dummies'
|
5
|
+
as_enum :gender, [:male, :female], :slim => true
|
6
|
+
end
|
7
|
+
|
8
|
+
class WithoutShortcutsTest < ActiveSupport::TestCase
|
9
|
+
def setup
|
10
|
+
reload_db
|
11
|
+
end
|
12
|
+
|
13
|
+
test "that no shortcut methods are generated if :slim => true" do
|
14
|
+
jane = SlimDummy.new
|
15
|
+
jane.gender = :female
|
16
|
+
|
17
|
+
# ensure that other methods still work as expected
|
18
|
+
assert_equal 1, jane.gender_cd
|
19
|
+
assert_equal :female, jane.gender
|
20
|
+
|
21
|
+
# then check for availability of shortcut methods
|
22
|
+
assert !jane.respond_to?(:male!), "should not respond_to <male!>"
|
23
|
+
assert !jane.respond_to?(:female?), "should not respond_to <female?>"
|
24
|
+
end
|
25
|
+
|
26
|
+
test "that saving and loading from a DB still works, even if :slim => true" do
|
27
|
+
anna = SlimDummy.find_by_name 'Anna'
|
28
|
+
|
29
|
+
assert_equal 1, anna.gender_cd
|
30
|
+
assert_equal :female, anna.gender
|
31
|
+
|
32
|
+
# change anna, save + reload
|
33
|
+
anna.gender = :male
|
34
|
+
anna.save!
|
35
|
+
anna.reload
|
36
|
+
|
37
|
+
assert_equal 0, anna.gender_cd
|
38
|
+
assert_equal :male, anna.gender
|
39
|
+
end
|
40
|
+
end
|