power_enum 1.1.0 → 1.1.1
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.tar.gz.sig +0 -0
- data/README.markdown +1 -1
- data/lib/active_record/virtual_enumerations.rb +24 -2
- data/lib/power_enum.rb +3 -19
- data/lib/power_enum/enumerated.rb +43 -38
- metadata +6 -3
- metadata.gz.sig +0 -0
data.tar.gz.sig
CHANGED
Binary file
|
data/README.markdown
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
https://github.com/albertosaurus/power_enum
|
4
4
|
|
5
5
|
[](https://travis-ci.org/albertosaurus/power_enum)
|
6
|
-
[](https://codeclimate.com/github/albertosaurus/power_enum)
|
7
7
|
|
8
8
|
Enumerations for Rails 3.1/3.2 Done Right.
|
9
9
|
|
@@ -54,13 +54,35 @@ module ActiveRecord # :nodoc:
|
|
54
54
|
#
|
55
55
|
# config.define :booking_status, :connector_type, :color, :order => :name
|
56
56
|
#
|
57
|
-
#
|
57
|
+
# Single Table Inheritance is also supported:
|
58
58
|
#
|
59
59
|
# Example:
|
60
60
|
#
|
61
61
|
# config.define :base_enum, :name_column => ;foo
|
62
62
|
# config.define :booking_status, :connector_type, :color, :extends => :base_enum
|
63
|
-
module VirtualEnumerations
|
63
|
+
module VirtualEnumerations
|
64
|
+
|
65
|
+
# Patches Module#const_missing to enable us to dynamically create enum
|
66
|
+
# classes at runtime.
|
67
|
+
def self.patch_const_lookup
|
68
|
+
|
69
|
+
# patch Module to support VirtualEnumerations
|
70
|
+
::Module.module_eval do
|
71
|
+
|
72
|
+
alias_method :enumerations_original_const_missing, :const_missing
|
73
|
+
|
74
|
+
# Override const_missing to see if VirtualEnumerations can create it.
|
75
|
+
def const_missing(const_id)
|
76
|
+
# let rails have a go at loading it
|
77
|
+
enumerations_original_const_missing(const_id)
|
78
|
+
rescue NameError
|
79
|
+
# now it's our turn
|
80
|
+
ActiveRecord::VirtualEnumerations.synthesize_if_defined(const_id) or raise
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
64
86
|
|
65
87
|
# Defines enumeration classes. Passes a config object to the given block
|
66
88
|
# which is used to define the virtual enumerations. Call config.define for
|
data/lib/power_enum.rb
CHANGED
@@ -14,27 +14,11 @@ class PowerEnum < Rails::Engine
|
|
14
14
|
include PowerEnum::Schema::SchemaStatements
|
15
15
|
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
include PowerEnum::Migration::CommandRecorder
|
20
|
-
end
|
17
|
+
ActiveRecord::Migration::CommandRecorder.class_eval do
|
18
|
+
include PowerEnum::Migration::CommandRecorder
|
21
19
|
end
|
22
20
|
|
23
|
-
|
24
|
-
::Module.module_eval do
|
25
|
-
|
26
|
-
alias_method :enumerations_original_const_missing, :const_missing
|
27
|
-
|
28
|
-
# Override const_missing to see if VirtualEnumerations can create it.
|
29
|
-
def const_missing(const_id)
|
30
|
-
# let rails have a go at loading it
|
31
|
-
enumerations_original_const_missing(const_id)
|
32
|
-
rescue NameError
|
33
|
-
# now it's our turn
|
34
|
-
ActiveRecord::VirtualEnumerations.synthesize_if_defined(const_id) or raise
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
21
|
+
ActiveRecord::VirtualEnumerations.patch_const_lookup
|
38
22
|
end
|
39
23
|
|
40
24
|
end
|
@@ -143,13 +143,6 @@ module PowerEnum::Enumerated
|
|
143
143
|
@all = load_all.collect{|val| val.freeze}.freeze
|
144
144
|
end
|
145
145
|
|
146
|
-
def load_all
|
147
|
-
conditions = self.acts_enumerated_conditions
|
148
|
-
order = self.acts_enumerated_order
|
149
|
-
where(conditions).order(order)
|
150
|
-
end
|
151
|
-
private :load_all
|
152
|
-
|
153
146
|
# Returns all the active enum values. See the 'active?' instance method.
|
154
147
|
def active
|
155
148
|
return @all_active if @all_active
|
@@ -176,22 +169,7 @@ module PowerEnum::Enumerated
|
|
176
169
|
nil
|
177
170
|
when 1
|
178
171
|
arg = args.first
|
179
|
-
|
180
|
-
when Symbol
|
181
|
-
return_val = lookup_name(arg.id2name) and return return_val
|
182
|
-
when String
|
183
|
-
return_val = lookup_name(arg) and return return_val
|
184
|
-
when Fixnum
|
185
|
-
return_val = lookup_id(arg) and return return_val
|
186
|
-
when self
|
187
|
-
return arg
|
188
|
-
when nil
|
189
|
-
nil
|
190
|
-
else
|
191
|
-
raise TypeError, "#{self.name}[]: argument should be a String, Symbol or Fixnum but got a: #{arg.class.name}"
|
192
|
-
end
|
193
|
-
|
194
|
-
handle_lookup_failure(arg)
|
172
|
+
lookup_enum_by_type(arg) || handle_lookup_failure(arg)
|
195
173
|
else
|
196
174
|
args.map{ |item| self[item] }.uniq
|
197
175
|
end
|
@@ -216,21 +194,6 @@ module PowerEnum::Enumerated
|
|
216
194
|
end
|
217
195
|
end
|
218
196
|
|
219
|
-
# Deals with a lookup failure for the given argument.
|
220
|
-
def handle_lookup_failure(arg)
|
221
|
-
if (lookup_failure_handler = self.acts_enumerated_on_lookup_failure)
|
222
|
-
case lookup_failure_handler
|
223
|
-
when Proc
|
224
|
-
lookup_failure_handler.call(arg)
|
225
|
-
else
|
226
|
-
self.send(lookup_failure_handler, arg)
|
227
|
-
end
|
228
|
-
else
|
229
|
-
self.send(:enforce_none, arg)
|
230
|
-
end
|
231
|
-
end
|
232
|
-
private :handle_lookup_failure
|
233
|
-
|
234
197
|
# Enum lookup by id
|
235
198
|
def lookup_id(arg)
|
236
199
|
all_by_id[arg]
|
@@ -312,6 +275,48 @@ module PowerEnum::Enumerated
|
|
312
275
|
|
313
276
|
# ---Private methods---
|
314
277
|
|
278
|
+
def load_all
|
279
|
+
conditions = self.acts_enumerated_conditions
|
280
|
+
order = self.acts_enumerated_order
|
281
|
+
where(conditions).order(order)
|
282
|
+
end
|
283
|
+
private :load_all
|
284
|
+
|
285
|
+
# Looks up the enum based on the type of the argument.
|
286
|
+
def lookup_enum_by_type(arg)
|
287
|
+
case arg
|
288
|
+
when Symbol
|
289
|
+
lookup_name(arg.id2name)
|
290
|
+
when String
|
291
|
+
lookup_name(arg)
|
292
|
+
when Fixnum
|
293
|
+
lookup_id(arg)
|
294
|
+
when self
|
295
|
+
arg
|
296
|
+
when nil
|
297
|
+
nil
|
298
|
+
else
|
299
|
+
raise TypeError, "#{self.name}[]: argument should"\
|
300
|
+
" be a String, Symbol or Fixnum but got a: #{arg.class.name}"
|
301
|
+
end
|
302
|
+
end
|
303
|
+
private :lookup_enum_by_type
|
304
|
+
|
305
|
+
# Deals with a lookup failure for the given argument.
|
306
|
+
def handle_lookup_failure(arg)
|
307
|
+
if (lookup_failure_handler = self.acts_enumerated_on_lookup_failure)
|
308
|
+
case lookup_failure_handler
|
309
|
+
when Proc
|
310
|
+
lookup_failure_handler.call(arg)
|
311
|
+
else
|
312
|
+
self.send(lookup_failure_handler, arg)
|
313
|
+
end
|
314
|
+
else
|
315
|
+
self.send(:enforce_none, arg)
|
316
|
+
end
|
317
|
+
end
|
318
|
+
private :handle_lookup_failure
|
319
|
+
|
315
320
|
# Returns a hash of all enumeration members keyed by their ids.
|
316
321
|
def all_by_id
|
317
322
|
@all_by_id ||= all_by_attribute( :id )
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: power_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -40,7 +40,7 @@ cert_chain:
|
|
40
40
|
amZaYUtWM2wwYTFkR2wyUVNMa2pScFFaRDFiRCtVSDdnT3F5N1piZGNzUkJM
|
41
41
|
NEg3VTV6VQpibEtkZEg2dXhDckRTTTdLYWJrelNPVmYKLS0tLS1FTkQgQ0VS
|
42
42
|
VElGSUNBVEUtLS0tLQo=
|
43
|
-
date: 2013-02-
|
43
|
+
date: 2013-02-17 00:00:00.000000000 Z
|
44
44
|
dependencies:
|
45
45
|
- !ruby/object:Gem::Dependency
|
46
46
|
name: rails
|
@@ -170,6 +170,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
170
170
|
- - ! '>='
|
171
171
|
- !ruby/object:Gem::Version
|
172
172
|
version: '0'
|
173
|
+
segments:
|
174
|
+
- 0
|
175
|
+
hash: -4180923029060958212
|
173
176
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
177
|
none: false
|
175
178
|
requirements:
|
@@ -178,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
178
181
|
version: '0'
|
179
182
|
requirements: []
|
180
183
|
rubyforge_project:
|
181
|
-
rubygems_version: 1.8.
|
184
|
+
rubygems_version: 1.8.25
|
182
185
|
signing_key:
|
183
186
|
specification_version: 3
|
184
187
|
summary: Allows you to treat instances of your ActiveRecord models as though they
|
metadata.gz.sig
CHANGED
Binary file
|