power_enum 3.4.0 → 3.5.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e64bc976c66ec97d00cd14baabeb0fa7c13afc66500bb8408bc9d1e884be30ee
4
- data.tar.gz: 43f0a3fa296b90154d32b82a38d8ae207b0b8f400edc821ded353e341ddb5187
3
+ metadata.gz: e2f8367b2fd4b4e5d5620725c8f8133db78fe9a3737bb00d2019546345215fc1
4
+ data.tar.gz: 0ef3b6a9709516dba463b137aae4a7aab54a3374abaf6c91c7dcd87c1fc0e7a5
5
5
  SHA512:
6
- metadata.gz: b36b482f22cf0ddc61e817f77838a245a250a3c28771c0d0e39d8e23f29b782abd93fbb06c84fa6b009f66bdb510933b0e292a8ceeed60d190caeeeba24d05f2
7
- data.tar.gz: 2731947e3bb7d5f9b91fffd726db29ab26451536fc60b8110886d2d8ed3402af9f8f0d10471bea00c83f36e4577476df2fb1c2cdebe106b74d684e80d7637d33
6
+ metadata.gz: c7857ebcf50b49d544afd0acaf3f6b366cf9f022030613eafcc64eaed6befdbb53fa768ec5880ef6ccc972aca32d08cdb021d3e18a9352bcfeade06add26d25d
7
+ data.tar.gz: '08b4955a6492efcc9845432d39d09b498fbb341ae69e3859e19b55d0baa3f19d0661ae7adf10dfda0aff685d22e1b66bc1fe941dbf83f928ca2a7f1d23198eae'
@@ -63,26 +63,7 @@ See "How to use it" below for more information.
63
63
 
64
64
  ## Installation
65
65
 
66
- ### READ THIS FIRST
67
-
68
- This gem is signed. The public key is available
69
- here: https://github.com/albertosaurus/power_enum (look for gem-public\_cert.pem). Hence, if
70
- you can get an error like the following if you're installing in `HighSecurity` mode.
71
-
72
- ```
73
- ERROR: While executing gem ... (RuntimeError)
74
- Couldn't verify data signature: Untrusted Signing Chain Root: cert = 'cert name', error = 'path "/your_gem_home/.gem/trust/cert-blah.pem" does not exist'
75
- ```
76
-
77
- If this happens, you need to add the PowerEnum public cert to your gem cert
78
- store. Download the certificate (gem-public\_cert.pem) and run the following.
79
-
80
- gem cert -a gem-public_cert.pem
81
-
82
- More information is available at http://guides.rubygems.org/security/
83
- which I strongly urge you to read.
84
-
85
- #### Using Bundler
66
+ ### Using Bundler
86
67
 
87
68
  Add the gem to your Gemfile
88
69
 
@@ -92,7 +73,7 @@ then run
92
73
 
93
74
  bundle install
94
75
 
95
- #### Manual Installation
76
+ ### Manual Installation
96
77
 
97
78
  gem install power_enum
98
79
 
@@ -298,10 +279,12 @@ drop_table :booking_statuses
298
279
  class BookingStatus < ActiveRecord::Base
299
280
  acts_as_enumerated :conditions => 'optional_sql_conditions',
300
281
  :order => 'optional_sql_order_by',
301
- :on_lookup_failure => :optional_class_method, #This also works: lambda{ |arg| some_custom_action }
302
- :name_column => 'optional_name_column' #If required, may override the default name column
303
- :alias_name => false #If set to false and have name_column set, will not
304
- # alias :name to the name column attribute.
282
+ :on_lookup_failure => :optional_class_method, # This also works: lambda{ |arg| some_custom_action }
283
+ :name_column => 'optional_name_column' # If required, may override the default name column
284
+ :alias_name => false, # If set to false and have name_column set, will not
285
+ # alias :name to the name column attribute.
286
+ :freeze_members => true # Optional, default is true in prod.
287
+ # This also works: lambda { true }
305
288
  end
