simple_enum 1.6.7 → 1.6.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e5699f6a6006961ea0474ccaf7c711e71308c14
4
- data.tar.gz: 2773209fbca30a18661973ff3bd69a83fdc75a63
3
+ metadata.gz: f6a68b2d21b025bcb22c251918ff51b412be8ab6
4
+ data.tar.gz: bbe02918d4165d9bca1917c7dc38981bc0127136
5
5
  SHA512:
6
- metadata.gz: 42ffff4b1ded83914ac64f886d3bf3cca7fd7feea2e2ba7e395067baefb3ad981e930efe3a135c8e9b785499dc1e672493ccc939944ef3a6cf1ec2a4a679e2b6
7
- data.tar.gz: 6b064e2a9af7137cf9c9fc1d46d12139d6ffd4b3919c8f85378781a1771c869bd7c33d99f1a5a1f380c7e2ad6fe8ebe2fc5649f4b8d3e82695c5b32ade4f2825
6
+ metadata.gz: 4ce5b6271ff2a6b391819e544901d2ddfcfd64ff5c6c253b5f28cdafa94e169f9d2baa6ea7bfd0e35e03e270a63c36d8c4fa6c4f878f8926861d67f63fea0a4d
7
+ data.tar.gz: 91ad1ead6ee168f5fdcd08f82f004a232dcd1d48a8406a08f3b7597f000d8c96f0d2b7f10099f1ef0a00d4a099d32c19516fa81310aa532a43db8cd62d938e62
data/README.rdoc CHANGED
@@ -82,6 +82,13 @@ useful when creating queries, displaying option elements or similar:
82
82
  *Disclaimer*: if you _ever_ decide to reorder this array, beaware that any previous mapping is lost. So it's recommended
83
83
  to create mappings (that might change) using hashes instead of arrays. For stuff like gender it might be probably perfectly
84
84
  fine to use arrays though.
85
+ * You can use string values instead of integer values if your database column has the type +string+ or +text+:
86
+
87
+ class User < ActiveRecord::Base
88
+ as_eum :status, [:deleted, :active, :dsiabled], :strings => true
89
+ end
90
+
91
+ User.create!(status: :active) #=> #<User id: 1, status_cd: "active">
85
92
  * Want to use `SimpleEnum` in an ActiveModel, or other class, just do:
86
93
 
87
94
  class MyModel
data/lib/simple_enum.rb CHANGED
@@ -182,6 +182,7 @@ module SimpleEnum
182
182
 
183
183
  # generate setter
184
184
  define_method("#{enum_cd}=") do |new_value|
185
+ new_value = new_value.to_s if options[:strings] && new_value
185
186
  real = new_value.blank? ? nil : values[EnumHash.symbolize(new_value)]
186
187
  real = new_value if real.nil? && values_inverted[new_value].present?
187
188
  raise(ArgumentError, "Invalid enumeration value: #{new_value}") if (options[:whiny] and real.nil? and !new_value.blank?)
@@ -18,6 +18,8 @@ module ActiveModel
18
18
  enum_def = @klass.enum_definitions[attribute]
19
19
  raw_value = record.send(enum_def[:column])
20
20
 
21
+ raw_value = raw_value.to_s if enum_def[:options][:strings] && raw_value
22
+
21
23
  next if (raw_value.nil? && options[:allow_nil]) || (raw_value.blank? && options[:allow_blank])
22
24
 
23
25
  unless @klass.send(enum_def[:name].to_s.pluralize).values.include?(raw_value)
@@ -1,5 +1,5 @@
1
1
  module SimpleEnum
2
2
 
3
3
  # +SimpleEnum+ version string.
4
- VERSION = "1.6.7"
4
+ VERSION = "1.6.8"
5
5
  end
@@ -30,6 +30,8 @@ def reload_db(options = {})
30
30
  t.column :word_cd, :string, :limit => 5
31
31
  t.column :role_cd, :string
32
32
  t.column :other, :integer
33
+ t.column :numeric_cd, :string
34
+ t.column :nilish_cd, :string
33
35
  end
34
36
 
35
37
  # Create ref-data table and fill with records
@@ -65,6 +67,16 @@ def extend_computer(current_i18n_name = "Computer", &block)
65
67
  end
66
68
  end
67
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
+
68
80
  def named_dummy(class_name, &block)
69
81
  begin
70
82
  return class_name.constantize
@@ -83,6 +95,8 @@ class Dummy < ActiveRecord::Base
83
95
  as_enum :word, { :alpha => 'alpha', :beta => 'beta', :gamma => 'gamma'}
84
96
  as_enum :didum, [ :foo, :bar, :foobar ], :column => 'other'
85
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
86
100
  end
87
101
 
88
102
  class Gender < ActiveRecord::Base
data/test/orm/mongoid.rb CHANGED
@@ -46,6 +46,16 @@ def extend_computer(current_i18n_name = "Computer", &block)
46
46
  end
47
47
  end
48
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
+
49
59
  def named_dummy(class_name, &block)
50
60
  begin
51
61
  return class_name.constantize
@@ -72,6 +82,8 @@ class Dummy
72
82
  as_enum :word, { :alpha => 'alpha', :beta => 'beta', :gamma => 'gamma'}
73
83
  as_enum :didum, [ :foo, :bar, :foobar ], :column => 'other'
74
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
75
87
 
76
88
  before_save :check_typed
77
89
 
@@ -44,6 +44,23 @@ class SimpleEnumTest < MiniTest::Unit::TestCase
44
44
  assert_equal(1, d.gender_cd)
45
45
  end
46
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
+
47
64
  def test_setting_value_as_key_in_constructor
48
65
  d = Dummy.new :gender => 1
49
66
  assert_equal(:female, d.gender)
@@ -221,6 +238,15 @@ class SimpleEnumTest < MiniTest::Unit::TestCase
221
238
  assert_equal(true, computer.save)
222
239
  end
223
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
+
224
250
  def test_that_argumenterror_is_raised_if_invalid_symbol_is_passed
225
251
  assert_raises ArgumentError do
226
252
  Dummy.new :gender => :foo
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.7
4
+ version: 1.6.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Westermann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-08 00:00:00.000000000 Z
11
+ date: 2013-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport