power_enum 3.5.0 → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,518 +2,523 @@
2
2
  # Copyright (c) 2012 Arthur Shagall
3
3
  # Released under the MIT License. See the LICENSE file for more details.
4
4
 
5
- # Implementation of acts_as_enumerated
6
- module PowerEnum::Enumerated
7
- extend ActiveSupport::Concern
5
+ module PowerEnum
8
6
 
9
- # Class level methods injected into ActiveRecord.
10
- module ClassMethods
7
+ # Implementation of acts_as_enumerated
8
+ module Enumerated
9
+ extend ActiveSupport::Concern
11
10
 
12
- # Returns false for ActiveRecord models that do not act as enumerated.
13
- def acts_as_enumerated?
14
- false
15
- end
11
+ # Class level methods injected into ActiveRecord.
12
+ module ClassMethods
16
13
 
17
- # Declares the model as enumerated. See the README for detailed usage instructions.
18
- #
19
- # === Supported options
20
- # [:conditions]
21
- # SQL search conditions
22
- # [:order]
23
- # SQL load order clause
24
- # [:on_lookup_failure]
25
- # Specifies the name of a class method to invoke when the +[]+ method is unable to locate a BookingStatus
26
- # record for arg. The default is the built-in :enforce_none which returns nil. There are also built-ins for
27
- # :enforce_strict (raise and exception regardless of the type for arg), :enforce_strict_literals (raises an
28
- # exception if the arg is a Integer or Symbol), :enforce_strict_ids (raises and exception if the arg is a
29
- # Integer) and :enforce_strict_symbols (raises an exception if the arg is a Symbol). The purpose of the
30
- # :on_lookup_failure option is that a) under some circumstances a lookup failure is a Bad Thing and action
31
- # should be taken, therefore b) a fallback action should be easily configurable. You can also give it a
32
- # lambda that takes in a single argument (The arg that was passed to +[]+).
33
- # [:name_column]
34
- # Override for the 'name' column. By default, assumed to be 'name'.
35
- # [:alias_name]
36
- # By default, if a name column is not 'name', will create an alias of 'name' to the name_column attribute. Set
37
- # this to +false+ if you don't want this behavior.
38
- # [:freeze_members]
39
- # Specifies whether individual enum instances should be frozen on database load. By default, true in production.
40
- # Can be either a lambda or a boolean.
41
- #
42
- # === Examples
43
- #
44
- # ====Example 1
45
- # class BookingStatus < ActiveRecord::Base
46
- # acts_as_enumerated
47
- # end
48
- #
49
- # ====Example 2
50
- # class BookingStatus < ActiveRecord::Base
51
- # acts_as_enumerated :on_lookup_failure => :enforce_strict
52
- # end
53
- #
54
- # ====Example 3
55
- # class BookingStatus < ActiveRecord::Base
56
- # acts_as_enumerated :conditions => [:exclude => false],
57
- # :order => 'created_at DESC',
58
- # :on_lookup_failure => :lookup_failed,
59
- # :name_column => :status_code
60
- #
61
- # def self.lookup_failed(arg)
62
- # logger.error("Invalid status code lookup #{arg.inspect}")
63
- # nil
64
- # end
65
- # end
66
- #
67
- # ====Example 4
68
- # class BookingStatus < ActiveRecord::Base
69
- # acts_as_enumerated :conditions => [:exclude => false],
70
- # :order => 'created_at DESC',
71
- # :on_lookup_failure => lambda { |arg| raise CustomError, "BookingStatus lookup failed; #{arg}" },
72
- # :name_column => :status_code,
73
- # :freeze_members => true
74
- # end
75
- def acts_as_enumerated(options = {})
76
- valid_keys = [:conditions, :order, :on_lookup_failure, :name_column, :alias_name, :freeze_members]
77
- options.assert_valid_keys(*valid_keys)
78
-
79
- valid_keys.each do |key|
80
- class_attribute "acts_enumerated_#{key.to_s}"
81
- if options.has_key?( key )
82
- self.send "acts_enumerated_#{key.to_s}=", options[key]
83
- end
14
+ # Returns false for ActiveRecord models that do not act as enumerated.
15
+ def acts_as_enumerated?
16
+ false
84
17
  end
85
18
 
86
- self.acts_enumerated_name_column = get_name_column(options)
19
+ # Declares the model as enumerated. See the README for detailed usage instructions.
20
+ #
21
+ # === Supported options
22
+ # [:conditions]
23
+ # SQL search conditions
24
+ # [:order]
25
+ # SQL load order clause
26
+ # [:on_lookup_failure]
27
+ # Specifies the name of a class method to invoke when the +[]+ method is unable to locate a BookingStatus
28
+ # record for arg. The default is the built-in :enforce_none which returns nil. There are also built-ins for
29
+ # :enforce_strict (raise and exception regardless of the type for arg), :enforce_strict_literals (raises an
30
+ # exception if the arg is a Integer or Symbol), :enforce_strict_ids (raises and exception if the arg is a
31
+ # Integer) and :enforce_strict_symbols (raises an exception if the arg is a Symbol). The purpose of the
32
+ # :on_lookup_failure option is that a) under some circumstances a lookup failure is a Bad Thing and action
33
+ # should be taken, therefore b) a fallback action should be easily configurable. You can also give it a
34
+ # lambda that takes in a single argument (The arg that was passed to +[]+).
35
+ # [:name_column]
36
+ # Override for the 'name' column. By default, assumed to be 'name'.
37
+ # [:alias_name]
38
+ # By default, if a name column is not 'name', will create an alias of 'name' to the name_column attribute. Set
39
+ # this to +false+ if you don't want this behavior.
40
+ # [:freeze_members]
41
+ # Specifies whether individual enum instances should be frozen on database load. By default, true in production.
42
+ # Can be either a lambda or a boolean.
43
+ #
44
+ # === Examples
45
+ #
46
+ # ====Example 1
47
+ # class BookingStatus < ActiveRecord::Base
48
+ # acts_as_enumerated
49
+ # end
50
+ #
51
+ # ====Example 2
52
+ # class BookingStatus < ActiveRecord::Base
53
+ # acts_as_enumerated :on_lookup_failure => :enforce_strict
54
+ # end
55
+ #
56
+ # ====Example 3
57
+ # class BookingStatus < ActiveRecord::Base
58
+ # acts_as_enumerated :conditions => [:exclude => false],
59
+ # :order => 'created_at DESC',
60
+ # :on_lookup_failure => :lookup_failed,
61
+ # :name_column => :status_code
62
+ #
63
+ # def self.lookup_failed(arg)
64
+ # logger.error("Invalid status code lookup #{arg.inspect}")
65
+ # nil
66
+ # end
67
+ # end
68
+ #
69
+ # ====Example 4
70
+ # class BookingStatus < ActiveRecord::Base
71
+ # acts_as_enumerated :conditions => [:exclude => false],
72
+ # :order => 'created_at DESC',
73
+ # :on_lookup_failure => lambda { |arg| raise CustomError, "BookingStatus lookup failed; #{arg}" },
74
+ # :name_column => :status_code,
75
+ # :freeze_members => true
76
+ # end
77
+ def acts_as_enumerated(options = {})
78
+ valid_keys = [:conditions, :order, :on_lookup_failure, :name_column, :alias_name, :freeze_members]
79
+ options.assert_valid_keys(*valid_keys)
80
+
81
+ valid_keys.each do |key|
82
+ class_attribute "acts_enumerated_#{key.to_s}"
83
+ if options.has_key?( key )
84
+ self.send "acts_enumerated_#{key.to_s}=", options[key]
85
+ end
86
+ end
87
87
 
88
- unless self.is_a? PowerEnum::Enumerated::EnumClassMethods
89
- preserve_query_aliases
90
- extend_enum_class_methods( options )
91
- end
92
- end
88
+ self.acts_enumerated_name_column = get_name_column(options)
93
89
 
94
- # Rails 4 delegates all the finder methods to 'all'. PowerEnum overrides 'all'. Hence,
95
- # the need to re-alias the query methods.
96
- def preserve_query_aliases
97
- class << self
98
- # I have to do the interesting hack below instead of using alias_method
99
- # because there's some sort of weirdness going on with how __all binds
100
- # to all in Ruby 2.0.
101
- __all = self.instance_method(:all)
102
-
103
- define_method(:__all) do
104
- __all.bind(self).call
90
+ unless self.is_a? PowerEnum::Enumerated::EnumClassMethods
91
+ preserve_query_aliases
92
+ extend_enum_class_methods( options )
105
93
  end
94
+ end
106
95
 
