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 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
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Irongaze Consulting LLC
1
+ Copyright (c) 2013 Irongaze Consulting LLC
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/Version.txt CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.0.2
data/lib/iron/enum.rb CHANGED
@@ -2,25 +2,24 @@
2
2
  #
3
3
  # Sample usage:
4
4
  #
5
- # Enum.enable_support(:all)
6
- # module Fruit
7
- # enum :apple, 1
8
- # enum :pear, 2
9
- # end
5
+ # module Fruit
6
+ # enum :apple, 1
7
+ # enum :pear, 2
8
+ # end
10
9
  #
11
10
  # Yields:
12
11
  #
13
- # Fruit::APPLE => 1
14
- # Fruit.name(1) => 'Apple'
15
- # Fruit.keys => [:apple, :pear]
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 definition list
21
- KEY_IDX = 0
19
+ # Indexes into our internal enum list
20
+ KEY_IDX = 0
22
21
  VALUE_IDX = 1
23
- NAME_IDX = 2
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 efault to
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:
@@ -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.flatten!
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
@@ -32,5 +32,10 @@ describe Enum do
32
32
  expect { @obj.pos = 4000 }.to raise_error
33
33
  end
34
34
 
35
+ it 'should convert strings that are ints to ints on setting values' do
36
+ @obj.pos = '1'
37
+ @obj.pos.should == 1
38
+ end
39
+
35
40
  end
36
41
  end
@@ -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
@@ -27,6 +27,6 @@ describe Enum do
27
27
  it 'should return select values' do
28
28
  ValueTest.values(:alpha, :gamma).should == [5, 2]
29
29
  end
30
-
30
+
31
31
  end
32
32
  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.1
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-23 00:00:00.000000000 Z
12
+ date: 2013-03-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec