power_enum 3.1.0 → 3.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.markdown +18 -31
- data/lib/power_enum/enumerated.rb +37 -32
- data/lib/power_enum/reflection.rb +9 -0
- metadata +16 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e2f8367b2fd4b4e5d5620725c8f8133db78fe9a3737bb00d2019546345215fc1
|
4
|
+
data.tar.gz: 0ef3b6a9709516dba463b137aae4a7aab54a3374abaf6c91c7dcd87c1fc0e7a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c7857ebcf50b49d544afd0acaf3f6b366cf9f022030613eafcc64eaed6befdbb53fa768ec5880ef6ccc972aca32d08cdb021d3e18a9352bcfeade06add26d25d
|
7
|
+
data.tar.gz: '08b4955a6492efcc9845432d39d09b498fbb341ae69e3859e19b55d0baa3f19d0661ae7adf10dfda0aff685d22e1b66bc1fe941dbf83f928ca2a7f1d23198eae'
|
data/README.markdown
CHANGED
@@ -10,7 +10,7 @@ Enumerations for Rails Done Right.
|
|
10
10
|
|
11
11
|
## Versions
|
12
12
|
|
13
|
-
* PowerEnum 3.X (this version) supports Rails 4.2 and Rails
|
13
|
+
* PowerEnum 3.X (this version) supports Rails 4.2, Rails 5.X and Rails 6.0
|
14
14
|
* PowerEnum 2.X supports Rails 4.X and Rails 5.0
|
15
15
|
* PowerEnum 1.X supports Rails 3.1/3.2, available here: https://github.com/albertosaurus/power_enum
|
16
16
|
|
@@ -54,7 +54,7 @@ See "How to use it" below for more information.
|
|
54
54
|
### PowerEnum 3.X
|
55
55
|
|
56
56
|
* Ruby 2.1 or later (JRuby should work but isn't extensively tested; Travis is being difficult).
|
57
|
-
* Rails 4.2, 5.0, 5.1
|
57
|
+
* Rails 4.2, 5.0, 5.1, 5.2, 6.0
|
58
58
|
|
59
59
|
### PowerEnum 2.X
|
60
60
|
|
@@ -63,26 +63,7 @@ See "How to use it" below for more information.
|
|
63
63
|
|
64
64
|
## Installation
|
65
65
|
|
66
|
-
###
|
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
|
-
|
76
|
+
### Manual Installation
|
96
77
|
|
97
78
|
gem install power_enum
|
98
79
|
|
@@ -198,7 +179,7 @@ create_table :bookings do |t|
|
|
198
179
|
t.timestamps
|
199
180
|
end
|
200
181
|
```
|
201
|
-
|
182
|
+
|
202
183
|
There are two methods added to Rails migrations:
|
203
184
|
|
204
185
|
##### create\_enum(enum\_name, options = {}, &block)
|
@@ -285,7 +266,7 @@ Example:
|
|
285
266
|
```ruby
|
286
267
|
remove_enum :booking_status
|
287
268
|
```
|
288
|
-
|
269
|
+
|
289
270
|
is the equivalent of
|
290
271
|
|
291
272
|
```ruby
|
@@ -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
|
304
|
-
#
|
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
|
|
@@ -341,6 +324,10 @@ other value.
|
|
341
324
|
`BookingStatus.all` returns an array of all BookingStatus records that match the `:conditions` specified in
|
342
325
|
`acts_as_enumerated`, in the order specified by `:order`.
|
343
326
|
|
327
|
+
##### all_except(*items)
|
328
|
+
|
329
|
+
`BookingStatus.all_except(arg1, arg2)` returns an array of all BookingStatus records with the given items filtered out.
|
330
|
+
|
344
331
|
##### active
|
345
332
|
|
346
333
|
`BookingStatus.active` returns an array of all BookingStatus records that are marked active. See the `active?` instance
|
@@ -784,7 +771,7 @@ Execute the test setup script:
|
|
784
771
|
#### Manually (if required)
|
785
772
|
|
786
773
|
Go to the 'dummy' project:
|
787
|
-
|
774
|
+
|
788
775
|
cd ./spec/dummy
|
789
776
|
|
790
777
|
If this is your first time, create the test database
|
@@ -808,7 +795,7 @@ Go back to gem root directory:
|
|
808
795
|
* Initial Version Copyright (c) 2005 Trevor Squires
|
809
796
|
* Rails 3 Updates Copyright (c) 2010 Pivotal Labs
|
810
797
|
* Initial Test Suite Copyright (c) 2011 Sergey Potapov
|
811
|
-
* Subsequent Updates Copyright (c) 2011-
|
798
|
+
* Subsequent Updates Copyright (c) 2011-2020 Arthur Shagall
|
812
799
|
|
813
800
|
Released under the MIT License. See the LICENSE file for more details.
|
814
801
|
|
@@ -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.
|
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
|
-
|
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.
|
@@ -194,6 +206,11 @@ module PowerEnum::Enumerated
|
|
194
206
|
all.map { |item| item.name_sym }
|
195
207
|
end
|
196
208
|
|
209
|
+
# Returns all except for the given list
|
210
|
+
def all_except(*excluded)
|
211
|
+
all.find_all { |item| !(item === excluded) }
|
212
|
+
end
|
213
|
+
|
197
214
|
# Enum lookup by Symbol, String, or id. Returns <tt>arg<tt> if arg is
|
198
215
|
# an enum instance. Passing in a list of arguments returns a list of
|
199
216
|
# enums. When called with no arguments, returns nil.
|
@@ -309,15 +326,14 @@ module PowerEnum::Enumerated
|
|
309
326
|
|
310
327
|
# ---Private methods---
|
311
328
|
|
312
|
-
def load_all
|
329
|
+
private def load_all
|
313
330
|
conditions = self.acts_enumerated_conditions
|
314
331
|
order = self.acts_enumerated_order
|
315
332
|
unscoped.where(conditions).order(order)
|
316
333
|
end
|
317
|
-
private :load_all
|
318
334
|
|
319
335
|
# Looks up the enum based on the type of the argument.
|
320
|
-
def lookup_enum_by_type(arg)
|
336
|
+
private def lookup_enum_by_type(arg)
|
321
337
|
case arg
|
322
338
|
when Symbol
|
323
339
|
lookup_name(arg.id2name)
|
@@ -334,10 +350,9 @@ module PowerEnum::Enumerated
|
|
334
350
|
" be a String, Symbol or Integer but got a: #{arg.class.name}"
|
335
351
|
end
|
336
352
|
end
|
337
|
-
private :lookup_enum_by_type
|
338
353
|
|
339
354
|
# Deals with a lookup failure for the given argument.
|
340
|
-
def handle_lookup_failure(arg)
|
355
|
+
private def handle_lookup_failure(arg)
|
341
356
|
if (lookup_failure_handler = self.acts_enumerated_on_lookup_failure)
|
342
357
|
case lookup_failure_handler
|
343
358
|
when Proc
|
@@ -349,16 +364,14 @@ module PowerEnum::Enumerated
|
|
349
364
|
self.send(:enforce_none, arg)
|
350
365
|
end
|
351
366
|
end
|
352
|
-
private :handle_lookup_failure
|
353
367
|
|
354
368
|
# Returns a hash of all enumeration members keyed by their ids.
|
355
|
-
def all_by_id
|
369
|
+
private def all_by_id
|
356
370
|
@all_by_id ||= all_by_attribute( primary_key )
|
357
371
|
end
|
358
|
-
private :all_by_id
|
359
372
|
|
360
373
|
# Returns a hash of all the enumeration members keyed by their names.
|
361
|
-
def all_by_name
|
374
|
+
private def all_by_name
|
362
375
|
begin
|
363
376
|
@all_by_name ||= all_by_attribute( :__enum_name__ )
|
364
377
|
rescue NoMethodError => err
|
@@ -368,9 +381,8 @@ module PowerEnum::Enumerated
|
|
368
381
|
raise
|
369
382
|
end
|
370
383
|
end
|
371
|
-
private :all_by_name
|
372
384
|
|
373
|
-
def all_by_attribute(attr) # :nodoc:
|
385
|
+
private def all_by_attribute(attr) # :nodoc:
|
374
386
|
aba = all.inject({}) { |memo, item|
|
375
387
|
memo[item.send(attr)] = item
|
376
388
|
memo
|
@@ -378,42 +390,35 @@ module PowerEnum::Enumerated
|
|
378
390
|
aba.freeze unless enumerations_model_updating?
|
379
391
|
aba
|
380
392
|
end
|
381
|
-
private :all_by_attribute
|
382
393
|
|
383
|
-
def enforce_none(arg) # :nodoc:
|
394
|
+
private def enforce_none(arg) # :nodoc:
|
384
395
|
nil
|
385
396
|
end
|
386
|
-
private :enforce_none
|
387
397
|
|
388
|
-
def enforce_strict(arg) # :nodoc:
|
398
|
+
private def enforce_strict(arg) # :nodoc:
|
389
399
|
raise_record_not_found(arg)
|
390
400
|
end
|
391
|
-
private :enforce_strict
|
392
401
|
|
393
|
-
def enforce_strict_literals(arg) # :nodoc:
|
402
|
+
private def enforce_strict_literals(arg) # :nodoc:
|
394
403
|
raise_record_not_found(arg) if (Integer === arg) || (Symbol === arg)
|
395
404
|
nil
|
396
405
|
end
|
397
|
-
private :enforce_strict_literals
|
398
406
|
|
399
|
-
def enforce_strict_ids(arg) # :nodoc:
|
407
|
+
private def enforce_strict_ids(arg) # :nodoc:
|
400
408
|
raise_record_not_found(arg) if Integer === arg
|
401
409
|
nil
|
402
410
|
end
|
403
|
-
private :enforce_strict_ids
|
404
411
|
|
405
|
-
def enforce_strict_symbols(arg) # :nodoc:
|
412
|
+
private def enforce_strict_symbols(arg) # :nodoc:
|
406
413
|
raise_record_not_found(arg) if Symbol === arg
|
407
414
|
nil
|
408
415
|
end
|
409
|
-
private :enforce_strict_symbols
|
410
416
|
|
411
417
|
# raise the {ActiveRecord::RecordNotFound} error.
|
412
418
|
# @private
|
413
|
-
def raise_record_not_found(arg)
|
419
|
+
private def raise_record_not_found(arg)
|
414
420
|
raise ActiveRecord::RecordNotFound, "Couldn't find a #{self.name} identified by (#{arg.inspect})"
|
415
421
|
end
|
416
|
-
private :raise_record_not_found
|
417
422
|
|
418
423
|
end
|
419
424
|
|
@@ -63,6 +63,8 @@ of these associations is deprecated and will be removed in the future.
|
|
63
63
|
@active_record_primary_key ||= options[:primary_key] || active_record.primary_key
|
64
64
|
end
|
65
65
|
|
66
|
+
alias_method :join_primary_key, :active_record_primary_key
|
67
|
+
|
66
68
|
def klass
|
67
69
|
@klass ||= active_record.send(:compute_type, class_name)
|
68
70
|
end
|
@@ -87,6 +89,8 @@ of these associations is deprecated and will be removed in the future.
|
|
87
89
|
@foreign_key ||= (@options[:foreign_key] || "#{@name}_id").to_s
|
88
90
|
end
|
89
91
|
|
92
|
+
alias_method :join_foreign_key, :foreign_key
|
93
|
+
|
90
94
|
# Returns the name of the enum table
|
91
95
|
def table_name
|
92
96
|
@table_name ||= self.class.const_get(class_name).table_name
|
@@ -116,6 +120,11 @@ of these associations is deprecated and will be removed in the future.
|
|
116
120
|
false
|
117
121
|
end
|
118
122
|
|
123
|
+
# Always returns true.
|
124
|
+
def collection?
|
125
|
+
true
|
126
|
+
end
|
127
|
+
|
119
128
|
# In this case, returns [[]]
|
120
129
|
def conditions
|
121
130
|
[[]]
|
metadata
CHANGED
@@ -1,32 +1,32 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: power_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Trevor Squires
|
8
8
|
- Pivotal Labs
|
9
9
|
- Arthur Shagall
|
10
10
|
- Sergey Potapov
|
11
|
-
autorequire:
|
11
|
+
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2021-01-05 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: bundler
|
18
18
|
requirement: !ruby/object:Gem::Requirement
|
19
19
|
requirements:
|
20
|
-
- - "
|
20
|
+
- - ">"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '1.
|
22
|
+
version: '1.7'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
|
-
- - "
|
27
|
+
- - ">"
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '1.
|
29
|
+
version: '1.7'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: rake
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -64,7 +64,7 @@ dependencies:
|
|
64
64
|
version: '4.2'
|
65
65
|
- - "<"
|
66
66
|
- !ruby/object:Gem::Version
|
67
|
-
version: '
|
67
|
+
version: '7'
|
68
68
|
type: :development
|
69
69
|
prerelease: false
|
70
70
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -74,7 +74,7 @@ dependencies:
|
|
74
74
|
version: '4.2'
|
75
75
|
- - "<"
|
76
76
|
- !ruby/object:Gem::Version
|
77
|
-
version: '
|
77
|
+
version: '7'
|
78
78
|
- !ruby/object:Gem::Dependency
|
79
79
|
name: railties
|
80
80
|
requirement: !ruby/object:Gem::Requirement
|
@@ -84,7 +84,7 @@ dependencies:
|
|
84
84
|
version: '4.2'
|
85
85
|
- - "<"
|
86
86
|
- !ruby/object:Gem::Version
|
87
|
-
version: '
|
87
|
+
version: '7'
|
88
88
|
type: :runtime
|
89
89
|
prerelease: false
|
90
90
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -94,7 +94,7 @@ dependencies:
|
|
94
94
|
version: '4.2'
|
95
95
|
- - "<"
|
96
96
|
- !ruby/object:Gem::Version
|
97
|
-
version: '
|
97
|
+
version: '7'
|
98
98
|
- !ruby/object:Gem::Dependency
|
99
99
|
name: activerecord
|
100
100
|
requirement: !ruby/object:Gem::Requirement
|
@@ -104,7 +104,7 @@ dependencies:
|
|
104
104
|
version: '4.2'
|
105
105
|
- - "<"
|
106
106
|
- !ruby/object:Gem::Version
|
107
|
-
version: '
|
107
|
+
version: '7'
|
108
108
|
type: :runtime
|
109
109
|
prerelease: false
|
110
110
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -114,7 +114,7 @@ dependencies:
|
|
114
114
|
version: '4.2'
|
115
115
|
- - "<"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: '
|
117
|
+
version: '7'
|
118
118
|
description: |
|
119
119
|
Power Enum allows you to treat instances of your ActiveRecord models as though they were an enumeration of values.
|
120
120
|
It allows you to cleanly solve many of the problems that the traditional Rails alternatives handle poorly if at all.
|
@@ -149,7 +149,7 @@ homepage: http://github.com/albertosaurus/power_enum_2
|
|
149
149
|
licenses:
|
150
150
|
- MIT
|
151
151
|
metadata: {}
|
152
|
-
post_install_message:
|
152
|
+
post_install_message:
|
153
153
|
rdoc_options: []
|
154
154
|
require_paths:
|
155
155
|
- lib
|
@@ -164,9 +164,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
164
164
|
- !ruby/object:Gem::Version
|
165
165
|
version: '0'
|
166
166
|
requirements: []
|
167
|
-
|
168
|
-
|
169
|
-
signing_key:
|
167
|
+
rubygems_version: 3.0.8
|
168
|
+
signing_key:
|
170
169
|
specification_version: 4
|
171
170
|
summary: Allows you to treat instances of your ActiveRecord models as though they
|
172
171
|
were an enumeration of values
|