107
- # From ActiveRecord::Querying
108
- delegate :find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, :to => :__all
109
- delegate :first_or_create, :first_or_create!, :first_or_initialize, :to => :__all
110
- delegate :find_or_create_by, :find_or_create_by!, :find_or_initialize_by, :to => :__all
111
- delegate :find_by, :find_by!, :to => :__all
112
- delegate :destroy, :destroy_all, :delete, :delete_all, :update, :update_all, :to => :__all
113
- delegate :find_each, :find_in_batches, :to => :__all
114
- delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins,
115
- :where, :preload, :eager_load, :includes, :from, :lock, :readonly,
116
- :having, :create_with, :uniq, :distinct, :references, :none, :unscope, :to => :__all
117
- delegate :count, :average, :minimum, :maximum, :sum, :calculate, :pluck, :ids, :to => :__all
96
+ # Rails 4 delegates all the finder methods to 'all'. PowerEnum overrides 'all'. Hence,
97
+ # the need to re-alias the query methods.
98
+ def preserve_query_aliases
99
+ class << self
100
+ # I have to do the interesting hack below instead of using alias_method
101
+ # because there's some sort of weirdness going on with how __all binds
102
+ # to all in Ruby 2.0.
103
+ __all = self.instance_method(:all)
104
+
105
+ define_method(:__all) do
106
+ __all.bind(self).call
107
+ end
108
+
109
+ # From ActiveRecord::Querying
110
+ delegate :find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, :to => :__all
111
+ delegate :first_or_create, :first_or_create!, :first_or_initialize, :to => :__all
112
+ delegate :find_or_create_by, :find_or_create_by!, :find_or_initialize_by, :to => :__all
113
+ delegate :find_by, :find_by!, :to => :__all
114
+ delegate :destroy, :destroy_all, :delete, :delete_all, :update, :update_all, :to => :__all
115
+ delegate :find_each, :find_in_batches, :to => :__all
116
+ delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins,
117
+ :where, :preload, :eager_load, :includes, :from, :lock, :readonly,
118
+ :having, :create_with, :uniq, :distinct, :references, :none, :unscope, :to => :__all
119
+ delegate :count, :average, :minimum, :maximum, :sum, :calculate, :pluck, :ids, :to => :__all
120
+ end
118
121
  end
119
- end
120
122
 
121
- # Injects the class methods into model
122
- def extend_enum_class_methods(options) #:nodoc:
123
+ # Injects the class methods into model
124
+ def extend_enum_class_methods(options) #:nodoc:
123
125
 
124
- extend PowerEnum::Enumerated::EnumClassMethods
126
+ extend PowerEnum::Enumerated::EnumClassMethods
125
127
 
126
- class_eval do
127
- include PowerEnum::Enumerated::EnumInstanceMethods
128
+ class_eval do
129
+ include PowerEnum::Enumerated::EnumInstanceMethods
128
130
 
129
- before_save :enumeration_model_update
130
- before_destroy :enumeration_model_update
131
- validates acts_enumerated_name_column, :presence => true, :uniqueness => true
132
- validate :validate_enumeration_model_updates_permitted
131
+ before_save :enumeration_model_update
132
+ before_destroy :enumeration_model_update
133
+ validates acts_enumerated_name_column, :presence => true, :uniqueness => true
134
+ validate :validate_enumeration_model_updates_permitted
133
135
 
134
- define_method :__enum_name__ do
135
- read_attribute(acts_enumerated_name_column).to_s
136
- end
136
+ define_method :__enum_name__ do
137
+ read_attribute(acts_enumerated_name_column).to_s
138
+ end
137
139
 
138
- if should_alias_name?(options) && acts_enumerated_name_column != :name
139
- alias_method :name, :__enum_name__
140
- end
141
- end # class_eval
140
+ if should_alias_name?(options) && acts_enumerated_name_column != :name
141
+ alias_method :name, :__enum_name__
142
+ end
143
+ end # class_eval
142
144
 
143
- end
145
+ end
144
146
 
145
- # Determines if the name column should be explicitly aliased
146
- def should_alias_name?(options) #:nodoc:
147
- if options.has_key?(:alias_name) then
148
- options[:alias_name]
149
- else
150
- true
147
+ # Determines if the name column should be explicitly aliased
148
+ def should_alias_name?(options) #:nodoc:
149
+ if options.has_key?(:alias_name) then
150
+ options[:alias_name]
151
+ else
152
+ true
153
+ end
151
154
  end
152
- end
153
155
 
154
- # Extracts the name column from options or gives the default
155
- def get_name_column(options) #:nodoc:
156
- if options.has_key?(:name_column) && !options[:name_column].blank? then
157
- options[:name_column].to_s.to_sym
158
- else
159
- :name
156
+ # Extracts the name column from options or gives the default
157
+ def get_name_column(options) #:nodoc:
158
+ if options.has_key?(:name_column) && !options[:name_column].blank? then
159
+ options[:name_column].to_s.to_sym
160
+ else
161
+ :name
162
+ end
160
163
  end
161
164
  end
162
- end
163
165
 
164
- # These are class level methods which are patched into classes that act as
165
- # enumerated
166
- module EnumClassMethods
167
- attr_accessor :enumeration_model_updates_permitted
166
+ # These are class level methods which are patched into classes that act as
167
+ # enumerated
168
+ module EnumClassMethods
169
+ attr_accessor :enumeration_model_updates_permitted
168
170
 
169
- # Returns true for ActiveRecord models that act as enumerated.
170
- def acts_as_enumerated?
171
- true
172
- end
171
+ # Returns true for ActiveRecord models that act as enumerated.
172
+ def acts_as_enumerated?
173
+ true
174
+ end
173
175
 
