power_enum 4.2.0 → 4.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 +4 -4
- data/README.markdown +33 -4
- data/VERSION +1 -1
- data/lib/power_enum/enumerated.rb +43 -9
- data/lib/power_enum/has_enumerated.rb +10 -2
- data/lib/power_enum/reflection.rb +5 -0
- metadata +16 -16
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1472eb40672379c9033d7bd7dc3e35d545160363f33d7b05b00e174dd05530a1
|
|
4
|
+
data.tar.gz: cdacd983f5bfe94e07bed56e4a54225a6c4c54fb3d5489aacb5beb7e5886571f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5395b07f622523606b08e2d81a3a68388abe12841f7e8277a223aae2dbfb488301f2b648241c75c623e50ac968134fc9962b0022982b46262fad6749ffe73347
|
|
7
|
+
data.tar.gz: ce2caa8a50801b3f004b3d624660827d9c39bbb8298cfe1a748a949ae4d0c8908bb6cd2712179ef6705e1cb10d92bc73e6ffbd4c09c1133e8e5ffbf3c2447590
|
data/README.markdown
CHANGED
|
@@ -10,7 +10,8 @@ Enumerations for Rails Done Right.
|
|
|
10
10
|
|
|
11
11
|
## Versions
|
|
12
12
|
|
|
13
|
-
* PowerEnum 4.
|
|
13
|
+
* PowerEnum 4.3+ supports Rails 8.X (Experimental)
|
|
14
|
+
* PowerEnum 4.0.X supports Rails 6.X, and Rails 7.0 (Experimental)
|
|
14
15
|
* PowerEnum 3.X supports Rails 4.2, Rails 5.X and Rails 6.0
|
|
15
16
|
* PowerEnum 2.X supports Rails 4.X and Rails 5.0
|
|
16
17
|
* PowerEnum 1.X supports Rails 3.1/3.2, available here: https://github.com/albertosaurus/power_enum
|
|
@@ -53,6 +54,11 @@ See "How to use it" below for more information.
|
|
|
53
54
|
|
|
54
55
|
## Requirements
|
|
55
56
|
|
|
57
|
+
### PowerEnum 4.3+
|
|
58
|
+
|
|
59
|
+
* Ruby 3.1 or later (JRuby should work but isn't extensively tested).
|
|
60
|
+
* Rails 8.X
|
|
61
|
+
|
|
56
62
|
### PowerEnum 4.0.X
|
|
57
63
|
|
|
58
64
|
* Ruby 2.7 or later (JRuby should work but isn't extensively tested).
|
|
@@ -299,7 +305,7 @@ With that, your BookingStatus class will have the following methods defined:
|
|
|
299
305
|
|
|
300
306
|
#### Class Methods
|
|
301
307
|
|
|
302
|
-
##### [](*args)
|
|
308
|
+
##### [](*args, &block)
|
|
303
309
|
|
|
304
310
|
`BookingStatus[arg]` performs a lookup for the BookingStatus instance for the given arg. The arg value can be a
|
|
305
311
|
'string' or a :symbol, in which case the lookup will be against the BookingStatus.name field. Alternatively arg can be
|
|
@@ -309,8 +315,10 @@ instance of the enum (in this case BookingStatus) as a convenience.
|
|
|
309
315
|
The `:on_lookup_failure` option specifies the name of a *class* method to invoke when the `[]` method is unable to
|
|
310
316
|
locate a BookingStatus record for arg. The default is the built-in `:enforce_none` which returns nil. There are also
|
|
311
317
|
built-ins for `:enforce_strict` (raise and exception regardless of the type for arg), `:enforce_strict_literals` (raises
|
|
312
|
-
an exception if the arg is a Integer or Symbol), `:enforce_strict_ids` (raises and exception if the arg is a Integer)
|
|
313
|
-
`:enforce_strict_symbols` (raises an exception if the arg is a Symbol)
|
|
318
|
+
an exception if the arg is a Integer or Symbol), `:enforce_strict_ids` (raises and exception if the arg is a Integer),
|
|
319
|
+
`:enforce_strict_symbols` (raises an exception if the arg is a Symbol), `:insert_new_record` (will insert a new
|
|
320
|
+
record - NOTE: only supported if the DB back end implements atomic UPSERT), and `:call_block`. Alternatively set
|
|
321
|
+
`:on_lookup_failure` to a proc for a more generic handler.
|
|
314
322
|
|
|
315
323
|
The purpose of the `:on_lookup_failure` option is that a) under some circumstances a lookup failure is a Bad Thing and
|
|
316
324
|
action should be taken, therefore b) a fallback action should be easily configurable. You can
|
|
@@ -320,6 +328,27 @@ You can also pass in multiple arguments to `[]`. This returns a list of enums co
|
|
|
320
328
|
passed in values. Duplicates are filtered out. For example `BookingStatus[arg1, arg2, arg3]` would be equivalent to
|
|
321
329
|
`[BookingStatus[arg1], BookingStatus[arg2], BookingStatus[arg3]]`.
|
|
322
330
|
|
|
331
|
+
Example:
|
|
332
|
+
|
|
333
|
+
```ruby
|
|
334
|
+
class BookingStatus < ActiveRecord::Base
|
|
335
|
+
acts_as_enumerated on_lookup_failure: :call_block
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
# Given a BookingStatus, whose possible members are "pending", "closed", or "canceled":
|
|
339
|
+
|
|
340
|
+
# Returns nil
|
|
341
|
+
BookingStatus[:expired]
|
|
342
|
+
|
|
343
|
+
# Returns the equivalent of BookingStatus[:pending, :closed]
|
|
344
|
+
BookingStatus[:Pending, :Closed] { |klass, arg| klass[arg.to_s.downcase] }
|
|
345
|
+
|
|
346
|
+
# Inserts a new record - CAVEAT EMPTOR
|
|
347
|
+
BookingStatus[:expired] do |klass, arg|
|
|
348
|
+
klass.insert_new_record(arg, description: "#{arg.to_s.capitalize} - inserted inline")
|
|
349
|
+
end
|
|
350
|
+
```
|
|
351
|
+
|
|
323
352
|
##### contains?(arg)
|
|
324
353
|
|
|
325
354
|
`BookingStatus.contains?(arg)` returns `true if` the given Symbol, String or id has a member instance in the enumeration,
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
4.
|
|
1
|
+
4.5.0
|
|
@@ -30,10 +30,13 @@ module PowerEnum
|
|
|
30
30
|
# record for arg. The default is the built-in :enforce_none which returns nil. There are also built-ins for
|
|
31
31
|
# :enforce_strict (raise and exception regardless of the type for arg), :enforce_strict_literals (raises an
|
|
32
32
|
# exception if the arg is a Integer or Symbol), :enforce_strict_ids (raises and exception if the arg is a
|
|
33
|
-
# Integer)
|
|
34
|
-
#
|
|
35
|
-
#
|
|
36
|
-
#
|
|
33
|
+
# Integer), :enforce_strict_symbols (raises an exception if the arg is a Symbol), :insert_new_record (see
|
|
34
|
+
# below), and :call_block (See documentation for +[arg]+).
|
|
35
|
+
# The purpose of the :on_lookup_failure option is that a) under some circumstances a lookup failure is a
|
|
36
|
+
# Bad Thing and action should be taken, therefore b) a fallback action should be easily configurable.
|
|
37
|
+
# You can also give it a lambda that takes in a single argument (The arg that was passed to +[]+).
|
|
38
|
+
# Note the :insert_new_record option - it will insert a new record into the database and update the enumerations
|
|
39
|
+
# cache. NOTE: This option is only supported where the database back end implements an atomic UPSERT operation.
|
|
37
40
|
# [:name_column]
|
|
38
41
|
# Override for the 'name' column. By default, assumed to be 'name'.
|
|
39
42
|
# [:alias_name]
|
|
@@ -218,15 +221,19 @@ module PowerEnum
|
|
|
218
221
|
# Enum lookup by Symbol, String, or id. Returns <tt>arg<tt> if arg is
|
|
219
222
|
# an enum instance. Passing in a list of arguments returns a list of
|
|
220
223
|
# enums. When called with no arguments, returns nil.
|
|
221
|
-
|
|
224
|
+
#
|
|
225
|
+
# If the given arg is not in the enumerations cache, the lookup failure handler is set to :call_block
|
|
226
|
+
# and an optional block of the form { |klass, arg| } is passed to this method, the block will be
|
|
227
|
+
# invoked with the first argument being this instance and the second the given argument.
|
|
228
|
+
def [](*args, &block)
|
|
222
229
|
case args.size
|
|
223
230
|
when 0
|
|
224
231
|
nil
|
|
225
232
|
when 1
|
|
226
233
|
arg = args.first
|
|
227
|
-
Array === arg ? self[*arg] : (lookup_enum_by_type(arg) || handle_lookup_failure(arg))
|
|
234
|
+
Array === arg ? self[*arg, &block] : (lookup_enum_by_type(arg) || handle_lookup_failure(arg, &block))
|
|
228
235
|
else
|
|
229
|
-
args.map{ |item| self[item] }.uniq
|
|
236
|
+
args.map{ |item| self[item, &block] }.uniq
|
|
230
237
|
end
|
|
231
238
|
end
|
|
232
239
|
|
|
@@ -356,13 +363,13 @@ module PowerEnum
|
|
|
356
363
|
end
|
|
357
364
|
|
|
358
365
|
# Deals with a lookup failure for the given argument.
|
|
359
|
-
private def handle_lookup_failure(arg)
|
|
366
|
+
private def handle_lookup_failure(arg, &block)
|
|
360
367
|
if (lookup_failure_handler = self.acts_enumerated_on_lookup_failure)
|
|
361
368
|
case lookup_failure_handler
|
|
362
369
|
when Proc
|
|
363
370
|
lookup_failure_handler.call(arg)
|
|
364
371
|
else
|
|
365
|
-
self.send(lookup_failure_handler, arg)
|
|
372
|
+
self.send(lookup_failure_handler, arg, &block)
|
|
366
373
|
end
|
|
367
374
|
else
|
|
368
375
|
self.send(:enforce_none, arg)
|
|
@@ -418,6 +425,33 @@ module PowerEnum
|
|
|
418
425
|
nil
|
|
419
426
|
end
|
|
420
427
|
|
|
428
|
+
# Insert a new record if +arg+ is a Symbol or a String, and flush the enumerations cache.
|
|
429
|
+
def insert_new_record(arg, description: nil)
|
|
430
|
+
if Symbol === arg || String === arg
|
|
431
|
+
value = arg.to_s
|
|
432
|
+
upsert_attributes = if column_names.include?("description")
|
|
433
|
+
{ acts_enumerated_name_column => value, description: description || value.capitalize.gsub("_", " ") }
|
|
434
|
+
else
|
|
435
|
+
{ acts_enumerated_name_column => value }
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
update_enumerations_model do
|
|
439
|
+
upsert(upsert_attributes, update_only: [], unique_by: acts_enumerated_name_column)
|
|
440
|
+
end
|
|
441
|
+
self[arg]
|
|
442
|
+
else
|
|
443
|
+
nil
|
|
444
|
+
end
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
private def call_block(arg, &block)
|
|
448
|
+
if block_given?
|
|
449
|
+
block.call self, arg
|
|
450
|
+
else
|
|
451
|
+
enforce_none(arg)
|
|
452
|
+
end
|
|
453
|
+
end
|
|
454
|
+
|
|
421
455
|
# raise the {ActiveRecord::RecordNotFound} error.
|
|
422
456
|
# @private
|
|
423
457
|
private def raise_record_not_found(arg)
|
|
@@ -165,9 +165,17 @@ module PowerEnum
|
|
|
165
165
|
when #{class_name}
|
|
166
166
|
val = #{class_name}.lookup_id(arg.id)
|
|
167
167
|
when String
|
|
168
|
-
val = #{class_name}.
|
|
168
|
+
val = if #{class_name}.acts_enumerated_on_lookup_failure.to_s == 'insert_new_record'
|
|
169
|
+
#{class_name}[arg]
|
|
170
|
+
else
|
|
171
|
+
#{class_name}.lookup_name(arg)
|
|
172
|
+
end
|
|
169
173
|
when Symbol
|
|
170
|
-
val = #{class_name}.
|
|
174
|
+
val = if #{class_name}.acts_enumerated_on_lookup_failure.to_s == 'insert_new_record'
|
|
175
|
+
#{class_name}[arg.id2name]
|
|
176
|
+
else
|
|
177
|
+
#{class_name}.lookup_name(arg.id2name)
|
|
178
|
+
end
|
|
171
179
|
when Integer
|
|
172
180
|
val = #{class_name}.lookup_id(arg)
|
|
173
181
|
when nil
|
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: 4.
|
|
4
|
+
version: 4.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:
|
|
14
|
+
date: 2026-09-01 00:00:00.000000000 Z
|
|
15
15
|
dependencies:
|
|
16
16
|
- !ruby/object:Gem::Dependency
|
|
17
17
|
name: bundler
|
|
@@ -61,60 +61,60 @@ dependencies:
|
|
|
61
61
|
requirements:
|
|
62
62
|
- - ">="
|
|
63
63
|
- !ruby/object:Gem::Version
|
|
64
|
-
version: '
|
|
64
|
+
version: '7.0'
|
|
65
65
|
- - "<"
|
|
66
66
|
- !ruby/object:Gem::Version
|
|
67
|
-
version: '
|
|
67
|
+
version: '9'
|
|
68
68
|
type: :development
|
|
69
69
|
prerelease: false
|
|
70
70
|
version_requirements: !ruby/object:Gem::Requirement
|
|
71
71
|
requirements:
|
|
72
72
|
- - ">="
|
|
73
73
|
- !ruby/object:Gem::Version
|
|
74
|
-
version: '
|
|
74
|
+
version: '7.0'
|
|
75
75
|
- - "<"
|
|
76
76
|
- !ruby/object:Gem::Version
|
|
77
|
-
version: '
|
|
77
|
+
version: '9'
|
|
78
78
|
- !ruby/object:Gem::Dependency
|
|
79
79
|
name: railties
|
|
80
80
|
requirement: !ruby/object:Gem::Requirement
|
|
81
81
|
requirements:
|
|
82
82
|
- - ">="
|
|
83
83
|
- !ruby/object:Gem::Version
|
|
84
|
-
version: '
|
|
84
|
+
version: '7.0'
|
|
85
85
|
- - "<"
|
|
86
86
|
- !ruby/object:Gem::Version
|
|
87
|
-
version: '
|
|
87
|
+
version: '9'
|
|
88
88
|
type: :runtime
|
|
89
89
|
prerelease: false
|
|
90
90
|
version_requirements: !ruby/object:Gem::Requirement
|
|
91
91
|
requirements:
|
|
92
92
|
- - ">="
|
|
93
93
|
- !ruby/object:Gem::Version
|
|
94
|
-
version: '
|
|
94
|
+
version: '7.0'
|
|
95
95
|
- - "<"
|
|
96
96
|
- !ruby/object:Gem::Version
|
|
97
|
-
version: '
|
|
97
|
+
version: '9'
|
|
98
98
|
- !ruby/object:Gem::Dependency
|
|
99
99
|
name: activerecord
|
|
100
100
|
requirement: !ruby/object:Gem::Requirement
|
|
101
101
|
requirements:
|
|
102
102
|
- - ">="
|
|
103
103
|
- !ruby/object:Gem::Version
|
|
104
|
-
version: '
|
|
104
|
+
version: '7.0'
|
|
105
105
|
- - "<"
|
|
106
106
|
- !ruby/object:Gem::Version
|
|
107
|
-
version: '
|
|
107
|
+
version: '9'
|
|
108
108
|
type: :runtime
|
|
109
109
|
prerelease: false
|
|
110
110
|
version_requirements: !ruby/object:Gem::Requirement
|
|
111
111
|
requirements:
|
|
112
112
|
- - ">="
|
|
113
113
|
- !ruby/object:Gem::Version
|
|
114
|
-
version: '
|
|
114
|
+
version: '7.0'
|
|
115
115
|
- - "<"
|
|
116
116
|
- !ruby/object:Gem::Version
|
|
117
|
-
version: '
|
|
117
|
+
version: '9'
|
|
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.
|
|
@@ -159,14 +159,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
159
159
|
requirements:
|
|
160
160
|
- - ">="
|
|
161
161
|
- !ruby/object:Gem::Version
|
|
162
|
-
version:
|
|
162
|
+
version: 3.1.0
|
|
163
163
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
164
164
|
requirements:
|
|
165
165
|
- - ">="
|
|
166
166
|
- !ruby/object:Gem::Version
|
|
167
167
|
version: '0'
|
|
168
168
|
requirements: []
|
|
169
|
-
rubygems_version: 3.
|
|
169
|
+
rubygems_version: 3.3.26
|
|
170
170
|
signing_key:
|
|
171
171
|
specification_version: 4
|
|
172
172
|
summary: Allows you to treat instances of your ActiveRecord models as though they
|