enumerate_it 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.rdoc +39 -1
- data/lib/enumerate_it.rb +38 -0
- data/lib/enumerate_it/class_methods.rb +16 -0
- data/lib/enumerate_it/version.rb +1 -1
- data/spec/enumerate_it_spec.rb +46 -0
- data/spec/support/test_classes.rb +16 -0
- metadata +5 -5
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -83,7 +83,7 @@ This will create some nice stuff:
|
|
83
83
|
|
84
84
|
* You can retrieve the symbol used to declare a specific enumeration value:
|
85
85
|
|
86
|
-
RelationshipStatus.key_for(
|
86
|
+
RelationshipStatus.key_for(RelationshipStatus::MARRIED) # :married
|
87
87
|
|
88
88
|
* You can iterate over the list of the enumeration's values:
|
89
89
|
|
@@ -197,6 +197,44 @@ This will create:
|
|
197
197
|
p.relationship_status_married? #=> true
|
198
198
|
p.relationship_status_divoced? #=> false
|
199
199
|
|
200
|
+
* You can define polymorphic behavior for the enum values, so you can define a class for each of
|
201
|
+
them:
|
202
|
+
|
203
|
+
class RelationshipStatus < EnumerateIt::Base
|
204
|
+
associate_values :married, :single
|
205
|
+
|
206
|
+
class Married
|
207
|
+
def saturday_night
|
208
|
+
"At home with the kids"
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
class Single
|
213
|
+
def saturday_night
|
214
|
+
"Party Hard!"
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
class Person < ActiveRecord::Base
|
220
|
+
has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_helpers => { :polymorphic => true }
|
221
|
+
end
|
222
|
+
|
223
|
+
p = Person.new
|
224
|
+
p.relationship_status = RelationshipStatus::MARRIED
|
225
|
+
p.relationship_status_object.saturday_night # => "At home with the kids"
|
226
|
+
|
227
|
+
p.relationship_status = RelationshipStatus::SINGLE
|
228
|
+
p.relationship_status_object.saturday_night # => "Party Hard!"
|
229
|
+
|
230
|
+
You can also change the suffix '_object', using the :suffix option:
|
231
|
+
|
232
|
+
class Person < ActiveRecord::Base
|
233
|
+
has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_helpers => { :polymorphic => { :suffix => "_mode" } }
|
234
|
+
end
|
235
|
+
|
236
|
+
p.relationship_status_mode.saturday_night
|
237
|
+
|
200
238
|
* The :create_helpers also creates some mutator helper methods, that can be used to change the attribute's value.
|
201
239
|
|
202
240
|
class Person < ActiveRecord::Base
|
data/lib/enumerate_it.rb
CHANGED
@@ -170,6 +170,44 @@
|
|
170
170
|
# p.relationship_status_married? #=> true
|
171
171
|
# p.relationship_status_divoced? #=> false
|
172
172
|
#
|
173
|
+
# - You can define polymorphic behavior for the enum values, so you can define a class for each of
|
174
|
+
# them:
|
175
|
+
#
|
176
|
+
# class RelationshipStatus < EnumerateIt::Base
|
177
|
+
# associate_values :married, :single
|
178
|
+
#
|
179
|
+
# class Married
|
180
|
+
# def saturday_night
|
181
|
+
# "At home with the kids"
|
182
|
+
# end
|
183
|
+
# end
|
184
|
+
#
|
185
|
+
# class Single
|
186
|
+
# def saturday_night
|
187
|
+
# "Party Hard!"
|
188
|
+
# end
|
189
|
+
# end
|
190
|
+
# end
|
191
|
+
#
|
192
|
+
# class Person < ActiveRecord::Base
|
193
|
+
# has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_helpers => { :polymorphic => true }
|
194
|
+
# end
|
195
|
+
#
|
196
|
+
# p = Person.new
|
197
|
+
# p.relationship_status = RelationshipStatus::MARRIED
|
198
|
+
# p.relationship_status_object.saturday_night # => "At home with the kids"
|
199
|
+
#
|
200
|
+
# p.relationship_status = RelationshipStatus::SINGLE
|
201
|
+
# p.relationship_status_object.saturday_night # => "Party Hard!"
|
202
|
+
#
|
203
|
+
# You can also change the suffix '_object', using the :suffix option:
|
204
|
+
#
|
205
|
+
# class Person < ActiveRecord::Base
|
206
|
+
# has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_helpers => { :polymorphic => { :suffix => "_mode" } }
|
207
|
+
# end
|
208
|
+
#
|
209
|
+
# p.relationship_status_mode.saturday_night
|
210
|
+
#
|
173
211
|
# - If you pass the :create_scopes option as 'true', it will create a scope method for each enumeration option (this option defaults to false):
|
174
212
|
#
|
175
213
|
# class Person < ActiveRecord::Base
|
@@ -12,6 +12,7 @@ module EnumerateIt
|
|
12
12
|
if options[:create_helpers]
|
13
13
|
create_helper_methods options[:with], attribute, options[:create_helpers]
|
14
14
|
create_mutator_methods options[:with], attribute, options[:create_helpers]
|
15
|
+
create_polymorphic_methods options[:with], attribute, options[:create_helpers]
|
15
16
|
end
|
16
17
|
|
17
18
|
if options[:create_scopes]
|
@@ -66,6 +67,21 @@ module EnumerateIt
|
|
66
67
|
end
|
67
68
|
end
|
68
69
|
|
70
|
+
def create_polymorphic_methods(klass, attribute_name, helpers)
|
71
|
+
return unless helpers.is_a?(Hash) && helpers[:polymorphic]
|
72
|
+
options = helpers[:polymorphic]
|
73
|
+
suffix = options.is_a?(Hash) && options[:suffix]
|
74
|
+
suffix ||= "_object"
|
75
|
+
|
76
|
+
class_eval do
|
77
|
+
define_method "#{attribute_name}#{suffix}" do
|
78
|
+
value = self.public_send(attribute_name)
|
79
|
+
|
80
|
+
klass.const_get(klass.key_for(value).to_s.camelize).new if value
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
69
85
|
def define_enumeration_class(attribute, options)
|
70
86
|
if options[:with].nil?
|
71
87
|
inner_enum_class_name = attribute.to_s.camelize.to_sym
|
data/lib/enumerate_it/version.rb
CHANGED
data/spec/enumerate_it_spec.rb
CHANGED
@@ -185,6 +185,52 @@ describe EnumerateIt do
|
|
185
185
|
target.foobar.should == TestEnumeration::VALUE_3
|
186
186
|
end
|
187
187
|
end
|
188
|
+
|
189
|
+
context "with :polymorphic option" do
|
190
|
+
before :each do
|
191
|
+
class Polymorphic
|
192
|
+
extend EnumerateIt
|
193
|
+
attr_accessor :foo
|
194
|
+
has_enumeration_for :foo, :with => PolymorphicEnum, :create_helpers => { :polymorphic => true }
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
it "calls methods on the enum constants' objects" do
|
199
|
+
target = Polymorphic.new
|
200
|
+
target.foo = PolymorphicEnum::NORMAL
|
201
|
+
|
202
|
+
target.foo_object.print("Gol").should == "I'm Normal: Gol"
|
203
|
+
|
204
|
+
target.foo = PolymorphicEnum::CRAZY
|
205
|
+
|
206
|
+
target.foo_object.print("Gol").should == "Whoa!: Gol"
|
207
|
+
end
|
208
|
+
|
209
|
+
it "returns nil if foo is not set" do
|
210
|
+
target = Polymorphic.new
|
211
|
+
|
212
|
+
target.foo_object.should be_nil
|
213
|
+
end
|
214
|
+
|
215
|
+
context "and :suffix" do
|
216
|
+
before :each do
|
217
|
+
class Polymorphic
|
218
|
+
has_enumeration_for :foo, :with => PolymorphicEnum, :create_helpers => { :polymorphic => { :suffix => "_strategy" } }
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
it "calls methods on the enum constants' objects" do
|
223
|
+
target = Polymorphic.new
|
224
|
+
target.foo = PolymorphicEnum::NORMAL
|
225
|
+
|
226
|
+
target.foo_strategy.print("Gol").should == "I'm Normal: Gol"
|
227
|
+
|
228
|
+
target.foo = PolymorphicEnum::CRAZY
|
229
|
+
|
230
|
+
target.foo_strategy.print("Gol").should == "Whoa!: Gol"
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
188
234
|
end
|
189
235
|
|
190
236
|
describe "using the :create_scopes option" do
|
@@ -33,6 +33,22 @@ class Foobar < EnumerateIt::Base
|
|
33
33
|
)
|
34
34
|
end
|
35
35
|
|
36
|
+
class PolymorphicEnum < EnumerateIt::Base
|
37
|
+
associate_values :normal, :crazy
|
38
|
+
|
39
|
+
class Normal
|
40
|
+
def print(msg)
|
41
|
+
"I'm Normal: #{msg}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class Crazy
|
46
|
+
def print(msg)
|
47
|
+
"Whoa!: #{msg}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
36
52
|
class BaseClass
|
37
53
|
extend EnumerateIt
|
38
54
|
has_enumeration_for :foobar, :with => TestEnumeration
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerate_it
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
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-
|
12
|
+
date: 2013-08-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -158,7 +158,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
158
158
|
version: '0'
|
159
159
|
segments:
|
160
160
|
- 0
|
161
|
-
hash:
|
161
|
+
hash: 1153526174871300187
|
162
162
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
163
163
|
none: false
|
164
164
|
requirements:
|
@@ -167,10 +167,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
167
|
version: '0'
|
168
168
|
segments:
|
169
169
|
- 0
|
170
|
-
hash:
|
170
|
+
hash: 1153526174871300187
|
171
171
|
requirements: []
|
172
172
|
rubyforge_project:
|
173
|
-
rubygems_version: 1.8.
|
173
|
+
rubygems_version: 1.8.24
|
174
174
|
signing_key:
|
175
175
|
specification_version: 3
|
176
176
|
summary: Ruby Enumerations
|