174
- # Returns all the enum values. Caches results after the first time this method is run.
175
- def all
176
- return @all if @all
176
+ # Returns all the enum values. Caches results after the first time this method is run.
177
+ def all
178
+ return @all if @all
177
179
 
178
- freeze_handler = if (handler = self.acts_enumerated_freeze_members).nil?
179
- -> { Rails.env.production? }
180
- else
181
- case handler
182
- when Proc
183
- handler
180
+ freeze_handler = if (handler = self.acts_enumerated_freeze_members).nil?
181
+ -> { Rails.env.production? }
184
182
  else
185
- -> { handler }
183
+ case handler
184
+ when Proc
185
+ handler
186
+ else
187
+ -> { handler }
188
+ end
186
189
  end
187
- end
188
190
 
189
- @all = load_all.collect{ |val| !!freeze_handler.call ? val.freeze : val }.freeze
190
- end
191
+ @all = load_all.collect{ |val| !!freeze_handler.call ? val.freeze : val }.freeze
192
+ end
191
193
 
192
- # Returns all the active enum values. See the 'active?' instance method.
193
- def active
194
- return @all_active if @all_active
195
- @all_active = all.find_all{ |enum| enum.active? }.freeze
196
- end
194
+ # Returns all the active enum values. See the 'active?' instance method.
195
+ def active
196
+ return @all_active if @all_active
197
+ @all_active = all.find_all{ |enum| enum.active? }.freeze
198
+ end
197
199
 
198
- # Returns all the inactive enum values. See the 'inactive?' instance method.
199
- def inactive
200
- return @all_inactive if @all_inactive
201
- @all_inactive = all.find_all{ |enum| !enum.active? }.freeze
202
- end
200
+ # Returns all the inactive enum values. See the 'inactive?' instance method.
201
+ def inactive
202
+ return @all_inactive if @all_inactive
203
+ @all_inactive = all.find_all{ |enum| !enum.active? }.freeze
204
+ end
203
205
 
204
- # Returns the names of all the enum values as an array of symbols.
205
- def names
206
- all.map { |item| item.name_sym }
207
- end
206
+ # Returns the names of all the enum values as an array of symbols.
207
+ def names
208
+ all.map { |item| item.name_sym }
209
+ end
208
210
 
209
- # Returns all except for the given list
210
- def all_except(*excluded)
211
- all.find_all { |item| !(item === excluded) }
212
- end
211
+ # Returns all except for the given list
212
+ def all_except(*excluded)
213
+ all.find_all { |item| !(item === excluded) }
214
+ end
213
215
 
214
- # Enum lookup by Symbol, String, or id. Returns <tt>arg<tt> if arg is
215
- # an enum instance. Passing in a list of arguments returns a list of
216
- # enums. When called with no arguments, returns nil.
217
- def [](*args)
218
- case args.size
219
- when 0
220
- nil
221
- when 1
222
- arg = args.first
223
- lookup_enum_by_type(arg) || handle_lookup_failure(arg)
224
- else
225
- args.map{ |item| self[item] }.uniq
216
+ # Enum lookup by Symbol, String, or id. Returns <tt>arg<tt> if arg is
217
+ # an enum instance. Passing in a list of arguments returns a list of
218
+ # enums. When called with no arguments, returns nil.
219
+ def [](*args)
220
+ case args.size
221
+ when 0
222
+ nil
223
+ when 1
224
+ arg = args.first
225
+ Array === arg ? self[*arg] : (lookup_enum_by_type(arg) || handle_lookup_failure(arg))
226
+ else
227
+ args.map{ |item| self[item] }.uniq
228
+ end
226
229
  end
227
- end
228
230
 
229
- # Returns <tt>true</tt> if the given Symbol, String or id has a member
230
- # instance in the enumeration, <tt>false</tt> otherwise. Returns <tt>true</tt>
231
- # if the argument is an enum instance, returns <tt>false</tt> if the argument
232
- # is nil or any other value.
233
- def contains?(arg)
234
- case arg
235
- when Symbol
236
- !!lookup_name(arg.id2name)
237
- when String
238
- !!lookup_name(arg)
239
- when Integer
240
- !!lookup_id(arg)
241
- when self
242
- true
243
- else
244
- false
231
+ # Returns <tt>true</tt> if the given Symbol, String or id has a member
232
+ # instance in the enumeration, <tt>false</tt> otherwise. Returns <tt>true</tt>
233
+ # if the argument is an enum instance, returns <tt>false</tt> if the argument
234
+ # is nil or any other value.
235
+ def contains?(arg)
236
+ case arg
237
+ when Symbol
238
+ !!lookup_name(arg.id2name)
239
+ when String
240
+ !!lookup_name(arg)
241
+ when Integer
242
+ !!lookup_id(arg)
243
+ when self
244
+ true
245
+ else
246
+ false
247
+ end
245
248
  end
246
- end
247
249
 