306
289
  ```
307
290
 
@@ -33,8 +33,11 @@ module PowerEnum::Enumerated
33
33
  # [:name_column]
34
34
  # Override for the 'name' column. By default, assumed to be 'name'.
35
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
36
+ # By default, if a name column is not 'name', will create an alias of 'name' to the name_column attribute. Set
37
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.
38
41
  #
39
42
  # === Examples
40
43
  #
@@ -66,10 +69,11 @@ module PowerEnum::Enumerated
66
69
  # acts_as_enumerated :conditions => [:exclude => false],
67
70
  # :order => 'created_at DESC',
68
71
  # :on_lookup_failure => lambda { |arg| raise CustomError, "BookingStatus lookup failed; #{arg}" },
69
- # :name_column => :status_code
72
+ # :name_column => :status_code,
73
+ # :freeze_members => true
70
74
  # end
71
75
  def acts_as_enumerated(options = {})
72
- valid_keys = [:conditions, :order, :on_lookup_failure, :name_column, :alias_name]
76
+ valid_keys = [:conditions, :order, :on_lookup_failure, :name_column, :alias_name, :freeze_members]
73
77
  options.assert_valid_keys(*valid_keys)
74
78
 
75
79
  valid_keys.each do |key|
@@ -79,7 +83,6 @@ module PowerEnum::Enumerated
79
83
  end
80
84
  end
81
85
 
82
- class_attribute :acts_enumerated_name_column
83
86
  self.acts_enumerated_name_column = get_name_column(options)
84
87
 
85
88
  unless self.is_a? PowerEnum::Enumerated::EnumClassMethods
@@ -138,7 +141,6 @@ module PowerEnum::Enumerated
138
141
  end # class_eval
139
142
 
140
143
  end
141
- private :extend_enum_class_methods
142
144
 
143
145
  # Determines if the name column should be explicitly aliased
144
146
  def should_alias_name?(options) #:nodoc:
@@ -148,7 +150,6 @@ module PowerEnum::Enumerated
148
150
  true
149
151
  end
150
152
  end
151
- private :should_alias_name?
152
153
 
153
154
  # Extracts the name column from options or gives the default
154
155
  def get_name_column(options) #:nodoc:
@@ -158,7 +159,6 @@ module PowerEnum::Enumerated
158
159
  :name
159
160
  end
160
161
  end
161
- private :get_name_column
162
162
  end
163
163
 
164
164
  # These are class level methods which are patched into classes that act as
@@ -174,7 +174,19 @@ module PowerEnum::Enumerated
174
174
  # Returns all the enum values. Caches results after the first time this method is run.
175
175
  def all
176
176
  return @all if @all
177
- @all = load_all.collect{|val| val.freeze}.freeze
177
+
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
184
+ else
185
+ -> { handler }
186
+ end
187
+ end
188
+
189
+ @all = load_all.collect{ |val| !!freeze_handler.call ? val.freeze : val }.freeze
178
190
  end
179
191
 
180
192
  # Returns all the active enum values. See the 'active?' instance method.
@@ -314,15 +326,14 @@ module PowerEnum::Enumerated
314
326
 
315
327
  # ---Private methods---
316
328
 
317
- def load_all
329
+ private def load_all
318
330
  conditions = self.acts_enumerated_conditions
319
331
  order = self.acts_enumerated_order
320
332
  unscoped.where(conditions).order(order)
321
333
  end
322
- private :load_all
323
334
 
324
335
  # Looks up the enum based on the type of the argument.
325
- def lookup_enum_by_type(arg)
336
+ private def lookup_enum_by_type(arg)
326
337
  case arg
327
338
  when Symbol
328
339
  lookup_name(arg.id2name)
@@ -339,10 +350,9 @@ module PowerEnum::Enumerated
339
350
  " be a String, Symbol or Integer but got a: #{arg.class.name}"
340
351
  end
341
352
  end
342
- private :lookup_enum_by_type
343
353
 
344
354
  # Deals with a lookup failure for the given argument.
345
- def handle_lookup_failure(arg)
355
+ private def handle_lookup_failure(arg)
346
356
  if (lookup_failure_handler = self.acts_enumerated_on_lookup_failure)
347
357
  case lookup_failure_handler
348
358
  when Proc
@@ -354,16 +364,14 @@ module PowerEnum::Enumerated
354
364
  self.send(:enforce_none, arg)
355
365
  end
356
366
  end
357
- private :handle_lookup_failure
358
367
 
359
368
  # Returns a hash of all enumeration members keyed by their ids.
360
- def all_by_id
369
+ private def all_by_id
361
370
  @all_by_id ||= all_by_attribute( primary_key )
362
371
  end
363
- private :all_by_id
364
372
 
365
373
  # Returns a hash of all the enumeration members keyed by their names.
366
- def all_by_name
374
+ private def all_by_name
367
375
  begin
368
376
  @all_by_name ||= all_by_attribute( :__enum_name__ )
369
377
  rescue NoMethodError => err
@@ -373,9 +381,8 @@ module PowerEnum::Enumerated
373
381
  raise
374
382
  end
375
383
  end
376
- private :all_by_name
377
384
 
378
- def all_by_attribute(attr) # :nodoc:
385
+ private def all_by_attribute(attr) # :nodoc:
379
386
  aba = all.inject({}) { |memo, item|
380
387
  memo[item.send(attr)] = item
381
388
  memo
@@ -383,42 +390,35 @@ module PowerEnum::Enumerated
383
390
  aba.freeze unless enumerations_model_updating?
384
391
  aba
385
392
  end
386
- private :all_by_attribute
387
393
 
388
- def enforce_none(arg) # :nodoc:
394
+ private def enforce_none(arg) # :nodoc:
389
395
  nil
390
396
  end
391
- private :enforce_none
392
397
 
393
- def enforce_strict(arg) # :nodoc:
398
+ private def enforce_strict(arg) # :nodoc:
394
399
  raise_record_not_found(arg)
395
400
  end
396
- private :enforce_strict
397
401
 
398
- def enforce_strict_literals(arg) # :nodoc:
402
+ private def enforce_strict_literals(arg) # :nodoc:
399
403
  raise_record_not_found(arg) if (Integer === arg) || (Symbol === arg)
400
404
  nil
401
405
  end
402
- private :enforce_strict_literals
403
406
 
404
- def enforce_strict_ids(arg) # :nodoc:
407
+ private def enforce_strict_ids(arg) # :nodoc:
405
408
  raise_record_not_found(arg) if Integer === arg
406
409
  nil
407
410
  end
408
- private :enforce_strict_ids
409
411
 
410
- def enforce_strict_symbols(arg) # :nodoc:
412
+ private def enforce_strict_symbols(arg) # :nodoc:
411
413
  raise_record_not_found(arg) if Symbol === arg
412
414
  nil
413
415
  end
414
- private :enforce_strict_symbols
415
416
 
416
417
  # raise the {ActiveRecord::RecordNotFound} error.
417
418
  # @private
418
- def raise_record_not_found(arg)
419
+ private def raise_record_not_found(arg)
419
420
  raise ActiveRecord::RecordNotFound, "Couldn't find a #{self.name} identified by (#{arg.inspect})"
420
421
  end
421
- private :raise_record_not_found
422
422
 
423
423
  end
424
424
 
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: 3.4.0
4
+ version: 3.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trevor Squires
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2020-06-16 00:00:00.000000000 Z
14
+ date: 2021-01-05 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: bundler