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.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -0
  3. data/LICENSE +1 -1
  4. data/README.md +248 -0
  5. data/Rakefile +4 -21
  6. data/lib/simple_enum/accessors/accessor.rb +55 -0
  7. data/lib/simple_enum/accessors/ignore_accessor.rb +11 -0
  8. data/lib/simple_enum/accessors/whiny_accessor.rb +12 -0
  9. data/lib/simple_enum/accessors.rb +18 -0
  10. data/lib/simple_enum/attribute.rb +77 -0
  11. data/lib/simple_enum/enum.rb +37 -0
  12. data/lib/simple_enum/hasher.rb +26 -0
  13. data/lib/simple_enum/mongoid.rb +11 -15
  14. data/lib/simple_enum/translation.rb +17 -0
  15. data/lib/simple_enum/version.rb +2 -2
  16. data/lib/simple_enum.rb +19 -276
  17. data/simple_enum.gemspec +9 -9
  18. data/spec/simple_enum/accessors_spec.rb +212 -0
  19. data/spec/simple_enum/attribute_spec.rb +208 -0
  20. data/spec/simple_enum/enum_spec.rb +96 -0
  21. data/spec/simple_enum/hasher_spec.rb +63 -0
  22. data/spec/simple_enum/mongoid_spec.rb +44 -0
  23. data/spec/simple_enum/translation_spec.rb +66 -0
  24. data/spec/spec_helper.rb +27 -0
  25. data/spec/support/active_record_support.rb +23 -0
  26. data/spec/support/i18n_support.rb +12 -0
  27. data/spec/support/model_support.rb +47 -0
  28. data/spec/support/mongoid_support.rb +21 -0
  29. metadata +50 -56
  30. data/README.rdoc +0 -293
  31. data/lib/simple_enum/enum_hash.rb +0 -64
  32. data/lib/simple_enum/validation.rb +0 -58
  33. data/locales/en.yml +0 -10
  34. data/test/array_conversions_test.rb +0 -21
  35. data/test/class_methods_test.rb +0 -114
  36. data/test/dirty_attributes_test.rb +0 -37
  37. data/test/enum_hash_test.rb +0 -73
  38. data/test/finders_test.rb +0 -45
  39. data/test/locales.yml +0 -25
  40. data/test/mongoid_test.rb +0 -66
  41. data/test/object_backed_test.rb +0 -61
  42. data/test/orm/active_record.rb +0 -114
  43. data/test/orm/common.rb +0 -23
  44. data/test/orm/mongoid.rb +0 -114
  45. data/test/poro_test.rb +0 -20
  46. data/test/prefixes_test.rb +0 -36
  47. data/test/simple_enum_test.rb +0 -314
  48. data/test/test_helper.rb +0 -40
  49. data/test/without_shortcuts_test.rb +0 -39
@@ -1,36 +0,0 @@
1
- require 'test_helper'
2
-
3
- class PrefixesTest < MiniTest::Unit::TestCase
4
- def setup
5
- reload_db
6
- end
7
-
8
- def test_that_gender_is_prefixed_in_getters_and_setters
9
- with_prefix = anonymous_dummy do
10
- as_enum :gender, [:male, :female], :prefix => true
11
- end
12
-
13
- d = with_prefix.new :gender => :male
14
- assert_respond_to d, :gender_male?
15
- assert_respond_to d, :gender_male!
16
- assert_respond_to d, :gender_female?
17
- assert_respond_to d, :gender_female!
18
-
19
- # just ensure that it DOES NOT RESPOND TO good old male!
20
- assert !d.respond_to?(:male!)
21
- end
22
-
23
- def test_that_custom_prefix_is_applied_to_getters_and_setters
24
- with_string_prefix = anonymous_dummy do
25
- as_enum :gender, [:male, :female], :prefix => 'didum'
26
- end
27
-
28
- d = with_string_prefix.new :gender => :female
29
- assert_respond_to d, :didum_female?
30
- assert_respond_to d, :didum_female!
31
-
32
- # just check wheter the results are still correct :)
33
- assert d.didum_female?
34
- assert !d.didum_male?
35
- end
36
- end
@@ -1,314 +0,0 @@
1
- require 'test_helper'
2
-
3
- class SimpleEnumTest < MiniTest::Unit::TestCase
4
- def setup
5
- reload_db
6
- end
7
-
8
- def test_reading_public_enum_definitions
9
- assert_equal "gender_cd", Dummy.enum_definitions[:gender][:column]
10
- end
11
-
12
- def test_enum_definitions_only_available_from_class
13
- assert_raises(NoMethodError) { Dummy.new.enum_definitions }
14
- assert_raises(NoMethodError) { Dummy.new.enum_definitions= {} }
15
- end
16
-
17
- def test_enum_definitions_local_to_model
18
- assert_equal nil, Computer.enum_definitions[:gender]
19
- end
20
-
21
- def test_getting_the_correct_integer_values_when_setting_to_symbol
22
- d = Dummy.new
23
- d.gender = :male
24
- assert_equal(0, d.gender_cd)
25
- end
26
-
27
- def test_getting_the_correct_symbold_when_setting_the_integer_value
28
- d = Dummy.new
29
- d.gender_cd = 1
30
- assert_equal(:female, d.gender)
31
- end
32
-
33
- def test_that_checker_returns_correct_result
34
- d = Dummy.new
35
- d.gender = :male
36
- assert_equal(true, d.male?)
37
- assert_equal(false, d.female?)
38
- end
39
-
40
- def test_setting_value_as_key
41
- d = Dummy.new
42
- d.gender = 1
43
- assert_equal(:female, d.gender)
44
- assert_equal(1, d.gender_cd)
45
- end
46
-
47
- def test_setting_value_when_it_is_not_a_string_and_strings_is_true
48
- d = Dummy.new
49
- d.numeric = 100
50
- assert_equal(:"100", d.numeric)
51
- assert_equal("100", d.numeric_cd)
52
- d.numeric = 3.14
53
- assert_equal(:"3.14", d.numeric)
54
- assert_equal("3.14", d.numeric_cd)
55
- end
56
-
57
- def test_setting_value_to_nil_when_enum_has_nil_as_symbol_and_strings_is_true
58
- d = Dummy.new
59
- d.nilish = nil
60
- assert_equal(nil, d.nilish)
61
- assert_equal(nil, d.nilish_cd)
62
- end
63
-
64
- def test_setting_value_as_key_in_constructor
65
- d = Dummy.new :gender => 1
66
- assert_equal(:female, d.gender)
67
- assert_equal(1, d.gender_cd)
68
- end
69
-
70
- def test_enum_comparisons
71
- d = Dummy.new
72
- assert_equal(false, d.gender?)
73
- d.gender = :male
74
- assert_equal(true, d.gender?)
75
- assert_equal(true, d.gender?(:male))
76
- assert_equal(false, d.gender?(:female))
77
- assert_equal(false, d.gender?(:whot))
78
- d.gender = :female
79
- assert_equal(true, d.gender?(:female))
80
- assert_equal(false, d.gender?(:male))
81
- end
82
-
83
- def test_enum_comparisons_with_strings
84
- d = Dummy.new(:gender => :male)
85
- assert_equal(true, d.gender?("male"))
86
- end
87
-
88
- def test_enum_comparisons_with_nil_always_returns_false
89
- d = Dummy.new(:gender => :male)
90
- assert_equal(false, d.gender?(nil))
91
- end
92
-
93
- def test_getting_symbol_when_data_is_fetched_from_datasource
94
- dummies = Dummy.all
95
-
96
- # Anna
97
- assert_equal(:female, dummies[0].gender)
98
- assert_equal(:alpha, dummies[0].word)
99
- assert_equal(:foo, dummies[0].didum)
100
-
101
- # Bella
102
- assert_equal(true, dummies[1].female?)
103
- assert_equal(true, dummies[1].beta?)
104
- assert_equal(:bar, dummies[1].didum)
105
-
106
- # Chris
107
- assert_equal(false, dummies[2].female?)
108
- assert_equal(:gamma, dummies[2].word)
109
- assert_equal(:foobar, dummies[2].didum)
110
- end
111
-
112
- def test_creating_and_saving_a_new_datasource_object_then_test_symbols
113
- d = Dummy.create({ :name => 'Dummy', :gender_cd => 0 }) # :gender => male
114
- assert_equal(true, d.male?)
115
-
116
- # change :gender_cd to 1
117
- d.female!
118
- d.save!
119
- assert_equal(true, Dummy.find(d.id).female?)
120
- end
121
-
122
- def test_validation_if
123
- validate_if_comp = extend_computer do
124
- validates_as_enum :manufacturer, :if => lambda { |computer|
125
- computer.name == "Fred"
126
- }
127
- end
128
-
129
- computer = validate_if_comp.new(:manufacturer_cd => 48328432)
130
-
131
- computer.name = nil
132
- assert_equal(true, computer.save)
133
-
134
- computer.name = "Fred"
135
- assert_equal(false, computer.save)
136
- end
137
-
138
- def test_validation_unless
139
- validate_unless_comp = extend_computer do
140
- validates_as_enum :manufacturer, :unless => lambda { |computer|
141
- computer.name == "Unless"
142
- }
143
- end
144
-
145
- computer = validate_unless_comp.new(:manufacturer_cd => 48328432)
146
-
147
- computer.name = nil
148
- assert_equal(false, computer.save)
149
- assert_equal(1, computer.errors[:manufacturer].size)
150
-
151
- computer.name = "Unless"
152
- assert_equal(true, computer.save)
153
- end
154
-
155
- def test_validation_on_update
156
- validate_update_comp = extend_computer do
157
- validates_as_enum :manufacturer, :on => :update
158
- end
159
-
160
- computer = validate_update_comp.new(:manufacturer_cd => nil)
161
- assert_equal(true, computer.save)
162
-
163
- computer.name = 'Something else'
164
- assert_equal(false, computer.save)
165
- assert_equal(1, computer.errors[:manufacturer].size)
166
- end
167
-
168
- def test_validation_on_create
169
- validate_create_comp = extend_computer do
170
- validates_as_enum :manufacturer, :on => :create
171
- end
172
-
173
- computer = validate_create_comp.new(:manufacturer_cd => nil)
174
- assert_equal(false, computer.save)
175
- assert_equal(1, computer.errors[:manufacturer].size)
176
-
177
- computer.manufacturer = :apple
178
- assert_equal(true, computer.save)
179
-
180
- computer.manufacturer = nil
181
- assert_equal(true, computer.save)
182
- end
183
-
184
- def test_validation_allow_nil
185
- validate_nil_comp = extend_computer do
186
- validates_as_enum :manufacturer, :allow_nil => true
187
- end
188
-
189
- computer = validate_nil_comp.new(:manufacturer_cd => nil)
190
- assert_equal(true, computer.save)
191
-
192
- computer.manufacturer = :apple
193
- assert_equal(true, computer.save)
194
-
195
- computer.manufacturer_cd = 84321483219
196
- assert_equal(false, computer.save)
197
- assert_equal(1, computer.errors[:manufacturer].size)
198
- end
199
-
200
- def test_default_error_messages_using_translations
201
- validated_comp = extend_computer("ValidatedComputer") do
202
- validates_as_enum :manufacturer
203
- validates_as_enum :operating_system
204
- end
205
-
206
- computer = validated_comp.new
207
- assert !computer.save, "save should return false"
208
- assert_equal "invalid option supplied", computer.errors[:manufacturer].first
209
- assert_equal "y u no os?", computer.errors[:operating_system].first
210
- end
211
-
212
- def test_allow_setting_custom_error_via_message
213
- validate_msg_comp = extend_computer do
214
- validates_as_enum :manufacturer, :message => "invalid manufacturer"
215
- end
216
-
217
- computer = validate_msg_comp.new
218
- assert !computer.valid?, "valid? should return false"
219
- assert_equal "invalid manufacturer", computer.errors[:manufacturer].first
220
- end
221
-
222
- def test_validator
223
- validator_comp = extend_computer do
224
- validates :manufacturer, :operating_system, :as_enum => true
225
- end
226
-
227
- computer = validator_comp.new
228
- assert !computer.save, "save should return false"
229
- assert_equal(1, computer.errors[:manufacturer].size)
230
- assert_equal(1, computer.errors[:operating_system].size)
231
-
232
- computer.manufacturer_cd = 84321483219
233
- assert !computer.save, "save should return false"
234
- assert_equal(1, computer.errors[:manufacturer].size)
235
-
236
- computer.manufacturer_cd = 0
237
- computer.operating_system_cd = 0
238
- assert_equal(true, computer.save)
239
- end
240
-
241
- def test_validator_allows_symbols_as_raw_colum_value_if_strings_is_true
242
- validated_dummy = extend_dummy do
243
- validates_as_enum :role
244
- end
245
-
246
- d = validated_dummy.new :role_cd => :admin
247
- assert d.valid?, "valid? should return true"
248
- end
249
-
250
- def test_that_argumenterror_is_raised_if_invalid_symbol_is_passed
251
- assert_raises ArgumentError do
252
- Dummy.new :gender => :foo
253
- end
254
- end
255
-
256
- def test_that_no_argumenterror_is_raised_if_whiny_is_false
257
- not_whiny = Class.new(Dummy) do
258
- as_enum :gender, [:male, :female], :whiny => false
259
- end
260
-
261
- d = not_whiny.new :gender => :foo
262
- assert_nil(d.gender)
263
- d.gender = ''
264
- assert_nil(d.gender)
265
- end
266
-
267
- def test_that_setting_to_nil_works_if_whiny_is_true_or_false
268
- d = Dummy.new :gender => :male
269
- assert_equal(:male, d.gender)
270
- d.gender = nil
271
- assert_nil(d.gender)
272
- d.gender = ''
273
- assert_nil(d.gender)
274
-
275
- not_whiny_again = Class.new(Dummy) do
276
- as_enum :gender, [:male, :female], :whiny => false
277
- end
278
-
279
- d = not_whiny_again.new :gender => :male
280
- assert_equal(:male, d.gender)
281
- d.gender = nil
282
- assert_nil(d.gender)
283
- d.gender = ''
284
- assert_nil(d.gender)
285
- end
286
-
287
- def test_argument_error_is_raised_when_using_enum_name_eq_column_name
288
- begin
289
- invalid_dummy = anonymous_dummy do
290
- as_enum :gender_cd, [:male, :female], :column => "gender_cd"
291
- end
292
- assert false, "no error raised"
293
- rescue ArgumentError => e
294
- assert e.to_s =~ /use different names for/, "invalid ArgumentError raised"
295
- end
296
- end
297
-
298
- def test_human_name_for_nil_value
299
- d = Dummy.new
300
- assert_nil(d.human_gender)
301
- end
302
-
303
- def test_strings_option
304
- d = Dummy.new :role => :anon
305
-
306
- assert_equal :anon, d.role
307
- assert_equal 'anon', d.role_cd
308
-
309
- d.role = :admin
310
-
311
- assert_equal :admin, d.role
312
- assert_equal 'admin', d.role_cd
313
- end
314
- end
data/test/test_helper.rb DELETED
@@ -1,40 +0,0 @@
1
- # Setup environment for both tests and IRB interactive console
2
- #
3
- $KCODE = 'u' if RUBY_VERSION =~ /^1\.8/ # to make parameterize work...
4
-
5
- require 'rubygems'
6
- require 'bundler/setup'
7
-
8
- require 'minitest/autorun'
9
- require 'active_support'
10
- require 'active_support/version'
11
-
12
- # setup fake rails env
13
- ROOT = File.join(File.dirname(__FILE__), '..')
14
- RAILS_ROOT = ROOT
15
- RAILS_ENV = 'test'
16
-
17
- # load orms
18
- ORM = ENV['SIMPLE_ENUM_TEST_ORM'] || 'active_record'
19
-
20
- def mongoid?; return ORM == 'mongoid';end
21
- def activerecord?; return ORM == 'active_record';end
22
-
23
- # load database implemntation
24
- require ORM
25
-
26
- # load simple enum AFTER ORM
27
- require 'simple_enum'
28
-
29
- # load ORM specific stuff
30
- require 'orm/common'
31
- require "orm/#{ORM}"
32
-
33
- # Add locales
34
- I18n.load_path << File.join(File.dirname(__FILE__), 'locales.yml')
35
-
36
- # setup db
37
- setup_db
38
-
39
- # Test environment info
40
- puts "Testing against: activesupport-#{ActiveSupport::VERSION::STRING}, #{ORM.to_s}-#{orm_version}"
@@ -1,39 +0,0 @@
1
- require 'test_helper'
2
-
3
- class WithoutShortcutsTest < MiniTest::Unit::TestCase
4
- def setup
5
- reload_db
6
-
7
- named_dummy('SlimDummy') do
8
- as_enum :gender, [:male, :female], :slim => true
9
- end
10
- end
11
-
12
- def test_that_no_shortcut_methods_are_generated_if_slime_is_true
13
- jane = SlimDummy.new
14
- jane.gender = :female
15
-
16
- # ensure that other methods still work as expected
17
- assert_equal 1, jane.gender_cd
18
- assert_equal :female, jane.gender
19
-
20
- # then check for availability of shortcut methods
21
- assert !jane.respond_to?(:male!), "should not respond_to <male!>"
22
- assert !jane.respond_to?(:female?), "should not respond_to <female?>"
23
- end
24
-
25
- def test_that_saving_and_loading_from_datasource_works_even_if_slim_is_true
26
- anna = SlimDummy.where(:name => 'Anna').first
27
-
28
- assert_equal 1, anna.gender_cd
29
- assert_equal :female, anna.gender
30
-
31
- # change anna, save + reload
32
- anna.gender = :male
33
- anna.save!
34
- anna.reload
35
-
36
- assert_equal 0, anna.gender_cd
37
- assert_equal :male, anna.gender
38
- end
39
- end