248
- # Enum lookup by id
249
- def lookup_id(arg)
250
- all_by_id[arg]
251
- end
250
+ # Enum lookup by id
251
+ def lookup_id(arg)
252
+ all_by_id[arg]
253
+ end
252
254
 
253
- # Enum lookup by String
254
- def lookup_name(arg)
255
- all_by_name[arg]
256
- end
255
+ # Enum lookup by String
256
+ def lookup_name(arg)
257
+ all_by_name[arg]
258
+ end
257
259
 
258
- # Returns true if the enum lookup by the given Symbol, String or id would have returned a value, false otherwise.
259
- def include?(arg)
260
- case arg
261
- when Symbol
262
- !lookup_name(arg.id2name).nil?
263
- when String
264
- !lookup_name(arg).nil?
265
- when Integer
266
- !lookup_id(arg).nil?
267
- when self
268
- possible_match = lookup_id(arg.id)
269
- !possible_match.nil? && possible_match == arg
270
- else
271
- false
260
+ # Returns true if the enum lookup by the given Symbol, String or id would have returned a value, false otherwise.
261
+ def include?(arg)
262
+ case arg
263
+ when Symbol
264
+ !lookup_name(arg.id2name).nil?
265
+ when String
266
+ !lookup_name(arg).nil?
267
+ when Integer
268
+ !lookup_id(arg).nil?
269
+ when self
270
+ possible_match = lookup_id(arg.id)
271
+ !possible_match.nil? && possible_match == arg
272
+ else
273
+ false
274
+ end
272
275
  end
273
- end
274
276
 
275
- # NOTE: purging the cache is sort of pointless because
276
- # of the per-process rails model.
277
- # By default this blows up noisily just in case you try to be more
278
- # clever than rails allows.
279
- # For those times (like in Migrations) when you really do want to
280
- # alter the records you can silence the carping by setting
281
- # enumeration_model_updates_permitted to true.
282
- def purge_enumerations_cache
283
- unless self.enumeration_model_updates_permitted
284
- raise "#{self.name}: cache purging disabled for your protection"
285
- end
286
- @all = @all_by_name = @all_by_id = @all_active = nil
287
- end
277
+ # NOTE: purging the cache is sort of pointless because
278
+ # of the per-process rails model.
279
+ # By default this blows up noisily just in case you try to be more
280
+ # clever than rails allows.
281
+ # For those times (like in Migrations) when you really do want to
282
+ # alter the records you can silence the carping by setting
283
+ # enumeration_model_updates_permitted to true.
284
+ def purge_enumerations_cache
285
+ unless self.enumeration_model_updates_permitted
286
+ raise "#{self.name}: cache purging disabled for your protection"
287
+ end
288
+ @all = @all_by_name = @all_by_id = @all_active = nil
289
+ end
288
290
 
289
- # The preferred method to update an enumerations model. The same
290
- # warnings as 'purge_enumerations_cache' and
291
- # 'enumerations_model_update_permitted' apply. Pass a block to this
292
- # method where you perform your updates. Cache will be
293
- # flushed automatically. If your block takes an argument, will pass in
294
- # the model class. The argument is optional.
295
- def update_enumerations_model(&block)
296
- if block_given?
297
- begin
298
- self.enumeration_model_updates_permitted = true
299
- purge_enumerations_cache
300
- @all = load_all
301
- @enumerations_model_updating = true
302
- case block.arity
303
- when 0
304
- yield
305
- else
306
- yield self
291
+ # The preferred method to update an enumerations model. The same
292
+ # warnings as 'purge_enumerations_cache' and
293
+ # 'enumerations_model_update_permitted' apply. Pass a block to this
294
+ # method where you perform your updates. Cache will be
295
+ # flushed automatically. If your block takes an argument, will pass in
296
+ # the model class. The argument is optional.
297
+ def update_enumerations_model(&block)
298
+ if block_given?
299
+ begin
300
+ self.enumeration_model_updates_permitted = true
301
+ purge_enumerations_cache
302
+ @all = load_all
303
+ @enumerations_model_updating = true
304
+ case block.arity
305
+ when 0
306
+ yield
307
+ else
308
+ yield self
309
+ end
310
+ ensure
311
+ purge_enumerations_cache
312
+ @enumerations_model_updating = false
313
+ self.enumeration_model_updates_permitted = false
307
314
  end
308
- ensure
309
- purge_enumerations_cache
310
- @enumerations_model_updating = false
311
- self.enumeration_model_updates_permitted = false
312
315
  end
313
316
  end
314
- end
315
317
 
316
- # Returns true if the enumerations model is in the middle of an
317
- # update_enumerations_model block, false otherwise.
318
- def enumerations_model_updating?
319
- !!@enumerations_model_updating
320
- end
318
+ # Returns true if the enumerations model is in the middle of an
319
+ # update_enumerations_model block, false otherwise.
320
+ def enumerations_model_updating?
321
+ !!@enumerations_model_updating
322
+ end
321
323
 
