iron-enum 1.0.1 → 1.0.2
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 +5 -0
- data/LICENSE +1 -1
- data/Version.txt +1 -1
- data/lib/iron/enum.rb +12 -13
- data/lib/iron/enum/core.rb +3 -8
- data/spec/enum/attr_spec.rb +5 -0
- data/spec/enum/option_spec.rb +8 -0
- data/spec/enum/value_spec.rb +1 -1
- metadata +2 -2
data/History.txt
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
== 1.0.2 / 2013-03-25
|
2
|
+
|
3
|
+
* Improve edge cases for Enum::Core#options to correctly return [] for nil and [] explicit parameters
|
4
|
+
* Add type coercion to enum_attr setter to allow setting via string iff that string represents a valid value in string form
|
5
|
+
|
1
6
|
== 1.0.1 / 2013-03-23
|
2
7
|
|
3
8
|
* Add validation of inclusion for ActiveRecord enum_attr owners
|
data/LICENSE
CHANGED
data/Version.txt
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.2
|
data/lib/iron/enum.rb
CHANGED
@@ -2,25 +2,24 @@
|
|
2
2
|
#
|
3
3
|
# Sample usage:
|
4
4
|
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
# end
|
5
|
+
# module Fruit
|
6
|
+
# enum :apple, 1
|
7
|
+
# enum :pear, 2
|
8
|
+
# end
|
10
9
|
#
|
11
10
|
# Yields:
|
12
11
|
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
# etc...
|
12
|
+
# Fruit::APPLE => 1
|
13
|
+
# Fruit.name(1) => 'Apple'
|
14
|
+
# Fruit.keys => [:apple, :pear]
|
17
15
|
#
|
16
|
+
# etc...
|
18
17
|
module Enum
|
19
18
|
|
20
|
-
# Indexes into our
|
21
|
-
KEY_IDX =
|
19
|
+
# Indexes into our internal enum list
|
20
|
+
KEY_IDX = 0
|
22
21
|
VALUE_IDX = 1
|
23
|
-
NAME_IDX =
|
22
|
+
NAME_IDX = 2
|
24
23
|
|
25
24
|
# Legacy method of enum creation. Call with a set of arrays, one for each desired enum. Arrays should
|
26
25
|
# contain the parameters supported by #enum, e.g. [:key, <value>, "<optional name>"]
|
@@ -31,7 +30,7 @@ module Enum
|
|
31
30
|
end
|
32
31
|
|
33
32
|
# Add an enumerated constant to the given module/class. The key should be a symbol, the value a fixed integer
|
34
|
-
# that the symbol represents. The name is an optional user-friendly name for the enum, which will
|
33
|
+
# that the symbol represents. The name is an optional user-friendly name for the enum, which will default to
|
35
34
|
# a capitalized version of the key.
|
36
35
|
#
|
37
36
|
# Sample usage:
|
data/lib/iron/enum/core.rb
CHANGED
@@ -49,14 +49,7 @@ module Enum
|
|
49
49
|
# Used for select tag options, optionally pass set of keys/ids to include, eg if there are only
|
50
50
|
# a subset that would be valid for selection in a given context.
|
51
51
|
def options(*included)
|
52
|
-
included.
|
53
|
-
if included.empty?
|
54
|
-
# All options
|
55
|
-
enum_list.collect {|row| option_for(row.first)}
|
56
|
-
else
|
57
|
-
# Only the specified ones
|
58
|
-
included.collect {|key| option_for(key)}.compact
|
59
|
-
end
|
52
|
+
rows_for(*included).collect {|row| option_for(row[KEY_IDX])}
|
60
53
|
end
|
61
54
|
|
62
55
|
# Array in order required by select fields
|
@@ -75,6 +68,7 @@ module Enum
|
|
75
68
|
|
76
69
|
def to_key(id)
|
77
70
|
return id if id.is_a?(Symbol)
|
71
|
+
id = id.to_i if id.is_a?(String) && id.to_i.to_s == id
|
78
72
|
row = enum_list.find {|row| row[VALUE_IDX] == id}
|
79
73
|
row.nil? ? nil : row[KEY_IDX]
|
80
74
|
end
|
@@ -87,6 +81,7 @@ module Enum
|
|
87
81
|
end
|
88
82
|
|
89
83
|
def rows_for(*included)
|
84
|
+
return [] if included.count == 1 && (included.first == [] || included.first == nil)
|
90
85
|
included.flatten!
|
91
86
|
if included.empty?
|
92
87
|
# All enums
|
data/spec/enum/attr_spec.rb
CHANGED
data/spec/enum/option_spec.rb
CHANGED
@@ -16,5 +16,13 @@ describe Enum do
|
|
16
16
|
OptionTest.options(:gamma, :beta).should == [['Monkeys', 20], ['Beta', 10]]
|
17
17
|
end
|
18
18
|
|
19
|
+
it 'should return no options for nil' do
|
20
|
+
OptionTest.options(nil).should == []
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should return no options for empty set' do
|
24
|
+
OptionTest.options([]).should == []
|
25
|
+
end
|
26
|
+
|
19
27
|
end
|
20
28
|
end
|
data/spec/enum/value_spec.rb
CHANGED
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.
|
4
|
+
version: 1.0.2
|
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-
|
12
|
+
date: 2013-03-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|