active_enum 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -24,6 +24,13 @@ Define an enum class with values
24
24
  value :id => 2, :name => 'Female'
25
25
  end
26
26
 
27
+ Define with id as the key
28
+
29
+ class Sex < ActiveEnum::Base
30
+ value 1 => 'Male'
31
+ value 2 => 'Female'
32
+ end
33
+
27
34
  Define using implicit id values
28
35
 
29
36
  class Sex < ActiveEnum::Base
data/lib/active_enum.rb CHANGED
@@ -10,9 +10,6 @@ module ActiveEnum
10
10
  mattr_accessor :use_name_as_value
11
11
  self.use_name_as_value = false
12
12
 
13
- mattr_accessor :raise_error_for_not_defined
14
- self.raise_error_for_not_defined = false
15
-
16
13
  class Configuration
17
14
  def enum(name, &block)
18
15
  class_name = name.to_s.camelize
@@ -1,6 +1,6 @@
1
1
  module ActiveEnum
2
2
  class DuplicateValue < StandardError; end
3
- class ValueNotDefined < StandardError; end
3
+ class InvalidValue < StandardError; end
4
4
 
5
5
  class Base
6
6
 
@@ -10,16 +10,17 @@ module ActiveEnum
10
10
  ActiveEnum.enum_classes << subclass
11
11
  end
12
12
 
13
- # :id => 1, :title => 'Foo'
14
- # :title => 'Foo'
13
+ # :name => 'Foo', implicit ID
14
+ # :id => 1, :name => 'Foo'
15
+ # 1 => 'Foo'
15
16
  #
16
- def value(enum_value={})
17
+ def value(enum_value)
17
18
  @values ||= []
18
19
 
19
- id = enum_value[:id] || next_id
20
- check_duplicate(id, enum_value[:name])
20
+ id, name = id_and_name(enum_value)
21
+ check_duplicate(id, name)
21
22
 
22
- @values << [id, enum_value[:name]]
23
+ @values << [id, name]
23
24
  sort_values! unless @order == :as_defined
24
25
  end
25
26
 
@@ -48,11 +49,9 @@ module ActiveEnum
48
49
  def [](index)
49
50
  if index.is_a?(Fixnum)
50
51
  row = lookup_by_id(index)
51
- raise(ActiveEnum::ValueNotDefined, "The id value #{index} is not defined for #{self} enum.") if row.nil? && ActiveEnum.raise_error_for_not_defined
52
52
  row[1] if row
53
53
  else
54
54
  row = lookup_by_name(index)
55
- raise(ActiveEnum::ValueNotDefined, "The name value #{index} is not defined for #{self} enum.") if row.nil? && ActiveEnum.raise_error_for_not_defined
56
55
  row[0] if row
57
56
  end
58
57
  end
@@ -67,6 +66,16 @@ module ActiveEnum
67
66
  @values.rassoc(index.to_s) || @values.rassoc(index.to_s.titleize)
68
67
  end
69
68
 
69
+ def id_and_name(hash)
70
+ if hash.has_key?(:id) || hash.has_key?(:name)
71
+ return (hash[:id] || next_id), hash[:name]
72
+ elsif hash.keys.first.is_a?(Fixnum)
73
+ return *Array(hash).first
74
+ else
75
+ raise ActiveEnum::InvalidValue, "The value supplied, #{hash}, is not a valid format."
76
+ end
77
+ end
78
+
70
79
  def next_id
71
80
  (ids.max || 0) + 1
72
81
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveEnum
2
- VERSION = '0.6.2'
2
+ VERSION = '0.6.3'
3
3
  end
@@ -26,6 +26,13 @@ describe ActiveEnum::Base do
26
26
  enum.all.should == [[1,'Name']]
27
27
  end
28
28
 
29
+ it 'should allow me to define a value as hash with id as key and name as value' do
30
+ enum = define_enum do
31
+ value 1 => 'Name'
32
+ end
33
+ enum.all.should == [[1,'Name']]
34
+ end
35
+
29
36
  it 'should increment value ids when defined without ids' do
30
37
  enum = define_enum do
31
38
  value :name => 'Name 1'
@@ -132,32 +139,6 @@ describe ActiveEnum::Base do
132
139
  enum[:name_1].should == 1
133
140
  end
134
141
 
135
- context "when raise_error_for_not_defined" do
136
- before(:all) do
137
- ActiveEnum.raise_error_for_not_defined = true
138
- end
139
-
140
- it 'should raise error when id value not defined' do
141
- enum = define_enum do
142
- value :id => 1, :name => 'Name 1'
143
- value :id => 2, :name => 'Name 2'
144
- end
145
- lambda { enum[-1] }.should raise_exception(ActiveEnum::ValueNotDefined)
146
- end
147
-
148
- it 'should raise error when name value not defined' do
149
- enum = define_enum do
150
- value :id => 1, :name => 'Name 1'
151
- value :id => 2, :name => 'Name 2'
152
- end
153
- lambda { enum['Name 3'] }.should raise_exception(ActiveEnum::ValueNotDefined)
154
- end
155
-
156
- after(:all) do
157
- ActiveEnum.raise_error_for_not_defined = false
158
- end
159
- end
160
-
161
142
  end
162
143
 
163
144
  it 'should return array for select helpers from to_select' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Meehan
@@ -9,7 +9,7 @@ autorequire: active_enum
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-17 00:00:00 +11:00
12
+ date: 2010-03-18 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies: []
15
15