simple_enum 1.6.9 → 2.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/LICENSE +1 -1
- data/README.md +248 -0
- data/Rakefile +4 -21
- data/lib/simple_enum/accessors/accessor.rb +55 -0
- data/lib/simple_enum/accessors/ignore_accessor.rb +11 -0
- data/lib/simple_enum/accessors/whiny_accessor.rb +12 -0
- data/lib/simple_enum/accessors.rb +18 -0
- data/lib/simple_enum/attribute.rb +77 -0
- data/lib/simple_enum/enum.rb +37 -0
- data/lib/simple_enum/hasher.rb +26 -0
- data/lib/simple_enum/mongoid.rb +11 -15
- data/lib/simple_enum/translation.rb +17 -0
- data/lib/simple_enum/version.rb +2 -2
- data/lib/simple_enum.rb +19 -276
- data/simple_enum.gemspec +9 -9
- data/spec/simple_enum/accessors_spec.rb +212 -0
- data/spec/simple_enum/attribute_spec.rb +208 -0
- data/spec/simple_enum/enum_spec.rb +96 -0
- data/spec/simple_enum/hasher_spec.rb +63 -0
- data/spec/simple_enum/mongoid_spec.rb +44 -0
- data/spec/simple_enum/translation_spec.rb +66 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/support/active_record_support.rb +23 -0
- data/spec/support/i18n_support.rb +12 -0
- data/spec/support/model_support.rb +47 -0
- data/spec/support/mongoid_support.rb +21 -0
- metadata +50 -56
- data/README.rdoc +0 -293
- data/lib/simple_enum/enum_hash.rb +0 -64
- data/lib/simple_enum/validation.rb +0 -58
- data/locales/en.yml +0 -10
- data/test/array_conversions_test.rb +0 -21
- data/test/class_methods_test.rb +0 -114
- data/test/dirty_attributes_test.rb +0 -37
- data/test/enum_hash_test.rb +0 -73
- data/test/finders_test.rb +0 -45
- data/test/locales.yml +0 -25
- data/test/mongoid_test.rb +0 -66
- data/test/object_backed_test.rb +0 -61
- data/test/orm/active_record.rb +0 -114
- data/test/orm/common.rb +0 -23
- data/test/orm/mongoid.rb +0 -114
- data/test/poro_test.rb +0 -20
- data/test/prefixes_test.rb +0 -36
- data/test/simple_enum_test.rb +0 -314
- data/test/test_helper.rb +0 -40
- data/test/without_shortcuts_test.rb +0 -39
data/test/class_methods_test.rb
DELETED
@@ -1,114 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ClassMethodsTest < MiniTest::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
reload_db
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_that_klass_genders_array_accessor_equal_to_attr_accessor
|
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
|
-
refute_nil Dummy.genders[:female]
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_inheritance_of_genders_to_subclasses
|
17
|
-
# issue#3
|
18
|
-
assert_equal({ :female => 1, :male => 0}, SpecificDummy.genders)
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_genders_reader_created
|
22
|
-
assert_equal [0, 1], Dummy.genders.values.sort
|
23
|
-
assert_equal %w{female male}, Dummy.genders.keys.map(&:to_s).sort
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_that_klass_genders_return_array_of_values
|
27
|
-
# usefuled for IN clauses
|
28
|
-
assert_equal [0, 1], Dummy.genders(:male, :female)
|
29
|
-
assert_equal [1, 0], Dummy.genders(:female, :male)
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_inverted_hash_returns_synonym_by_code
|
33
|
-
assert_equal :male, Dummy.genders.invert[0]
|
34
|
-
assert_equal :female, Dummy.genders.invert[1]
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_generation_of_value_shortcuts_on_class
|
38
|
-
g = Dummy.new
|
39
|
-
|
40
|
-
assert_equal 0, Dummy.male
|
41
|
-
assert_equal 1, Dummy.female
|
42
|
-
assert_equal 'alpha', Dummy.alpha
|
43
|
-
assert_respond_to Dummy, :male
|
44
|
-
assert_respond_to Dummy, :female
|
45
|
-
assert_respond_to Dummy, :beta
|
46
|
-
assert_respond_to Dummy, :foobar
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_that_no_klass_shortcuts_are_created_if_slim_true
|
50
|
-
with_slim = named_dummy('Dummy1') do
|
51
|
-
as_enum :gender, [:male, :female], :slim => true
|
52
|
-
end
|
53
|
-
|
54
|
-
assert !with_slim.respond_to?(:male)
|
55
|
-
assert !with_slim.respond_to?(:female)
|
56
|
-
assert_respond_to with_slim, :genders
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_that_no_klass_shortcuts_are_created_if_slim_class_though_instance_shortcuts_are
|
60
|
-
with_slim_class = named_dummy('Dummy2') do
|
61
|
-
as_enum :gender, [:male, :female], :slim => :class
|
62
|
-
end
|
63
|
-
|
64
|
-
jane = with_slim_class.new
|
65
|
-
|
66
|
-
assert_respond_to jane, :male!
|
67
|
-
assert_respond_to jane, :female!
|
68
|
-
assert !with_slim_class.respond_to?(:male)
|
69
|
-
assert !with_slim_class.respond_to?(:female)
|
70
|
-
assert_respond_to with_slim_class, :genders
|
71
|
-
assert_same 0, with_slim_class.genders.male
|
72
|
-
assert_same 1, with_slim_class.genders[:female]
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_that_klass_shortcuts_respect_prefix_true_and_are_prefixed_by_enum_cd
|
76
|
-
with_prefix = named_dummy('Dummy3') do
|
77
|
-
as_enum :gender, [:male, :female], :prefix => true
|
78
|
-
end
|
79
|
-
|
80
|
-
assert !with_prefix.respond_to?(:male)
|
81
|
-
assert !with_prefix.respond_to?(:female)
|
82
|
-
assert_respond_to with_prefix, :gender_male
|
83
|
-
assert_respond_to with_prefix, :gender_female
|
84
|
-
assert_equal 0, with_prefix.gender_male
|
85
|
-
assert_respond_to with_prefix, :genders
|
86
|
-
end
|
87
|
-
|
88
|
-
def test_to_ensure_that_klass_shortcut_also_work_with_custom_prefixes
|
89
|
-
with_custom_prefix = named_dummy('Dummy4') do
|
90
|
-
as_enum :gender, [:male, :female], :prefix => :g
|
91
|
-
end
|
92
|
-
|
93
|
-
assert !with_custom_prefix.respond_to?(:male)
|
94
|
-
assert !with_custom_prefix.respond_to?(:female)
|
95
|
-
assert !with_custom_prefix.respond_to?(:gender_female)
|
96
|
-
assert_respond_to with_custom_prefix, :g_male
|
97
|
-
assert_respond_to with_custom_prefix, :g_female
|
98
|
-
assert_equal 1, with_custom_prefix.g_female
|
99
|
-
assert_respond_to with_custom_prefix, :genders
|
100
|
-
end
|
101
|
-
|
102
|
-
def test_that_the_human_enum_name_method_returns_translated_humanized_values
|
103
|
-
assert_equal :male.to_s.humanize, Dummy.human_enum_name(:genders, :male)
|
104
|
-
assert_equal "Girl", Dummy.human_enum_name(:genders, :female)
|
105
|
-
assert_equal "Foo", Dummy.human_enum_name(:didums, :foo)
|
106
|
-
assert_equal "Foos", Dummy.human_enum_name(:didums, :foo, :count => 5)
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_enum_for_select_value_class_method
|
110
|
-
for_select = Dummy.genders_for_select(:value)
|
111
|
-
assert_equal ["Male", 0], for_select.first
|
112
|
-
assert_equal ["Girl", 1], for_select.last
|
113
|
-
end
|
114
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
DirtyDummy = anonymous_dummy do
|
4
|
-
as_enum :gender, [:male, :female], :dirty => true
|
5
|
-
end
|
6
|
-
|
7
|
-
class DirtyAttributesTest < ActiveSupport::TestCase
|
8
|
-
def setup
|
9
|
-
reload_db
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_setting_using_changed_on_enum
|
13
|
-
jane = DirtyDummy.create!(:gender => :female)
|
14
|
-
assert_equal 1, jane.gender_cd
|
15
|
-
jane.gender = :male # operation? =)
|
16
|
-
assert_equal :male, jane.gender
|
17
|
-
assert_equal true, jane.gender_cd_changed?
|
18
|
-
assert_equal true, jane.gender_changed?
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_access_old_value_via_gender_was
|
22
|
-
john = DirtyDummy.create!(:gender => :male)
|
23
|
-
assert_equal 0, john.gender_cd
|
24
|
-
john.gender = :female
|
25
|
-
assert_equal :female, john.gender
|
26
|
-
assert_equal 0, john.gender_cd_was
|
27
|
-
assert_equal :male, john.gender_was
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_dirty_methods_are_disabled_by_default
|
31
|
-
no_dirty = Dummy.new
|
32
|
-
assert !no_dirty.respond_to?(:gender_was), "should not respond_to :gender_was"
|
33
|
-
assert !no_dirty.respond_to?(:gender_changed?), "should not respond_to :gender_changed?"
|
34
|
-
assert !no_dirty.respond_to?(:word_was), "should not respond_to :word_was"
|
35
|
-
assert !no_dirty.respond_to?(:word_changed?), "should not respond_to :word_changed?"
|
36
|
-
end
|
37
|
-
end
|
data/test/enum_hash_test.rb
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'simple_enum/enum_hash'
|
3
|
-
|
4
|
-
class EnumHashTest < MiniTest::Unit::TestCase
|
5
|
-
|
6
|
-
def test_create_new_enumhash_instance_from_array_of_symbols
|
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
|
-
def test_create_new_enumhash_instance_from_hash
|
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
|
-
def test_create_new_enumhash_instance_from_query_results
|
23
|
-
reload_db :genders => true
|
24
|
-
genders = SimpleEnum::EnumHash.new Gender.all
|
25
|
-
|
26
|
-
assert_same 0, genders[@male]
|
27
|
-
assert_same genders[@male], genders[:male]
|
28
|
-
assert_same 1, genders.female
|
29
|
-
assert_equal @male, genders.send(:male, true)
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_that_enumhash_keys_are_ordered
|
33
|
-
ordered = SimpleEnum::EnumHash.new [:alpha, :beta, :gamma, :delta, :epsilon, :zeta, :eta]
|
34
|
-
expected_keys = [:alpha, :beta, :gamma, :delta, :epsilon, :zeta, :eta]
|
35
|
-
assert_equal expected_keys, ordered.keys
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_valid_key_value_association_when_simple_array_is_merged_into_enumhash
|
39
|
-
a = [:a, :b, :c, :d]
|
40
|
-
h = SimpleEnum::EnumHash.new(a)
|
41
|
-
|
42
|
-
assert_same 0, h[:a]
|
43
|
-
assert_same 1, h[:b]
|
44
|
-
assert_same 2, h[:c]
|
45
|
-
assert_same 3, h[:d]
|
46
|
-
assert_equal [:a, :b, :c, :d], h.keys
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_that_an_already_correct_looking_array_is_converted_to_hash
|
50
|
-
a = [[:a, 5], [:b, 10]]
|
51
|
-
h = SimpleEnum::EnumHash.new(a)
|
52
|
-
|
53
|
-
assert_same 5, h[:a]
|
54
|
-
assert_same 10, h[:b]
|
55
|
-
assert_equal [:a, :b], h.keys
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_that_an_array_of_query_results_are_converted_to_result_ids
|
59
|
-
reload_db :genders => true # reload db
|
60
|
-
a = Gender.all
|
61
|
-
|
62
|
-
h = SimpleEnum::EnumHash.new(a)
|
63
|
-
|
64
|
-
assert_same 0, h[@male]
|
65
|
-
assert_same 1, h[@female]
|
66
|
-
end
|
67
|
-
|
68
|
-
def test_strings_option
|
69
|
-
h = SimpleEnum::EnumHash.new([:male, :female], true)
|
70
|
-
assert_equal "male", h[:male]
|
71
|
-
assert_equal "female", h[:female]
|
72
|
-
end
|
73
|
-
end
|
data/test/finders_test.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class FindersTest < MiniTest::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
reload_db
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_find_all_female_genders
|
9
|
-
girls = Dummy.where(:gender_cd => Dummy.genders[:female]).sort { |a,b| a.name <=> b.name }
|
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
|
-
def test_find_all_gamma_words
|
19
|
-
gammas = Dummy.where(:word_cd => Dummy.words(:gamma)).all
|
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
|
-
def test_find_all_with_attribute_didum_equal_to_foo
|
29
|
-
skip('Not available in Mongoid') if mongoid?
|
30
|
-
|
31
|
-
foos = Dummy.where('other = ?', Dummy.didums(:foo)).all
|
32
|
-
|
33
|
-
assert_equal 1, foos.length
|
34
|
-
assert_equal false, foos.first.foobar?
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_find_using_insecure_inline_string_conditions
|
38
|
-
skip('Not available in Mongoid') if mongoid?
|
39
|
-
|
40
|
-
men = Dummy.where("gender_cd = #{Dummy.genders(:male)}").all
|
41
|
-
|
42
|
-
assert_equal 1, men.length
|
43
|
-
assert_equal true, men.first.male?
|
44
|
-
end
|
45
|
-
end
|
data/test/locales.yml
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# english translation
|
2
|
-
en:
|
3
|
-
activerecord:
|
4
|
-
errors:
|
5
|
-
models:
|
6
|
-
validated_computer:
|
7
|
-
attributes:
|
8
|
-
operating_system:
|
9
|
-
invalid_enum: y u no os?
|
10
|
-
mongoid:
|
11
|
-
errors:
|
12
|
-
models:
|
13
|
-
validated_computer:
|
14
|
-
attributes:
|
15
|
-
operating_system:
|
16
|
-
invalid_enum: y u no os?
|
17
|
-
|
18
|
-
enums:
|
19
|
-
dummy:
|
20
|
-
genders:
|
21
|
-
female: Girl
|
22
|
-
didums:
|
23
|
-
foo:
|
24
|
-
one: Foo
|
25
|
-
other: Foos
|
data/test/mongoid_test.rb
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class MongoidTest < MiniTest::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@default_options = SimpleEnum.default_options
|
6
|
-
reload_db
|
7
|
-
end
|
8
|
-
|
9
|
-
def teardown
|
10
|
-
SimpleEnum.default_options.clear.update(@default_options)
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_creates_a_field_per_default
|
14
|
-
skip('Only available in mongoid') unless mongoid?
|
15
|
-
klass = anonymous_dummy do
|
16
|
-
as_enum :gender, [:male, :female]
|
17
|
-
end
|
18
|
-
refute_nil klass.new.fields['gender_cd']
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_passing_custom_field_options
|
22
|
-
skip('Only available in mongoid') unless mongoid?
|
23
|
-
klass = anonymous_dummy do
|
24
|
-
field :verify, :type => Integer
|
25
|
-
as_enum :gender, [:male, :female], :field => { :type => Integer, :default => 1 }
|
26
|
-
end
|
27
|
-
|
28
|
-
gender_field = klass.new.fields['gender_cd']
|
29
|
-
refute_nil gender_field
|
30
|
-
assert_equal 1, gender_field.default
|
31
|
-
assert_equal klass.fields['verify'].class, gender_field.class
|
32
|
-
assert_equal :female, klass.new.gender
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_skip_field_creation_if_field_false
|
36
|
-
skip('Only available in mongoid') unless mongoid?
|
37
|
-
klass = anonymous_dummy do
|
38
|
-
as_enum :gender, [:male, :female], :field => false
|
39
|
-
end
|
40
|
-
|
41
|
-
assert_nil klass.new.fields['gender_cd']
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_default_field_options
|
45
|
-
skip('Only available in mongoid') unless mongoid?
|
46
|
-
SimpleEnum.default_options[:field] = { :type => String }
|
47
|
-
klass = anonymous_dummy do
|
48
|
-
as_enum :gender, [:male, :female]
|
49
|
-
end
|
50
|
-
|
51
|
-
refute_nil klass.new.fields['gender_cd']
|
52
|
-
assert_equal String, klass.new.fields['gender_cd'].options[:type]
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_default_field_options_are_merged_recursivly
|
56
|
-
skip('Only available in mongoid') unless mongoid?
|
57
|
-
SimpleEnum.default_options[:field] = { :type => String }
|
58
|
-
klass = anonymous_dummy do
|
59
|
-
as_enum :gender, [:male, :female], :field => { :default => "0" }
|
60
|
-
end
|
61
|
-
|
62
|
-
refute_nil klass.new.fields['gender_cd']
|
63
|
-
assert_equal String, klass.new.fields['gender_cd'].options[:type]
|
64
|
-
assert_equal "0", klass.new.fields['gender_cd'].options[:default]
|
65
|
-
end
|
66
|
-
end
|
data/test/object_backed_test.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ObjectBackedTest < MiniTest::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
reload_db :genders => true
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_how_working_with_object_backed_columns_work
|
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 = anonymous_dummy do
|
19
|
-
as_enum :gender, { simple_obj.new('Male') => 0, simple_obj.new('Female') => 1 }
|
20
|
-
end
|
21
|
-
|
22
|
-
d = with_object.where(:name => 'Anna').first
|
23
|
-
|
24
|
-
assert_same simple_obj, d.gender.class
|
25
|
-
assert_equal 'Female', d.gender.name
|
26
|
-
assert_same true, d.female?
|
27
|
-
assert_same false, d.male?
|
28
|
-
assert_same 0, with_object.male
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_db_backed_objects
|
32
|
-
# using method described in 'Advanced Rails Recipes - Recipe 61: Look Up Constant Data Efficiently'
|
33
|
-
# "cache" as defined in ARR#61
|
34
|
-
genders = Gender.all
|
35
|
-
# works without mapping... .map { |g| [g, g.id] }
|
36
|
-
|
37
|
-
# use cached array of values
|
38
|
-
with_db_obj = anonymous_dummy do
|
39
|
-
as_enum :gender, genders
|
40
|
-
end
|
41
|
-
|
42
|
-
d = with_db_obj.where(:name => 'Bella').first
|
43
|
-
|
44
|
-
assert_respond_to with_db_obj, :female
|
45
|
-
assert_respond_to with_db_obj, :male
|
46
|
-
assert_equal 0, with_db_obj.male
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_that_accesing_keys_and_values_of_each_enumeration_value_works_as_expected
|
50
|
-
|
51
|
-
with_db_obj = anonymous_dummy do
|
52
|
-
as_enum :gender, Gender.all.to_a
|
53
|
-
end
|
54
|
-
|
55
|
-
assert_same 0, with_db_obj.male
|
56
|
-
assert_equal @male.id, with_db_obj.male(true).id
|
57
|
-
|
58
|
-
assert_same :male, Dummy.male(true)
|
59
|
-
assert_same 0, Dummy.male
|
60
|
-
end
|
61
|
-
end
|
data/test/orm/active_record.rb
DELETED
@@ -1,114 +0,0 @@
|
|
1
|
-
require 'active_record/version'
|
2
|
-
|
3
|
-
def orm_version
|
4
|
-
ActiveRecord::VERSION::STRING
|
5
|
-
end
|
6
|
-
|
7
|
-
def ar32?
|
8
|
-
ActiveRecord::VERSION::MAJOR >= 3 && ActiveRecord::VERSION::MINOR >= 2
|
9
|
-
end
|
10
|
-
|
11
|
-
def setup_db
|
12
|
-
# create database connection (in memory db!)
|
13
|
-
ActiveRecord::Base.establish_connection({
|
14
|
-
:adapter => RUBY_PLATFORM =~ /java/ ? 'jdbcsqlite3' : 'sqlite3',
|
15
|
-
:database => ':memory:'})
|
16
|
-
|
17
|
-
# Fix visitor, for JRuby
|
18
|
-
if RUBY_PLATFORM =~ /java/ && ar32?
|
19
|
-
ActiveRecord::ConnectionAdapters::SQLiteAdapter.send(:define_method, :visitor) do
|
20
|
-
@visitor ||= Arel::Visitors::SQLite.new(self)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
# Reload database
|
26
|
-
def reload_db(options = {})
|
27
|
-
ActiveRecord::Base.connection.create_table :dummies, :force => true do |t|
|
28
|
-
t.column :name, :string
|
29
|
-
t.column :gender_cd, :integer
|
30
|
-
t.column :word_cd, :string, :limit => 5
|
31
|
-
t.column :role_cd, :string
|
32
|
-
t.column :other, :integer
|
33
|
-
t.column :numeric_cd, :string
|
34
|
-
t.column :nilish_cd, :string
|
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
|
-
# Validations
|
43
|
-
ActiveRecord::Base.connection.create_table :computers, :force => true do |t|
|
44
|
-
t.column :name, :string
|
45
|
-
t.column :operating_system_cd, :integer
|
46
|
-
t.column :manufacturer_cd, :integer
|
47
|
-
end
|
48
|
-
|
49
|
-
fill_db(options)
|
50
|
-
end
|
51
|
-
|
52
|
-
# Models
|
53
|
-
def anonymous_dummy(&block)
|
54
|
-
Class.new(ActiveRecord::Base) do
|
55
|
-
ar32? ? self.table_name = 'dummies' : set_table_name('dummies')
|
56
|
-
instance_eval &block
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def extend_computer(current_i18n_name = "Computer", &block)
|
61
|
-
Class.new(Computer) do
|
62
|
-
ar32? ? self.table_name = 'computers' : set_table_name('computers')
|
63
|
-
instance_eval &block
|
64
|
-
instance_eval <<-RUBY
|
65
|
-
def self.model_name; MockName.mock!(#{current_i18n_name.inspect}) end
|
66
|
-
RUBY
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def extend_dummy(current_i18n_name = "Dummy", &block)
|
71
|
-
Class.new(Dummy) do
|
72
|
-
ar32? ? self.table_name = 'dummies' : set_table_name('dummies')
|
73
|
-
instance_eval &block
|
74
|
-
instance_eval <<-RUBY
|
75
|
-
def self.model_name; MockName.mock!(#{current_i18n_name.inspect}) end
|
76
|
-
RUBY
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def named_dummy(class_name, &block)
|
81
|
-
begin
|
82
|
-
return class_name.constantize
|
83
|
-
rescue NameError
|
84
|
-
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
|
85
|
-
klass.module_eval do
|
86
|
-
ar32? ? self.table_name = 'dummies' : set_table_name('dummies')
|
87
|
-
instance_eval &block
|
88
|
-
end
|
89
|
-
klass
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
class Dummy < ActiveRecord::Base
|
94
|
-
as_enum :gender, [:male, :female]
|
95
|
-
as_enum :word, { :alpha => 'alpha', :beta => 'beta', :gamma => 'gamma'}
|
96
|
-
as_enum :didum, [ :foo, :bar, :foobar ], :column => 'other'
|
97
|
-
as_enum :role, [:admin, :member, :anon], :strings => true
|
98
|
-
as_enum :numeric, [:"100", :"3.14"], :strings => true
|
99
|
-
as_enum :nilish, [:nil], :strings => true
|
100
|
-
end
|
101
|
-
|
102
|
-
class Gender < ActiveRecord::Base
|
103
|
-
end
|
104
|
-
|
105
|
-
class Computer < ActiveRecord::Base
|
106
|
-
as_enum :manufacturer, [:dell, :compaq, :apple]
|
107
|
-
as_enum :operating_system, [:windows, :osx, :linux, :bsd]
|
108
|
-
end
|
109
|
-
|
110
|
-
# Used to test STI stuff
|
111
|
-
class SpecificDummy < Dummy
|
112
|
-
ar32? ? self.table_name = 'dummies' : set_table_name('dummies')
|
113
|
-
end
|
114
|
-
|
data/test/orm/common.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
class MockName < String
|
2
|
-
def name; to_s end
|
3
|
-
|
4
|
-
def self.mock!(name)
|
5
|
-
ActiveModel::Name.new(MockName.new(name))
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
def fill_db(options={})
|
10
|
-
options = { :fill => true, :genders => false }.merge(options)
|
11
|
-
|
12
|
-
if options[:fill]
|
13
|
-
# fill db with some rows
|
14
|
-
Dummy.create({ :name => 'Anna', :gender_cd => 1, :word_cd => 'alpha', :other => 0})
|
15
|
-
Dummy.create({ :name => 'Bella', :gender_cd => 1, :word_cd => 'beta', :other => 1})
|
16
|
-
Dummy.create({ :name => 'Chris', :gender_cd => 0, :word_cd => 'gamma', :other => 2})
|
17
|
-
end
|
18
|
-
|
19
|
-
if options[:genders]
|
20
|
-
@male = Gender.create!({ :name => 'male' })
|
21
|
-
@female = Gender.create!({ :name => 'female' })
|
22
|
-
end
|
23
|
-
end
|
data/test/orm/mongoid.rb
DELETED
@@ -1,114 +0,0 @@
|
|
1
|
-
require 'mongoid/version'
|
2
|
-
require 'simple_enum/mongoid'
|
3
|
-
|
4
|
-
def orm_version
|
5
|
-
Mongoid::VERSION
|
6
|
-
end
|
7
|
-
|
8
|
-
def setup_db
|
9
|
-
# create database connection
|
10
|
-
Mongoid.configure do |config|
|
11
|
-
config.master = Mongo::Connection.new('localhost').db("simple-enum-test-suite")
|
12
|
-
config.use_utc = true
|
13
|
-
config.include_root_in_json = true
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
# Reload database
|
19
|
-
def reload_db(options = {})
|
20
|
-
|
21
|
-
# clear collections except system
|
22
|
-
Mongoid.master.collections.select do |collection|
|
23
|
-
collection.name !~ /system/
|
24
|
-
end.each(&:drop)
|
25
|
-
|
26
|
-
fill_db(options)
|
27
|
-
end
|
28
|
-
|
29
|
-
# models
|
30
|
-
def anonymous_dummy(&block)
|
31
|
-
Class.new do
|
32
|
-
include Mongoid::Document
|
33
|
-
include SimpleEnum::Mongoid
|
34
|
-
self.collection_name = 'dummies'
|
35
|
-
instance_eval &block
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def extend_computer(current_i18n_name = "Computer", &block)
|
40
|
-
Class.new(Computer) do
|
41
|
-
self.collection_name = 'computers'
|
42
|
-
instance_eval &block
|
43
|
-
instance_eval <<-RUBY
|
44
|
-
def self.model_name; MockName.mock!(#{current_i18n_name.inspect}) end
|
45
|
-
RUBY
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def extend_dummy(current_i18n_name = "Dummy", &block)
|
50
|
-
Class.new(Dummy) do
|
51
|
-
self.collection_name = 'dummies'
|
52
|
-
instance_eval &block
|
53
|
-
instance_eval <<-RUBY
|
54
|
-
def self.model_name; MockName.mock!(#{current_i18n_name.inspect}) end
|
55
|
-
RUBY
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def named_dummy(class_name, &block)
|
60
|
-
begin
|
61
|
-
return class_name.constantize
|
62
|
-
rescue NameError
|
63
|
-
klass = Object.const_set(class_name, Class.new)
|
64
|
-
klass.module_eval do
|
65
|
-
include Mongoid::Document
|
66
|
-
include SimpleEnum::Mongoid
|
67
|
-
|
68
|
-
self.collection_name = 'dummies'
|
69
|
-
instance_eval &block
|
70
|
-
end
|
71
|
-
|
72
|
-
klass
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|
76
|
-
|
77
|
-
class Dummy
|
78
|
-
include Mongoid::Document
|
79
|
-
include SimpleEnum::Mongoid
|
80
|
-
|
81
|
-
as_enum :gender, [:male, :female]
|
82
|
-
as_enum :word, { :alpha => 'alpha', :beta => 'beta', :gamma => 'gamma'}
|
83
|
-
as_enum :didum, [ :foo, :bar, :foobar ], :column => 'other'
|
84
|
-
as_enum :role, [:admin, :member, :anon], :strings => true
|
85
|
-
as_enum :numeric, [:"100", :"3.14"], :strings => true
|
86
|
-
as_enum :nilish, [:nil], :strings => true
|
87
|
-
|
88
|
-
before_save :check_typed
|
89
|
-
|
90
|
-
def check_typed
|
91
|
-
attributes['_type'] = nil unless (self.hereditary? || self.polymorphic?)
|
92
|
-
end
|
93
|
-
|
94
|
-
end
|
95
|
-
|
96
|
-
class Gender
|
97
|
-
include Mongoid::Document
|
98
|
-
include SimpleEnum::Mongoid
|
99
|
-
|
100
|
-
field :name, :type => String
|
101
|
-
end
|
102
|
-
|
103
|
-
class Computer
|
104
|
-
include Mongoid::Document
|
105
|
-
include SimpleEnum::Mongoid
|
106
|
-
|
107
|
-
field :name, :type => String
|
108
|
-
|
109
|
-
as_enum :manufacturer, [:dell, :compaq, :apple]
|
110
|
-
as_enum :operating_system, [:windows, :osx, :linux, :bsd]
|
111
|
-
end
|
112
|
-
|
113
|
-
# Used to test STI stuff
|
114
|
-
class SpecificDummy < Dummy; end
|
data/test/poro_test.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class POROTest < MiniTest::Unit::TestCase
|
4
|
-
class MyPORO
|
5
|
-
include SimpleEnum
|
6
|
-
|
7
|
-
attr_accessor :gender_cd
|
8
|
-
as_enum :gender, [:male, :female]
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_reading_and_writing
|
12
|
-
poro = POROTest::MyPORO.new
|
13
|
-
poro.gender_cd = 1
|
14
|
-
assert_equal :female, poro.gender
|
15
|
-
|
16
|
-
poro.male!
|
17
|
-
assert_equal 0, poro.gender_cd
|
18
|
-
assert_equal :male, poro.gender
|
19
|
-
end
|
20
|
-
end
|