322
- # Returns the name of the column this enum uses as the basic underlying value.
323
- def name_column
324
- @name_column ||= self.acts_enumerated_name_column
325
- end
324
+ # Returns the name of the column this enum uses as the basic underlying value.
325
+ def name_column
326
+ @name_column ||= self.acts_enumerated_name_column
327
+ end
326
328
 
327
- # ---Private methods---
329
+ # ---Private methods---
328
330
 
329
- private def load_all
330
- conditions = self.acts_enumerated_conditions
331
- order = self.acts_enumerated_order
332
- unscoped.where(conditions).order(order)
333
- end
331
+ private def load_all
332
+ conditions = self.acts_enumerated_conditions
333
+ order = self.acts_enumerated_order
334
+ unscoped.where(conditions).order(order)
335
+ end
334
336
 
335
- # Looks up the enum based on the type of the argument.
336
- private def lookup_enum_by_type(arg)
337
- case arg
338
- when Symbol
339
- lookup_name(arg.id2name)
340
- when String
341
- lookup_name(arg)
342
- when Integer
343
- lookup_id(arg)
344
- when self
345
- arg
346
- when nil
347
- nil
348
- else
349
- raise TypeError, "#{self.name}[]: argument should"\
350
- " be a String, Symbol or Integer but got a: #{arg.class.name}"
337
+ # Looks up the enum based on the type of the argument.
338
+ private def lookup_enum_by_type(arg)
339
+ case arg
340
+ when Symbol
341
+ lookup_name(arg.id2name)
342
+ when String
343
+ lookup_name(arg)
344
+ when Integer
345
+ lookup_id(arg)
346
+ when self
347
+ arg
348
+ when nil
349
+ nil
350
+ else
351
+ raise TypeError, "#{self.name}[]: argument should"\
352
+ " be a String, Symbol or Integer but got a: #{arg.class.name}"
353
+ end
351
354
  end
352
- end
353
355
 
354
- # Deals with a lookup failure for the given argument.
355
- private def handle_lookup_failure(arg)
356
- if (lookup_failure_handler = self.acts_enumerated_on_lookup_failure)
357
- case lookup_failure_handler
358
- when Proc
359
- lookup_failure_handler.call(arg)
356
+ # Deals with a lookup failure for the given argument.
357
+ private def handle_lookup_failure(arg)
358
+ if (lookup_failure_handler = self.acts_enumerated_on_lookup_failure)
359
+ case lookup_failure_handler
360
+ when Proc
361
+ lookup_failure_handler.call(arg)
362
+ else
363
+ self.send(lookup_failure_handler, arg)
364
+ end
360
365
  else
361
- self.send(lookup_failure_handler, arg)
366
+ self.send(:enforce_none, arg)
362
367
  end
363
- else
364
- self.send(:enforce_none, arg)
365
368
  end
366
- end
367
369
 
368
- # Returns a hash of all enumeration members keyed by their ids.
369
- private def all_by_id
370
- @all_by_id ||= all_by_attribute( primary_key )
371
- end
370
+ # Returns a hash of all enumeration members keyed by their ids.
371
+ private def all_by_id
372
+ @all_by_id ||= all_by_attribute( primary_key )
373
+ end
372
374
 
373
- # Returns a hash of all the enumeration members keyed by their names.
374
- private def all_by_name
375
- begin
376
- @all_by_name ||= all_by_attribute( :__enum_name__ )
377
- rescue NoMethodError => err
378
- if err.name == name_column
379
- raise TypeError, "#{self.name}: you need to define a '#{name_column}' column in the table '#{table_name}'"
375
+ # Returns a hash of all the enumeration members keyed by their names.
376
+ private def all_by_name
377
+ begin
378
+ @all_by_name ||= all_by_attribute( :__enum_name__ )
379
+ rescue NoMethodError => err
380
+ if err.name == name_column
381
+ raise TypeError, "#{self.name}: you need to define a '#{name_column}' column in the table '#{table_name}'"
382
+ end
383
+ raise
380
384
  end
381
- raise
382
385
  end
383
- end
384
386
 
385
- private def all_by_attribute(attr) # :nodoc:
386
- aba = all.inject({}) { |memo, item|
387
- memo[item.send(attr)] = item
388
- memo
389
- }
390
- aba.freeze unless enumerations_model_updating?
391
- aba
392
- end
387
+ private def all_by_attribute(attr) # :nodoc:
388
+ aba = all.inject({}) { |memo, item|
389
+ memo[item.send(attr)] = item
390
+ memo
391
+ }
392
+ aba.freeze unless enumerations_model_updating?
393
+ aba
394
+ end
393
395
 
394
- private def enforce_none(arg) # :nodoc:
395
- nil
396
- end
396
+ private def enforce_none(arg) # :nodoc:
397
+ nil
398
+ end
397
399
 
398
- private def enforce_strict(arg) # :nodoc:
399
- raise_record_not_found(arg)
400
- end
400
+ private def enforce_strict(arg) # :nodoc:
401
+ raise_record_not_found(arg)
402
+ end
401
403
 
402
- private def enforce_strict_literals(arg) # :nodoc:
403
- raise_record_not_found(arg) if (Integer === arg) || (Symbol === arg)
404
- nil
405
- end
404
+ private def enforce_strict_literals(arg) # :nodoc:
405
+ raise_record_not_found(arg) if (Integer === arg) || (Symbol === arg)
406
+ nil
407
+ end
406
408
 
407
- private def enforce_strict_ids(arg) # :nodoc:
408
- raise_record_not_found(arg) if Integer === arg
409
- nil
410
- end
409
+ private def enforce_strict_ids(arg) # :nodoc:
410
+ raise_record_not_found(arg) if Integer === arg
411
+ nil
412
+ end
411
413
 
412
- private def enforce_strict_symbols(arg) # :nodoc:
413
- raise_record_not_found(arg) if Symbol === arg
414
- nil
415
- end
414
+ private def enforce_strict_symbols(arg) # :nodoc:
415
+ raise_record_not_found(arg) if Symbol === arg
416
+ nil
417
+ end
418
+
419
+ # raise the {ActiveRecord::RecordNotFound} error.
420
+ # @private
421
+ private def raise_record_not_found(arg)
422
+ raise ActiveRecord::RecordNotFound, "Couldn't find a #{self.name} identified by (#{arg.inspect})"
423
+ end
416
424
 
417
- # raise the {ActiveRecord::RecordNotFound} error.
418
- # @private
419
- private def raise_record_not_found(arg)
420
- raise ActiveRecord::RecordNotFound, "Couldn't find a #{self.name} identified by (#{arg.inspect})"
421
425
  end
422
426
 
423
- end
424
-
425
- # These are instance methods for objects which are enums.
426
- module EnumInstanceMethods
427
- # Behavior depends on the type of +arg+.
428
- #
429
- # * If +arg+ is +nil+, returns +false+.
430
- # * If +arg+ is an instance of +Symbol+, +Integer+ or +String+, returns the result of +BookingStatus[:foo] == BookingStatus[arg]+.
431
- # * If +arg+ is an +Array+, returns +true+ if any member of the array returns +true+ for +===(arg)+, +false+ otherwise.
432
- # * In all other cases, delegates to +===(arg)+ of the superclass.
433
- #
434
- # Examples:
435
- #
436
- # BookingStatus[:foo] === :foo #Returns true
437
- # BookingStatus[:foo] === 'foo' #Returns true
438
- # BookingStatus[:foo] === :bar #Returns false
439
- # BookingStatus[:foo] === [:foo, :bar, :baz] #Returns true
440
- # BookingStatus[:foo] === nil #Returns false
441
- #
442
- # You should note that defining an +:on_lookup_failure+ method that raises an exception will cause +===+ to
443
- # also raise an exception for any lookup failure of +BookingStatus[arg]+.
444
- def ===(arg)
445
- case arg
446
- when nil
447
- false
448
- when Symbol, String, Integer
449
- return self == self.class[arg]
450
- when Array
451
- return self.in?(*arg)
452
- else
453
- super
427
+ # These are instance methods for objects which are enums.
428
+ module EnumInstanceMethods
429
+ # Behavior depends on the type of +arg+.
430
+ #
431
+ # * If +arg+ is +nil+, returns +false+.
432
+ # * If +arg+ is an instance of +Symbol+, +Integer+ or +String+, returns the result of +BookingStatus[:foo] == BookingStatus[arg]+.
433
+ # * If +arg+ is an +Array+, returns +true+ if any member of the array returns +true+ for +===(arg)+, +false+ otherwise.
434
+ # * In all other cases, delegates to +===(arg)+ of the superclass.
435
+ #
436
+ # Examples:
437
+ #
438
+ # BookingStatus[:foo] === :foo #Returns true
439
+ # BookingStatus[:foo] === 'foo' #Returns true
440
+ # BookingStatus[:foo] === :bar #Returns false
441
+ # BookingStatus[:foo] === [:foo, :bar, :baz] #Returns true
442
+ # BookingStatus[:foo] === nil #Returns false
443
+ #
444
+ # You should note that defining an +:on_lookup_failure+ method that raises an exception will cause +===+ to
445
+ # also raise an exception for any lookup failure of +BookingStatus[arg]+.
446
+ def ===(arg)
447
+ case arg
448
+ when nil
449
+ false
450
+ when Symbol, String, Integer
451
+ return self == self.class[arg]
452
+ when Array
453
+ return self.in?(*arg)
454
+ else
455
+ super
456
+ end
454
457
  end
455
- end
456
458
 
457
- alias_method :like?, :===
459
+ alias_method :like?, :===
458
460
 
459
- # Returns true if any element in the list returns true for ===(arg), false otherwise.
460
- def in?(*list)
461
- for item in list
462
- self === item and return true
461
+ # Returns true if any element in the list returns true for ===(arg), false otherwise.
462
+ def in?(*list)
463
+ for item in list
464
+ self === item and return true
465
+ end
466
+ false
463
467
  end
464
- false
465
- end
466
468
 
467
- # Returns the symbol representation of the name of the enum. BookingStatus[:foo].name_sym returns :foo.
468
- def name_sym
469
- self.__enum_name__.to_sym
470
- end
469
+ # Returns the symbol representation of the name of the enum. BookingStatus[:foo].name_sym returns :foo.
470
+ def name_sym
471
+ self.__enum_name__.to_sym
472
+ end
471
473
 
472
- alias_method :to_sym, :name_sym
474
+ alias_method :to_sym, :name_sym
473
475
 
474
- # By default enumeration #to_s should return stringified name of the enum. BookingStatus[:foo].to_s returns "foo"
475
- def to_s
476
- self.__enum_name__
477
- end
476
+ # By default enumeration #to_s should return stringified name of the enum. BookingStatus[:foo].to_s returns "foo"
477
+ def to_s
478
+ self.__enum_name__
479
+ end
478
480
 
479
- # Returns true if the instance is active, false otherwise. If it has an attribute 'active',
480
- # returns the attribute cast to a boolean, otherwise returns true. This method is used by the 'active'
481
- # class method to select active enums.
482
- def active?
483
- @_active_status ||= ( attributes.include?('active') ? !!self.active : true )
484
- end
481
+ # Returns true if the instance is active, false otherwise. If it has an attribute 'active',
482
+ # returns the attribute cast to a boolean, otherwise returns true. This method is used by the 'active'
483
+ # class method to select active enums.
484
+ def active?
485
+ @_active_status ||= ( attributes.include?('active') ? !!self.active : true )
486
+ end
485
487
 
486
- # Returns true if the instance is inactive, false otherwise. Default implementations returns !active?
487
- # This method is used by the 'inactive' class method to select inactive enums.
488
- def inactive?
489
- !active?
490
- end
488
+ # Returns true if the instance is inactive, false otherwise. Default implementations returns !active?
489
+ # This method is used by the 'inactive' class method to select inactive enums.
490
+ def inactive?
491
+ !active?
492
+ end
491
493
 
492
- # NOTE: updating the models that back an acts_as_enumerated is
493
- # rather dangerous because of rails' per-process model.
494
- # The cached values could get out of synch between processes
495
- # and rather than completely disallow changes I make you jump
496
- # through an extra hoop just in case you're defining your enumeration
497
- # values in Migrations. I.e. set enumeration_model_updates_permitted = true
498
- private def enumeration_model_update
499
- if self.class.enumeration_model_updates_permitted
500
- self.class.purge_enumerations_cache
501
- true
502
- else
503
- # Ugh. This just seems hack-ish. I wonder if there's a better way.
504
- if Rails.version =~ /^4\.2\.*/
505
- false
494
+ # NOTE: updating the models that back an acts_as_enumerated is
495
+ # rather dangerous because of rails' per-process model.
496
+ # The cached values could get out of synch between processes
497
+ # and rather than completely disallow changes I make you jump
498
+ # through an extra hoop just in case you're defining your enumeration
499
+ # values in Migrations. I.e. set enumeration_model_updates_permitted = true
500
+ private def enumeration_model_update
501
+ if self.class.enumeration_model_updates_permitted
502
+ self.class.purge_enumerations_cache
503
+ true
506
504
  else
507
- throw(:abort)
505
+ # Ugh. This just seems hack-ish. I wonder if there's a better way.
506
+ if Rails.version =~ /^4\.2\.*/
507
+ false
508
+ else
509
+ throw(:abort)
510
+ end
508
511
  end
509
512
  end
510
- end
511
513
 
512
- # Validates that model updates are enabled.
513
- private def validate_enumeration_model_updates_permitted
514
- unless self.class.enumeration_model_updates_permitted
515
- self.errors.add(self.class.name_column, "changes to acts_as_enumeration model instances are not permitted")
514
+ # Validates that model updates are enabled.
515
+ private def validate_enumeration_model_updates_permitted
516
+ unless self.class.enumeration_model_updates_permitted
517
+ self.errors.add(self.class.name_column, "changes to acts_as_enumeration model instances are not permitted")
518
+ end
516
519
  end
517
- end
518
- end # module EnumInstanceMethods
519
- end # module PowerEnum::Enumerated
520
+ end # module EnumInstanceMethods
521
+
522
+ end # module Enumerated
523
+
524
+ end # module PowerEnum