iron-enum 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ == 1.0.3 / 2013-06-21
2
+
3
+ * Minor bugfix to allow better handling of setting an enum attr with a blank string as if it were setting to nil
4
+
1
5
  == 1.0.2 / 2013-03-25
2
6
 
3
7
  * Improve edge cases for Enum::Core#options to correctly return [] for nil and [] explicit parameters
data/Version.txt CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.0.3
@@ -122,6 +122,7 @@ module Enum
122
122
  end
123
123
 
124
124
  def #{attr_field}=(val)
125
+ val = nil if val.is_a?(String) && val.empty?
125
126
  @#{attr_field} = #{enum_klass}.value(val)
126
127
  end
127
128
  eos
@@ -67,8 +67,18 @@ module Enum
67
67
  private
68
68
 
69
69
  def to_key(id)
70
+ return nil if id.nil?
70
71
  return id if id.is_a?(Symbol)
71
- id = id.to_i if id.is_a?(String) && id.to_i.to_s == id
72
+ if id.is_a?(String)
73
+ # Check for "15" style ids - common in web usage a la Rails where params come in as text
74
+ if id.to_i.to_s == id
75
+ # Yup, convert
76
+ id = id.to_i
77
+ else
78
+ # No, so invalid
79
+ return nil
80
+ end
81
+ end
72
82
  row = enum_list.find {|row| row[VALUE_IDX] == id}
73
83
  row.nil? ? nil : row[KEY_IDX]
74
84
  end
@@ -36,6 +36,16 @@ describe Enum do
36
36
  @obj.pos = '1'
37
37
  @obj.pos.should == 1
38
38
  end
39
+
40
+ it 'should set empty strings as nil' do
41
+ @obj.pos = ''
42
+ @obj.pos.should be_nil
43
+ end
44
+
45
+ it 'should raise on setting to invalid strings' do
46
+ expect {@obj.pos = 'abc'}.to raise_error
47
+ expect {@obj.pos = '1a'}.to raise_error
48
+ end
39
49
 
40
50
  end
41
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iron-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-25 00:00:00.000000000 Z
12
+ date: 2013-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec