enumbler 0.10.0 → 0.12.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/CHANGELOG.md +40 -0
- data/README.md +84 -3
- data/lib/enumbler/enabler.rb +99 -0
- data/lib/enumbler/enumble.rb +28 -4
- data/lib/enumbler/version.rb +1 -1
- data/lib/enumbler.rb +26 -0
- metadata +11 -35
- data/.github/workflows/ruby.yml +0 -71
- data/.gitignore +0 -14
- data/.rspec +0 -3
- data/.rubocop.yml +0 -76
- data/.ruby-version +0 -1
- data/.solargraph.yml +0 -15
- data/AGENTS.md +0 -13
- data/Gemfile +0 -28
- data/Gemfile.lock +0 -124
- data/Gemfile.rails6.0 +0 -28
- data/Gemfile.rails6.0.lock +0 -99
- data/Gemfile.rails6.1 +0 -28
- data/Gemfile.rails6.1.lock +0 -98
- data/Gemfile.rails7.0 +0 -28
- data/Gemfile.rails7.0.lock +0 -96
- data/Gemfile.rails7.1 +0 -28
- data/Gemfile.rails7.1.lock +0 -114
- data/Gemfile.rails7.2 +0 -28
- data/Gemfile.rails7.2.lock +0 -112
- data/Gemfile.rails8.0 +0 -28
- data/Gemfile.rails8.0.lock +0 -112
- data/Rakefile +0 -8
- data/bin/console +0 -15
- data/bin/pre_commit_rubocop.rb +0 -43
- data/bin/setup +0 -13
- data/enumbler.gemspec +0 -35
- data/lefthook.yml +0 -12
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8976fda559de218589436c6fba7fe3e31d2f43ecefdb989d1ac30e54581973f0
|
|
4
|
+
data.tar.gz: b592b99299c6c6656d5f4914c0a487d82ba9b56589b8c0d57deb03b206b1a057
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 374808a1e72ad4bf6104cacf52ea5e00e8eecfd341395f667083caa0469f57a2e39630cf2601583274c36de71da4a978b2e7c45bec4e35e4043559736d4e3c13
|
|
7
|
+
data.tar.gz: 2b5e72f8985aaf052d24997c6285b71c4a2d323b30f9a99e2b287116d94c39e9a9f2be90d7d06effbc3c05662fda0020748299670724590030d32148986aed81
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this gem are recorded here. Versions follow the bare
|
|
4
|
+
git tags on `main` (no `v` prefix).
|
|
5
|
+
|
|
6
|
+
## 0.12.0
|
|
7
|
+
|
|
8
|
+
### Added
|
|
9
|
+
|
|
10
|
+
- `enumbler_allow_duplicate_labels`: a per-model opt-in, declared before the
|
|
11
|
+
first `enumble`, that lets enumbles share a label. Duplicates are then
|
|
12
|
+
rejected only on id or enum, and Enumbles that share only a label are no
|
|
13
|
+
longer `==`/`eql?`. On such a model a String lookup that matches an enum
|
|
14
|
+
name resolves to it; one that matches more than one label raises the new
|
|
15
|
+
`Enumbler::AmbiguousLabelError` (an `Enumbler::Error`), even without
|
|
16
|
+
`raise_error`, and `find_by_enumble` passes it through rather than raising
|
|
17
|
+
`ActiveRecord::RecordNotFound`. Exact-case matches rank above case-insensitive
|
|
18
|
+
ones, and within each an enum name ranks above a label. Models that do not
|
|
19
|
+
opt in are unchanged.
|
|
20
|
+
|
|
21
|
+
### Changed
|
|
22
|
+
|
|
23
|
+
- **Minimum Ruby is now 3.2** (was 3.1). Ruby 3.1 is end-of-life, and the
|
|
24
|
+
patched `sqlite3` the test suite needs no longer installs on it.
|
|
25
|
+
ActiveRecord/ActiveSupport support is unchanged (>= 6.0, < 9).
|
|
26
|
+
- Rails 8.1 is now in the CI matrix, as is Ruby 4.0 (with Rails 7.1+).
|
|
27
|
+
- Gem metadata points at `github.com/octoopi/enumbler` and this changelog,
|
|
28
|
+
and the packaged gem ships only `lib/`, the README, the changelog and the
|
|
29
|
+
license.
|
|
30
|
+
|
|
31
|
+
## 0.11.0
|
|
32
|
+
|
|
33
|
+
- Added `Enumbler.suppress_database_warnings` (and the
|
|
34
|
+
`ENUMBLER_SUPPRESS_DATABASE_WARNINGS` environment variable) to silence the
|
|
35
|
+
boot-time `[Enumbler Warning]` messages.
|
|
36
|
+
- Releases publish through RubyGems trusted publishing from CI.
|
|
37
|
+
|
|
38
|
+
## 0.10.0
|
|
39
|
+
|
|
40
|
+
- Support for Rails 7.x and 8.0.
|
data/README.md
CHANGED
|
@@ -149,6 +149,44 @@ Feeling.verklempt.enumble.label # => 'overcome with emotion'
|
|
|
149
149
|
Feeling.verklempt.emotion # => 'overcome with emotion'
|
|
150
150
|
```
|
|
151
151
|
|
|
152
|
+
### Allow duplicate labels
|
|
153
|
+
|
|
154
|
+
Every enumble in a model normally needs a unique id, enum, **and** label: a second `enumble` that repeats any of the three raises `You cannot add the same Enumble twice!`. When labels genuinely repeat, say a `Topic` table where "Classroom" appears under two different parents, opt the model in with `enumbler_allow_duplicate_labels` before its first `enumble`:
|
|
155
|
+
|
|
156
|
+
```ruby
|
|
157
|
+
ActiveRecord::Schema.define do
|
|
158
|
+
create_table :topics, force: true do |t|
|
|
159
|
+
t.string :label, null: false # no unique index: labels repeat
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
class Topic < ApplicationRecord
|
|
164
|
+
include Enumbler::Enabler
|
|
165
|
+
|
|
166
|
+
enumbler_allow_duplicate_labels
|
|
167
|
+
|
|
168
|
+
enumble :k12_classroom, 17, label: "Classroom"
|
|
169
|
+
enumble :higher_ed_classroom, 34, label: "Classroom"
|
|
170
|
+
enumble :science, 20, label: "Science"
|
|
171
|
+
end
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
With it on, an enumble is a duplicate only when its **id or enum** is already taken. Enum names are still unique, so constants (`Topic::K12_CLASSROOM`), class shortcuts (`Topic.k12_classroom`), predicates (`topic.k12_classroom?`), `graphql_enum`, and lookups by id, symbol, Enumble, or record all work as before. Two Enumbles that share only a label are no longer `==`.
|
|
175
|
+
|
|
176
|
+
String lookups (`find_enumble(s)`, `find_by_enumble`, `ids_from_enumbler`, the `enumbled_to` scopes, and their bang versions) become stricter:
|
|
177
|
+
|
|
178
|
+
```ruby
|
|
179
|
+
Topic.find_enumble("higher_ed_classroom") # => Enumbler::Enumble<:higher_ed_classroom>, by enum name
|
|
180
|
+
Topic.find_enumble("Science") # => Enumbler::Enumble<:science>, one label match resolves
|
|
181
|
+
Topic.find_enumble("Art") # => nil, as before
|
|
182
|
+
Topic.find_enumble("Classroom") # => raises Enumbler::AmbiguousLabelError
|
|
183
|
+
Lesson.topic("Classroom") # => raises Enumbler::AmbiguousLabelError (Lesson `enumbled_to :topic`)
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
A String that matches more than one label raises `Enumbler::AmbiguousLabelError` (a subclass of `Enumbler::Error`) **whether or not** you asked for an error on a missing value, rather than silently returning whichever enumble was defined first. `find_by_enumble` lets it through instead of turning it into `ActiveRecord::RecordNotFound`. Matching follows `case_sensitive:` as usual, so `"classroom"` is ambiguous too unless you pass `case_sensitive: true`. Exact-case matches rank above case-insensitive ones, and within each an enum name ranks above a label. So with an enum `:classroom` labeled "Classroom" and a second "Classroom" label, `"classroom"` resolves to `:classroom` but `"Classroom"` raises: it matches two labels exactly, and resolving it to an enum that only matches ignoring case would be a guess. Likewise `"Classroom"` still resolves when the only other label is `"classroom"`.
|
|
187
|
+
|
|
188
|
+
Models that do not call `enumbler_allow_duplicate_labels` behave exactly as they always have.
|
|
189
|
+
|
|
152
190
|
## Core ext
|
|
153
191
|
|
|
154
192
|
Adds case equality power to the `Symbol` class allowing you to use case methods directly against an enabled instance:
|
|
@@ -162,15 +200,58 @@ when :blue, :purple
|
|
|
162
200
|
end
|
|
163
201
|
```
|
|
164
202
|
|
|
203
|
+
## Suppressing database warnings
|
|
204
|
+
|
|
205
|
+
When an `enumble` defines extra attributes, the Enumbler verifies them against the model's table at class-load time. If the table cannot be inspected — for example, while your test databases are dropped mid-way through a `rake parallel:setup` — it prints an `[Enumbler Warning]` for every affected model rather than raising. If those warnings are just noise in your workflow, you can suppress them:
|
|
206
|
+
|
|
207
|
+
```ruby
|
|
208
|
+
Enumbler.suppress_database_warnings = true
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Or set the environment variable `ENUMBLER_SUPPRESS_DATABASE_WARNINGS` to any non-empty value — handy for wrapping database reset commands:
|
|
212
|
+
|
|
213
|
+
```bash
|
|
214
|
+
ENUMBLER_SUPPRESS_DATABASE_WARNINGS=1 bundle exec rake parallel:setup
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
The Ruby-level setting, when assigned, takes precedence over the environment variable.
|
|
218
|
+
|
|
165
219
|
## Development
|
|
166
220
|
|
|
167
221
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
168
222
|
|
|
169
|
-
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
223
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
224
|
+
|
|
225
|
+
### Branching (modified git flow)
|
|
226
|
+
|
|
227
|
+
Day-to-day work follows git flow: feature/bug/chore branches come off `develop` and are merged back into `develop` via PR. Releases move `develop` into `main` through a `release/x.y.z` branch (or a `hotfix/x.y.z` branch off `main` for urgent fixes), and after tagging, the tag is merged back into `develop` so both branches share the release commit.
|
|
228
|
+
|
|
229
|
+
### Releasing a New Version
|
|
230
|
+
|
|
231
|
+
A release is triggered by pushing a **version tag** to GitHub: the `CI Matrix Testing` workflow runs the rspec matrix and, if green, the `Publish Gem` job builds the gem and pushes it to [rubygems.org](https://rubygems.org).
|
|
232
|
+
|
|
233
|
+
Two things to know before tagging:
|
|
234
|
+
|
|
235
|
+
1. The published version comes from `lib/enumbler/version.rb`, **not** the tag name — make sure they match (the tag is bare, no `v` prefix: `0.10.0`).
|
|
236
|
+
2. RubyGems rejects duplicate versions, so every release needs a version bump.
|
|
237
|
+
|
|
238
|
+
Steps:
|
|
239
|
+
|
|
240
|
+
1. Bump `Enumbler::VERSION` in `lib/enumbler/version.rb` and add its section to `CHANGELOG.md`, then refresh every lockfile so it records the new version (`for g in Gemfile Gemfile.rails*[0-9]; do BUNDLE_GEMFILE=$g bundle lock; done`) on a `release/x.y.z` branch off `develop` (or `hotfix/x.y.z` off `main`).
|
|
241
|
+
2. Merge the branch into `main`, then tag the release commit:
|
|
242
|
+
|
|
243
|
+
```bash
|
|
244
|
+
git checkout main && git pull
|
|
245
|
+
git tag -a 0.10.0 -m "Release 0.10.0"
|
|
246
|
+
git push origin main 0.10.0
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
3. Watch the `CI Matrix Testing` workflow on the tag: when the publish job is green, confirm the new version on [rubygems.org](https://rubygems.org/gems/enumbler).
|
|
250
|
+
4. Merge the tag back into `develop`: `git checkout develop && git merge 0.10.0 && git push`.
|
|
170
251
|
|
|
171
252
|
### Matrix testing
|
|
172
253
|
|
|
173
|
-
|
|
254
|
+
The gem supports Ruby >= 3.2 and ActiveRecord/ActiveSupport >= 6.0, < 9. CI runs the rspec matrix across those using one `Gemfile.rails*` per Rails minor (6.0 through 8.1); the main `Gemfile` tracks the newest Rails. To test against one of them locally, run:
|
|
174
255
|
|
|
175
256
|
```bash
|
|
176
257
|
BUNDLE_GEMFILE=Gemfile.rails7.2 bundle install
|
|
@@ -183,7 +264,7 @@ BUNDLE_GEMFILE=Gemfile.rails7.2 bundle exec rspec
|
|
|
183
264
|
|
|
184
265
|
## Contributing
|
|
185
266
|
|
|
186
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
267
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/octoopi/enumbler.
|
|
187
268
|
|
|
188
269
|
## License
|
|
189
270
|
|
data/lib/enumbler/enabler.rb
CHANGED
|
@@ -99,6 +99,7 @@ module Enumbler
|
|
|
99
99
|
@enumbler_label_column_name ||= :label
|
|
100
100
|
|
|
101
101
|
enumble = Enumble.new(enum, id, label: label, label_column_name: @enumbler_label_column_name, **attributes)
|
|
102
|
+
enumble.allow_duplicate_labels! if enumbler_allow_duplicate_labels?
|
|
102
103
|
|
|
103
104
|
if @enumbles.include?(enumble)
|
|
104
105
|
raise Error, "You cannot add the same Enumble twice! Attempted to add: #{enum}, #{id}."
|
|
@@ -133,6 +134,49 @@ module Enumbler
|
|
|
133
134
|
@enumbler_label_column_name = label_column_name
|
|
134
135
|
end
|
|
135
136
|
|
|
137
|
+
# By default every enumble in a model needs a unique id, enum, and
|
|
138
|
+
# label. Call this before your first `enumble` to let labels repeat:
|
|
139
|
+
# an enumble is then rejected as a duplicate only when its id or enum is
|
|
140
|
+
# already taken. Enum names stay unique, so constants, class
|
|
141
|
+
# shortcuts, predicates, and `graphql_enum` are unaffected.
|
|
142
|
+
#
|
|
143
|
+
# class Topic < ApplicationRecord
|
|
144
|
+
# include Enumbler::Enabler
|
|
145
|
+
#
|
|
146
|
+
# enumbler_allow_duplicate_labels
|
|
147
|
+
#
|
|
148
|
+
# enumble :k12_classroom, 17, label: "Classroom"
|
|
149
|
+
# enumble :higher_ed_classroom, 34, label: "Classroom"
|
|
150
|
+
# end
|
|
151
|
+
#
|
|
152
|
+
# A String lookup (`find_enumble`, `find_by_enumble`, `ids_from_enumbler`
|
|
153
|
+
# and friends) that matches an enum name still resolves to that enumble.
|
|
154
|
+
# One that matches no enum name but more than one label raises
|
|
155
|
+
# {Enumbler::AmbiguousLabelError}, even without `raise_error`, rather
|
|
156
|
+
# than return whichever was defined first. Exact-case matches beat
|
|
157
|
+
# case-insensitive ones; within each, an enum name beats a label. So
|
|
158
|
+
# with `:classroom` labeled "Classroom" and a second "Classroom" label,
|
|
159
|
+
# "classroom" resolves to `:classroom` but "Classroom" raises.
|
|
160
|
+
#
|
|
161
|
+
# Remember to drop any unique index on the label column.
|
|
162
|
+
#
|
|
163
|
+
# @param allow [Boolean] (default: true)
|
|
164
|
+
# @raise [Error] when called after the model has defined an enumble
|
|
165
|
+
def enumbler_allow_duplicate_labels(allow = true) # rubocop:disable Style/OptionalBooleanParameter
|
|
166
|
+
if @enumbles.present?
|
|
167
|
+
raise Error, "Call `enumbler_allow_duplicate_labels` on #{self} before its first `enumble`."
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
@enumbler_allow_duplicate_labels = allow ? true : false
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Whether this model lets enumbles share a label. See
|
|
174
|
+
# {.enumbler_allow_duplicate_labels}.
|
|
175
|
+
# @return [Boolean]
|
|
176
|
+
def enumbler_allow_duplicate_labels?
|
|
177
|
+
@enumbler_allow_duplicate_labels || false
|
|
178
|
+
end
|
|
179
|
+
|
|
136
180
|
# Like `ActiveRecord#find_by`, will try and return an instance of this
|
|
137
181
|
# model that matches any of our enumble attributes (instance, id, string,
|
|
138
182
|
# or symbol).
|
|
@@ -155,6 +199,8 @@ module Enumbler
|
|
|
155
199
|
|
|
156
200
|
find_by = raise_error ? :find_by! : :find_by
|
|
157
201
|
@enumbled_model.public_send(find_by, id: id)
|
|
202
|
+
rescue Enumbler::AmbiguousLabelError
|
|
203
|
+
raise
|
|
158
204
|
rescue Enumbler::Error
|
|
159
205
|
raise ActiveRecord::RecordNotFound.new("Couldn't find #{@enumbled_model}", @enumbled_model)
|
|
160
206
|
end
|
|
@@ -182,6 +228,9 @@ module Enumbler
|
|
|
182
228
|
# want one argument to be found and not returned in an array.
|
|
183
229
|
# @raise [Error] when there is no [Enumbler::Enumble] to be found and
|
|
184
230
|
# `raise_error: true`
|
|
231
|
+
# @raise [AmbiguousLabelError] on a model with
|
|
232
|
+
# {.enumbler_allow_duplicate_labels}, when a String matches more than
|
|
233
|
+
# one label (regardless of `raise_error`)
|
|
185
234
|
# @param args [Integer, String, Symbol]
|
|
186
235
|
# @param case_sensitive [Boolean] should a String search be case sensitive
|
|
187
236
|
# (default: false)
|
|
@@ -197,6 +246,9 @@ module Enumbler
|
|
|
197
246
|
# if none found.
|
|
198
247
|
# @raise [Error] when there is no [Enumbler::Enumble] to be found and
|
|
199
248
|
# `raise_error: true`
|
|
249
|
+
# @raise [AmbiguousLabelError] on a model with
|
|
250
|
+
# {.enumbler_allow_duplicate_labels}, when a String matches more than
|
|
251
|
+
# one label (regardless of `raise_error`)
|
|
200
252
|
# @param args [Integer, String, Symbol]
|
|
201
253
|
# @param case_sensitive [Boolean] should a String search be case sensitive
|
|
202
254
|
# (default: false)
|
|
@@ -217,6 +269,9 @@ module Enumbler
|
|
|
217
269
|
#
|
|
218
270
|
# @raise [Error] when there is no [Enumbler::Enumble] to be found and
|
|
219
271
|
# `raise_error: true`
|
|
272
|
+
# @raise [AmbiguousLabelError] on a model with
|
|
273
|
+
# {.enumbler_allow_duplicate_labels}, when a String matches more than
|
|
274
|
+
# one label (regardless of `raise_error`)
|
|
220
275
|
# @param args [Integer, String, Symbol]
|
|
221
276
|
# @param case_sensitive [Boolean] should a String search be case sensitive
|
|
222
277
|
# (default: false)
|
|
@@ -236,6 +291,8 @@ module Enumbler
|
|
|
236
291
|
@enumbled_model.enumbles.find { |e| e.enum == arg }
|
|
237
292
|
elsif arg.is_a?(Enumbler::Enumble)
|
|
238
293
|
@enumbled_model.enumbles.find { |e| e.enum == arg.enum }
|
|
294
|
+
elsif arg.is_a?(String) && @enumbled_model.enumbler_allow_duplicate_labels?
|
|
295
|
+
find_enumble_by_string_allowing_duplicate_labels(arg, case_sensitive: case_sensitive)
|
|
239
296
|
elsif arg.is_a?(String)
|
|
240
297
|
@enumbled_model.enumbles.find do |e|
|
|
241
298
|
if case_sensitive
|
|
@@ -256,6 +313,8 @@ module Enumbler
|
|
|
256
313
|
|
|
257
314
|
nil
|
|
258
315
|
end
|
|
316
|
+
rescue AmbiguousLabelError
|
|
317
|
+
raise
|
|
259
318
|
rescue Error
|
|
260
319
|
raise Error, err
|
|
261
320
|
end
|
|
@@ -263,6 +322,8 @@ module Enumbler
|
|
|
263
322
|
|
|
264
323
|
# See {.find_enumbles}. Same method, only raises error when none found.
|
|
265
324
|
# @raise [Error] when there is no [Enumbler::Enumble] to be found
|
|
325
|
+
# @raise [AmbiguousLabelError] when a String matches more than one label
|
|
326
|
+
# (see {.enumbler_allow_duplicate_labels})
|
|
266
327
|
# @param args [Integer, String, Symbol]
|
|
267
328
|
# @param case_sensitive [Boolean] should a String search be case sensitive
|
|
268
329
|
# (default: false)
|
|
@@ -280,6 +341,8 @@ module Enumbler
|
|
|
280
341
|
# Color.id_from_enumbler(Color.black) # => 1
|
|
281
342
|
#
|
|
282
343
|
# @raise [Error] when there is no enumble to be found
|
|
344
|
+
# @raise [AmbiguousLabelError] when a String matches more than one label
|
|
345
|
+
# (see {.enumbler_allow_duplicate_labels})
|
|
283
346
|
# @param arg [Integer, Symbol, Class]
|
|
284
347
|
# @param case_sensitive [Boolean] should a string search be performed with
|
|
285
348
|
# case sensitivity (default: false)
|
|
@@ -292,6 +355,8 @@ module Enumbler
|
|
|
292
355
|
|
|
293
356
|
# See {.ids_from_enumbler}. Raises error if none found.
|
|
294
357
|
# @raise [Error] when there is no enumble to be found
|
|
358
|
+
# @raise [AmbiguousLabelError] when a String matches more than one label
|
|
359
|
+
# (see {.enumbler_allow_duplicate_labels})
|
|
295
360
|
# @param arg [Integer, Symbol, Class]
|
|
296
361
|
# @param case_sensitive [Boolean] should a string search be performed with
|
|
297
362
|
# case sensitivity (default: false)
|
|
@@ -312,6 +377,8 @@ module Enumbler
|
|
|
312
377
|
# Color.ids_from_enumbler(Color.black, Color.white) # => [1, 2]
|
|
313
378
|
#
|
|
314
379
|
# @raise [Error] when there is no enumble to be found
|
|
380
|
+
# @raise [AmbiguousLabelError] when a String matches more than one label
|
|
381
|
+
# (see {.enumbler_allow_duplicate_labels})
|
|
315
382
|
# @param *args [Integer, Symbol, Class]
|
|
316
383
|
# @param case_sensitive [Boolean] should a string search be performed with
|
|
317
384
|
# case sensitivity (default: false)
|
|
@@ -325,6 +392,8 @@ module Enumbler
|
|
|
325
392
|
|
|
326
393
|
# See {.ids_from_enumbler}. Raises error when none found.
|
|
327
394
|
# @raise [Error] when there is no enumble to be found
|
|
395
|
+
# @raise [AmbiguousLabelError] when a String matches more than one label
|
|
396
|
+
# (see {.enumbler_allow_duplicate_labels})
|
|
328
397
|
# @param *args [Integer, Symbol, Class]
|
|
329
398
|
# @param case_sensitive [Boolean] should a string search be performed with
|
|
330
399
|
# case sensitivity (default: false)
|
|
@@ -380,6 +449,32 @@ module Enumbler
|
|
|
380
449
|
|
|
381
450
|
private
|
|
382
451
|
|
|
452
|
+
# The String branch of {.find_enumbles} for a model that allows
|
|
453
|
+
# duplicate labels. Matching runs in tiers: exact case first, then
|
|
454
|
+
# (unless `case_sensitive`) ignoring case. Within a tier an enum name
|
|
455
|
+
# wins, then a single label match resolves and more than one raises.
|
|
456
|
+
# So an exact-case label shared by two enumbles raises even when some
|
|
457
|
+
# enum name matches it ignoring case: better than guessing.
|
|
458
|
+
def find_enumble_by_string_allowing_duplicate_labels(arg, case_sensitive:)
|
|
459
|
+
tiers = [->(value) { arg == value }]
|
|
460
|
+
tiers << ->(value) { arg.casecmp?(value) } unless case_sensitive
|
|
461
|
+
|
|
462
|
+
tiers.each do |matches|
|
|
463
|
+
by_enum = @enumbled_model.enumbles.find { |e| matches.call(e.enum.to_s) }
|
|
464
|
+
return by_enum if by_enum
|
|
465
|
+
|
|
466
|
+
by_label = @enumbled_model.enumbles.select { |e| matches.call(e.label) }
|
|
467
|
+
next if by_label.empty?
|
|
468
|
+
return by_label.first if by_label.one?
|
|
469
|
+
|
|
470
|
+
raise AmbiguousLabelError,
|
|
471
|
+
"#{@enumbled_model} has more than one enumble labeled #{arg.inspect} " \
|
|
472
|
+
"(#{by_label.map { |e| ":#{e.enum}" }.join(', ')}); look it up by enum or id instead."
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
nil
|
|
476
|
+
end
|
|
477
|
+
|
|
383
478
|
def define_dynamic_methods_and_constants_for_enumbled_model(enumble)
|
|
384
479
|
method_name = "#{enumble.enum}?"
|
|
385
480
|
not_method_name = "not_#{enumble.enum}?"
|
|
@@ -462,12 +557,16 @@ module Enumbler
|
|
|
462
557
|
raise Enumbler::Error,
|
|
463
558
|
"The model #{self} does not support the attribute(s): #{unsupported_attrs.keys.map(&:to_s).to_sentence}"
|
|
464
559
|
rescue ActiveRecord::PendingMigrationError
|
|
560
|
+
return if Enumbler.suppress_database_warnings?
|
|
561
|
+
|
|
465
562
|
warn "[Enumbler Warning] => The model #{self} does not currently support the attribute(s): " \
|
|
466
563
|
"#{unsupported_attrs.keys.map(&:to_s).to_sentence}. " \
|
|
467
564
|
"You have a pending migration which hopefully would remedy this! " \
|
|
468
565
|
"If not, you need to add a migration for this attibrute or " \
|
|
469
566
|
"remove it from the Enumbler."
|
|
470
567
|
rescue ActiveRecord::StatementInvalid
|
|
568
|
+
return if Enumbler.suppress_database_warnings?
|
|
569
|
+
|
|
471
570
|
warn "[Enumbler Warning] => Unable to find a table for #{self}." \
|
|
472
571
|
"This is to be expected if there is a pending migration; " \
|
|
473
572
|
"however, if there is not then something is amiss."
|
data/lib/enumbler/enumble.rb
CHANGED
|
@@ -9,14 +9,17 @@ module Enumbler
|
|
|
9
9
|
@id = id
|
|
10
10
|
@enum = enum
|
|
11
11
|
@label_column_name = label_column_name
|
|
12
|
+
@allow_duplicate_labels = false
|
|
12
13
|
@label = (label_col_specified? ? attributes[label_column_name] : label) || enum.to_s.dasherize
|
|
13
14
|
@additional_attributes = attributes || {}
|
|
14
15
|
@additional_attributes.merge!({ label: label }) unless label.nil?
|
|
15
16
|
end
|
|
16
17
|
|
|
18
|
+
# Equal when the id or enum match, or the label matches and neither side
|
|
19
|
+
# allows duplicate labels.
|
|
17
20
|
def ==(other)
|
|
18
21
|
other.class == self.class &&
|
|
19
|
-
(other.id == id || other.enum == enum || other
|
|
22
|
+
(other.id == id || other.enum == enum || same_label_as?(other))
|
|
20
23
|
end
|
|
21
24
|
|
|
22
25
|
def attributes
|
|
@@ -34,9 +37,24 @@ module Enumbler
|
|
|
34
37
|
self
|
|
35
38
|
end
|
|
36
39
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
alias eql? ==
|
|
41
|
+
|
|
42
|
+
# Whether this Enumble may share its label with another in its model.
|
|
43
|
+
# @return [Boolean]
|
|
44
|
+
def allow_duplicate_labels?
|
|
45
|
+
@allow_duplicate_labels
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Lets this Enumble share its label: a shared label no longer makes two
|
|
49
|
+
# Enumbles equal, only id or enum do. Called by `enumble` on a model that
|
|
50
|
+
# uses {Enumbler::Enabler::ClassMethods#enumbler_allow_duplicate_labels};
|
|
51
|
+
# it is not a constructor keyword so it cannot collide with a column name
|
|
52
|
+
# passed through `**attributes`.
|
|
53
|
+
# @api private
|
|
54
|
+
# @return [self]
|
|
55
|
+
def allow_duplicate_labels!
|
|
56
|
+
@allow_duplicate_labels = true
|
|
57
|
+
self
|
|
40
58
|
end
|
|
41
59
|
|
|
42
60
|
# Standardizing the enum for a GraphQL schema with an uppercase string
|
|
@@ -52,6 +70,12 @@ module Enumbler
|
|
|
52
70
|
|
|
53
71
|
private
|
|
54
72
|
|
|
73
|
+
def same_label_as?(other)
|
|
74
|
+
return false if allow_duplicate_labels? || other.allow_duplicate_labels?
|
|
75
|
+
|
|
76
|
+
other.label == label
|
|
77
|
+
end
|
|
78
|
+
|
|
55
79
|
def label_col_specified?
|
|
56
80
|
label_column_name != :label
|
|
57
81
|
end
|
data/lib/enumbler/version.rb
CHANGED
data/lib/enumbler.rb
CHANGED
|
@@ -17,6 +17,32 @@ module Enumbler
|
|
|
17
17
|
# An error raised by the Enumbler.
|
|
18
18
|
class Error < StandardError; end
|
|
19
19
|
|
|
20
|
+
# Raised when a String lookup matches the labels of more than one enumble,
|
|
21
|
+
# which can only happen on a model that calls
|
|
22
|
+
# `enumbler_allow_duplicate_labels`. Raised whether or not the lookup was
|
|
23
|
+
# asked to raise on a missing value: a guess is worse than no answer.
|
|
24
|
+
class AmbiguousLabelError < Error; end
|
|
25
|
+
|
|
26
|
+
class << self
|
|
27
|
+
attr_writer :suppress_database_warnings
|
|
28
|
+
|
|
29
|
+
# Suppress the boot-time `[Enumbler Warning]` messages emitted when a
|
|
30
|
+
# model's table or attributes cannot be verified (e.g., while the database
|
|
31
|
+
# is dropped during a test database reset). Enable by setting
|
|
32
|
+
# `Enumbler.suppress_database_warnings = true` or by setting the
|
|
33
|
+
# environment variable `ENUMBLER_SUPPRESS_DATABASE_WARNINGS` to any
|
|
34
|
+
# non-empty value. The Ruby-level setting, when assigned, wins over the
|
|
35
|
+
# environment variable.
|
|
36
|
+
# @return [Boolean]
|
|
37
|
+
def suppress_database_warnings?
|
|
38
|
+
if @suppress_database_warnings.nil?
|
|
39
|
+
ENV["ENUMBLER_SUPPRESS_DATABASE_WARNINGS"].present?
|
|
40
|
+
else
|
|
41
|
+
@suppress_database_warnings
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
20
46
|
# Include these ClassMethods in your base ApplicationRecord model to bestow
|
|
21
47
|
# any of your models with the ability to be connected to an Enumbled relation
|
|
22
48
|
# in the same way you would use `belongs_to` now you use `enumbled_to`.
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: enumbler
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.12.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Damon Timm
|
|
@@ -49,56 +49,32 @@ dependencies:
|
|
|
49
49
|
- - "<"
|
|
50
50
|
- !ruby/object:Gem::Version
|
|
51
51
|
version: '9'
|
|
52
|
-
description:
|
|
52
|
+
description: 'Database-backed enums for ActiveRecord: real foreign-key lookup tables
|
|
53
|
+
that behave like enums, with constants, predicates, scopes, and seeding generated
|
|
54
|
+
from one definition.'
|
|
53
55
|
email:
|
|
54
56
|
- damon@linguabee.com
|
|
55
57
|
executables: []
|
|
56
58
|
extensions: []
|
|
57
59
|
extra_rdoc_files: []
|
|
58
60
|
files:
|
|
59
|
-
-
|
|
60
|
-
- ".gitignore"
|
|
61
|
-
- ".rspec"
|
|
62
|
-
- ".rubocop.yml"
|
|
63
|
-
- ".ruby-version"
|
|
64
|
-
- ".solargraph.yml"
|
|
65
|
-
- AGENTS.md
|
|
66
|
-
- Gemfile
|
|
67
|
-
- Gemfile.lock
|
|
68
|
-
- Gemfile.rails6.0
|
|
69
|
-
- Gemfile.rails6.0.lock
|
|
70
|
-
- Gemfile.rails6.1
|
|
71
|
-
- Gemfile.rails6.1.lock
|
|
72
|
-
- Gemfile.rails7.0
|
|
73
|
-
- Gemfile.rails7.0.lock
|
|
74
|
-
- Gemfile.rails7.1
|
|
75
|
-
- Gemfile.rails7.1.lock
|
|
76
|
-
- Gemfile.rails7.2
|
|
77
|
-
- Gemfile.rails7.2.lock
|
|
78
|
-
- Gemfile.rails8.0
|
|
79
|
-
- Gemfile.rails8.0.lock
|
|
61
|
+
- CHANGELOG.md
|
|
80
62
|
- LICENSE.txt
|
|
81
63
|
- README.md
|
|
82
|
-
- Rakefile
|
|
83
|
-
- bin/console
|
|
84
|
-
- bin/pre_commit_rubocop.rb
|
|
85
|
-
- bin/setup
|
|
86
|
-
- enumbler.gemspec
|
|
87
|
-
- lefthook.yml
|
|
88
64
|
- lib/enumbler.rb
|
|
89
65
|
- lib/enumbler/collection.rb
|
|
90
66
|
- lib/enumbler/core_ext/symbol/case_equality_operator.rb
|
|
91
67
|
- lib/enumbler/enabler.rb
|
|
92
68
|
- lib/enumbler/enumble.rb
|
|
93
69
|
- lib/enumbler/version.rb
|
|
94
|
-
homepage: https://github.com/
|
|
70
|
+
homepage: https://github.com/octoopi/enumbler
|
|
95
71
|
licenses:
|
|
96
72
|
- MIT
|
|
97
73
|
metadata:
|
|
98
74
|
allowed_push_host: https://rubygems.org
|
|
99
|
-
homepage_uri: https://github.com/
|
|
100
|
-
source_code_uri: https://github.com/
|
|
101
|
-
changelog_uri: https://github.com/
|
|
75
|
+
homepage_uri: https://github.com/octoopi/enumbler
|
|
76
|
+
source_code_uri: https://github.com/octoopi/enumbler
|
|
77
|
+
changelog_uri: https://github.com/octoopi/enumbler/blob/main/CHANGELOG.md
|
|
102
78
|
rubygems_mfa_required: 'true'
|
|
103
79
|
rdoc_options: []
|
|
104
80
|
require_paths:
|
|
@@ -107,14 +83,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
107
83
|
requirements:
|
|
108
84
|
- - ">="
|
|
109
85
|
- !ruby/object:Gem::Version
|
|
110
|
-
version: '3.
|
|
86
|
+
version: '3.2'
|
|
111
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
88
|
requirements:
|
|
113
89
|
- - ">="
|
|
114
90
|
- !ruby/object:Gem::Version
|
|
115
91
|
version: '0'
|
|
116
92
|
requirements: []
|
|
117
|
-
rubygems_version: 3.6.
|
|
93
|
+
rubygems_version: 3.6.9
|
|
118
94
|
specification_version: 4
|
|
119
95
|
summary: Enums are terrific, but lack integrity. Let's add some!
|
|
120
96
|
test_files: []
|
data/.github/workflows/ruby.yml
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
name: CI Matrix Testing
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches: [ main ]
|
|
6
|
-
tags: ["*"]
|
|
7
|
-
pull_request:
|
|
8
|
-
|
|
9
|
-
jobs:
|
|
10
|
-
rspec:
|
|
11
|
-
runs-on: ubuntu-latest
|
|
12
|
-
strategy:
|
|
13
|
-
fail-fast: false
|
|
14
|
-
matrix:
|
|
15
|
-
ruby: [3.1, 3.2, 3.3, 3.4]
|
|
16
|
-
rails_version: ["6.0", "6.1", "7.0", "7.1", "7.2", "8.0"]
|
|
17
|
-
exclude:
|
|
18
|
-
- ruby: 3.1
|
|
19
|
-
rails_version: "8.0"
|
|
20
|
-
- ruby: 3.4
|
|
21
|
-
rails_version: "6.0"
|
|
22
|
-
- ruby: 3.4
|
|
23
|
-
rails_version: "6.1"
|
|
24
|
-
- ruby: 3.4
|
|
25
|
-
rails_version: "7.0"
|
|
26
|
-
|
|
27
|
-
steps:
|
|
28
|
-
- uses: actions/checkout@v4
|
|
29
|
-
|
|
30
|
-
- name: Set up Ruby
|
|
31
|
-
uses: ruby/setup-ruby@v1
|
|
32
|
-
with:
|
|
33
|
-
ruby-version: ${{ matrix.ruby }}
|
|
34
|
-
|
|
35
|
-
- name: Install sqlite3
|
|
36
|
-
run: sudo apt-get update && sudo apt-get install -y libsqlite3-dev
|
|
37
|
-
|
|
38
|
-
- name: Install dependencies
|
|
39
|
-
env:
|
|
40
|
-
BUNDLE_GEMFILE: Gemfile.rails${{ matrix.rails_version }}
|
|
41
|
-
run: |
|
|
42
|
-
bundle install --jobs 4 --retry 3
|
|
43
|
-
|
|
44
|
-
- name: Run specs
|
|
45
|
-
env:
|
|
46
|
-
BUNDLE_GEMFILE: Gemfile.rails${{ matrix.rails_version }}
|
|
47
|
-
run: bundle exec rspec
|
|
48
|
-
|
|
49
|
-
build:
|
|
50
|
-
needs: rspec
|
|
51
|
-
|
|
52
|
-
name: Publish Gem
|
|
53
|
-
runs-on: ubuntu-latest
|
|
54
|
-
|
|
55
|
-
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
|
56
|
-
|
|
57
|
-
steps:
|
|
58
|
-
- uses: actions/checkout@v4
|
|
59
|
-
|
|
60
|
-
- name: Set up Ruby
|
|
61
|
-
uses: ruby/setup-ruby@v1
|
|
62
|
-
with:
|
|
63
|
-
ruby-version: 3.4
|
|
64
|
-
|
|
65
|
-
- name: Publish to RubyGems
|
|
66
|
-
run: |
|
|
67
|
-
gem build *.gemspec
|
|
68
|
-
gem push *.gem
|
|
69
|
-
env:
|
|
70
|
-
GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
|
|
71
|
-
|
data/.gitignore
DELETED
data/.rspec
DELETED