active_enum 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +29 -12
- data/Rakefile +1 -1
- data/lib/active_enum.rb +3 -0
- data/lib/active_enum/base.rb +7 -4
- data/lib/active_enum/version.rb +1 -1
- data/spec/active_enum/base_spec.rb +52 -6
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -35,21 +35,28 @@ Beware that if you change the order of values defined in an enum which don't hav
|
|
35
35
|
This could corrupt your data if the enum values have been stored in a model record, as they will no longer map to
|
36
36
|
the original enum.
|
37
37
|
|
38
|
-
|
38
|
+
Enum class usage
|
39
|
+
|
40
|
+
Sex[1] # => 'Male'
|
41
|
+
Sex['Male'] # => 1
|
42
|
+
Sex[:male] # => 1
|
43
|
+
Sex.to_select # => [['Male', 1], ['Female',2]] for select form helpers
|
44
|
+
|
45
|
+
=== Ordering
|
46
|
+
|
47
|
+
To define the sorting of returned values use the order method. Which is useful for to_select method.
|
39
48
|
|
40
49
|
class Sex < ActiveEnum::Base
|
41
|
-
order :
|
50
|
+
order :asc
|
42
51
|
|
43
52
|
value :id => 1, :name => 'Male'
|
44
53
|
value :id => 2, :name => 'Female'
|
45
54
|
end
|
46
55
|
|
47
|
-
|
56
|
+
By default the order is ascending (:asc) but you can also choose descending (:desc) or in order of definition (:as_defined).
|
57
|
+
The last option is useful when supplying id values but have a specific order needed to display them.
|
48
58
|
|
49
|
-
|
50
|
-
Sex['Male'] # => 1
|
51
|
-
Sex[:male] # => 1
|
52
|
-
Sex.to_select # => [['Male', 1], ['Female',2]] for select form helpers
|
59
|
+
=== Enumerate model attributes
|
53
60
|
|
54
61
|
Use the enum to enumerate an ActiveRecord model attribute
|
55
62
|
|
@@ -79,6 +86,8 @@ Multiple attributes with same enum
|
|
79
86
|
enumerate :to, :from, :with => Sex
|
80
87
|
end
|
81
88
|
|
89
|
+
=== Attribute value lookup
|
90
|
+
|
82
91
|
Access the enum values and the enum class using the attribute method with a symbol for the enum component you want
|
83
92
|
|
84
93
|
user = User.new
|
@@ -89,6 +98,13 @@ Access the enum values and the enum class using the attribute method with a symb
|
|
89
98
|
user.sex(:name) # => 'Male'
|
90
99
|
user.sex(:enum) # => Sex
|
91
100
|
|
101
|
+
You can set the default to return the enum name value for enumerated attribute
|
102
|
+
|
103
|
+
ActiveEnum.use_name_as_value = true
|
104
|
+
user.sex # => 'Male'
|
105
|
+
|
106
|
+
=== Boolean check
|
107
|
+
|
92
108
|
You can check if the attribute value matches a particular enum value by passing the enum value as an argument to the question method
|
93
109
|
|
94
110
|
user.sex?(:male) # => true
|
@@ -96,14 +112,13 @@ You can check if the attribute value matches a particular enum value by passing
|
|
96
112
|
user.sex?('Male') # => true
|
97
113
|
user.sex?('Female') # => false
|
98
114
|
|
99
|
-
|
115
|
+
=== Enum lookup
|
100
116
|
|
101
|
-
|
117
|
+
A convenience method on the class is available to the enum class of any enumerated attribute
|
102
118
|
|
103
|
-
|
119
|
+
User.enum_for(:sex) # => Sex
|
104
120
|
|
105
|
-
|
106
|
-
user.sex # => 'Male'
|
121
|
+
=== Bulk definition
|
107
122
|
|
108
123
|
Define enum classes in bulk without class files, in an initializer file for example.
|
109
124
|
|
@@ -125,6 +140,8 @@ Define enum classes in bulk without class files, in an initializer file for exam
|
|
125
140
|
|
126
141
|
All defined enum classes are stored in ActiveEnum.enum_classes array if you need look them up or iterate over them.
|
127
142
|
|
143
|
+
=== Model as enum or acts_as_enum
|
144
|
+
|
128
145
|
You can make an existing model class behave like an enum class with acts_as_enum
|
129
146
|
|
130
147
|
class User < ActiveRecord::Base
|
data/Rakefile
CHANGED
data/lib/active_enum.rb
CHANGED
@@ -10,6 +10,9 @@ 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
|
+
|
13
16
|
class Configuration
|
14
17
|
def enum(name, &block)
|
15
18
|
class_name = name.to_s.camelize
|
data/lib/active_enum/base.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
module ActiveEnum
|
2
2
|
class DuplicateValue < StandardError; end
|
3
|
+
class ValueNotDefined < StandardError; end
|
3
4
|
|
4
5
|
class Base
|
5
6
|
|
6
7
|
class << self
|
7
|
-
|
8
|
+
|
8
9
|
def inherited(subclass)
|
9
10
|
ActiveEnum.enum_classes << subclass
|
10
11
|
end
|
@@ -13,13 +14,13 @@ module ActiveEnum
|
|
13
14
|
# :title => 'Foo'
|
14
15
|
#
|
15
16
|
def value(enum_value={})
|
16
|
-
@values ||= []
|
17
|
+
@values ||= []
|
17
18
|
|
18
19
|
id = enum_value[:id] || next_id
|
19
20
|
check_duplicate(id, enum_value[:name])
|
20
21
|
|
21
22
|
@values << [id, enum_value[:name]]
|
22
|
-
sort_values!
|
23
|
+
sort_values! unless @order == :as_defined
|
23
24
|
end
|
24
25
|
|
25
26
|
# order enum values using :asc or :desc
|
@@ -47,9 +48,11 @@ module ActiveEnum
|
|
47
48
|
def [](index)
|
48
49
|
if index.is_a?(Fixnum)
|
49
50
|
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
|
50
52
|
row[1] if row
|
51
53
|
else
|
52
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
|
53
56
|
row[0] if row
|
54
57
|
end
|
55
58
|
end
|
@@ -63,7 +66,7 @@ module ActiveEnum
|
|
63
66
|
def lookup_by_name(index)
|
64
67
|
@values.rassoc(index.to_s) || @values.rassoc(index.to_s.titleize)
|
65
68
|
end
|
66
|
-
|
69
|
+
|
67
70
|
def next_id
|
68
71
|
(ids.max || 0) + 1
|
69
72
|
end
|
data/lib/active_enum/version.rb
CHANGED
@@ -60,13 +60,33 @@ describe ActiveEnum::Base do
|
|
60
60
|
enum.all.first[0].should == 1
|
61
61
|
end
|
62
62
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
63
|
+
context "sorting" do
|
64
|
+
it 'should return values ascending by default' do
|
65
|
+
enum = define_enum do
|
66
|
+
value :id => 2, :name => 'Name 2'
|
67
|
+
value :id => 1, :name => 'Name 1'
|
68
|
+
end
|
69
|
+
enum.all.should == [[1,'Name 1'], [2, 'Name 2']]
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'should return sorted values by id using order setting' do
|
73
|
+
enum = define_enum do
|
74
|
+
order :desc
|
75
|
+
value :id => 1, :name => 'Name 1'
|
76
|
+
value :id => 2, :name => 'Name 2'
|
77
|
+
end
|
78
|
+
enum.all.should == [[2, 'Name 2'], [1,'Name 1']]
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'should return sorted values by id using order setting' do
|
82
|
+
enum = define_enum do
|
83
|
+
order :as_defined
|
84
|
+
value :id => 3, :name => 'Name 3'
|
85
|
+
value :id => 1, :name => 'Name 1'
|
86
|
+
value :id => 2, :name => 'Name 2'
|
87
|
+
end
|
88
|
+
enum.all.should == [[3,'Name 3'], [1,'Name 1'], [2, 'Name 2']]
|
68
89
|
end
|
69
|
-
enum.all.first[0].should == 2
|
70
90
|
end
|
71
91
|
|
72
92
|
it 'should return array of ids' do
|
@@ -112,6 +132,32 @@ describe ActiveEnum::Base do
|
|
112
132
|
enum[:name_1].should == 1
|
113
133
|
end
|
114
134
|
|
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
|
+
|
115
161
|
end
|
116
162
|
|
117
163
|
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.
|
4
|
+
version: 0.6.2
|
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-
|
12
|
+
date: 2010-03-17 00:00:00 +11:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|