enumbler 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +1 -3
- data/lib/enumbler/enabler.rb +40 -1
- data/lib/enumbler/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 012b0ded68a635ef8ac4cb765eec4a648f16a4365abaa1d6def1897d389bcba8
|
4
|
+
data.tar.gz: a74d123fa2635a2118e1300cbf75a56d3255756e9bf9384049bb27f74187eb7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e623093fbb3dbc1645c9d3f713c32100fa561b8ade1a62c03d4da9ae7b6c6a964b45ddcacd4bc158f8c41cc8bdf1150a02c340ce84c9b6e94ec831f798a00a76
|
7
|
+
data.tar.gz: '09ef23ba8854c6ff1f01fbffd7e9227e183148acf6a03ce14bd02f0244d9c38f2ef2b9d1e7494320b5b6c8be08849689d1875eff1d68d81545e358299e8ebeb8'
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -170,14 +170,12 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
170
170
|
|
171
171
|
## Roadmap
|
172
172
|
|
173
|
-
*
|
174
|
-
* Ideally, we could make this work more like a traditional `enum`; for example, overriding the `.where` method by allowing something like: `House.where(color: :blue)` instead of `House.where_color(:blue)`. But right now am in a rush and not sure how to go about doing that properly.
|
173
|
+
* Ideally, we could make this work more like a traditional `enum`; for example, overriding the `.where` method by allowing something like: `House.where(color: :blue)` instead of `House.color(:blue)`. But right now am in a rush and not sure how to go about doing that properly.
|
175
174
|
|
176
175
|
## Contributing
|
177
176
|
|
178
177
|
Bug reports and pull requests are welcome on GitHub at https://github.com/linguabee/enumbler.
|
179
178
|
|
180
|
-
|
181
179
|
## License
|
182
180
|
|
183
181
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/enumbler/enabler.rb
CHANGED
@@ -375,11 +375,21 @@ module Enumbler
|
|
375
375
|
method_name = "#{enumble.enum}?"
|
376
376
|
not_method_name = "not_#{enumble.enum}?"
|
377
377
|
alias_method_name = "is_#{enumble.enum}"
|
378
|
+
any_method_name = "any_#{enumble.enum}?"
|
379
|
+
|
380
|
+
[method_name, not_method_name, alias_method_name].each do |mname|
|
381
|
+
detect_enumbler_conflict(enumble.enum, mname)
|
382
|
+
end
|
383
|
+
|
384
|
+
[enumble.enum, any_method_name].each do |mname|
|
385
|
+
detect_enumbler_conflict(enumble.enum, mname, klass_method: true)
|
386
|
+
end
|
378
387
|
|
379
388
|
const_set(enumble.enum.to_s.upcase, enumble.id)
|
380
389
|
define_method(method_name) { id == enumble.id }
|
381
390
|
define_method(not_method_name) { id != enumble.id }
|
382
391
|
alias_method alias_method_name, method_name
|
392
|
+
|
383
393
|
define_singleton_method(enumble.enum) do |attr = nil|
|
384
394
|
return find(enumble.id) if attr.nil?
|
385
395
|
|
@@ -388,13 +398,42 @@ module Enumbler
|
|
388
398
|
raise Enumbler::Error, "The attribute #{attr} is not supported on this Enumble."
|
389
399
|
end
|
390
400
|
|
391
|
-
define_singleton_method(
|
401
|
+
define_singleton_method(any_method_name) do
|
392
402
|
where(id: enumble.id).exists?
|
393
403
|
rescue NoMethodError
|
394
404
|
raise Enumbler::Error, "The attribute #{attr} is not supported on this Enumble."
|
395
405
|
end
|
396
406
|
end
|
397
407
|
|
408
|
+
# This idea sourced lovingly from ActiveRecord::Enum
|
409
|
+
ENUMBLER_CONFLICT_MESSAGE = <<~TEXT.squish
|
410
|
+
You tried to define the enumble :%<enum>s on the model %<klass>s, but
|
411
|
+
this will generate a %<type>s method `%<method>s`, which is already defined
|
412
|
+
by %<source>s.
|
413
|
+
TEXT
|
414
|
+
|
415
|
+
def detect_enumbler_conflict(enumble_name, method_name, klass_method: false)
|
416
|
+
if klass_method && dangerous_class_method?(method_name)
|
417
|
+
raise_conflict_error(enumble_name, method_name, type: 'class')
|
418
|
+
elsif klass_method && method_defined_within?(method_name, ActiveRecord::Relation)
|
419
|
+
raise_conflict_error(enumble_name, method_name, type: 'class', source: ActiveRecord::Relation.name)
|
420
|
+
elsif !klass_method && dangerous_attribute_method?(method_name)
|
421
|
+
raise_conflict_error(enumble_name, method_name)
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
def raise_conflict_error(enumble_name, method_name, type: 'instance', source: 'ActiveRecord')
|
426
|
+
raise Error,
|
427
|
+
format(
|
428
|
+
ENUMBLER_CONFLICT_MESSAGE,
|
429
|
+
enum: enumble_name,
|
430
|
+
klass: name,
|
431
|
+
type: type,
|
432
|
+
method: method_name,
|
433
|
+
source: source
|
434
|
+
)
|
435
|
+
end
|
436
|
+
|
398
437
|
# I accidentally forgot to provide an id one time and it was confusing as
|
399
438
|
# the last argument became the hash of options. This should help.
|
400
439
|
def validate_id_is_numeric(enum, id)
|
data/lib/enumbler/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumbler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Damon Timm
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -198,7 +198,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
198
198
|
- !ruby/object:Gem::Version
|
199
199
|
version: '0'
|
200
200
|
requirements: []
|
201
|
-
rubygems_version: 3.1.
|
201
|
+
rubygems_version: 3.1.4
|
202
202
|
signing_key:
|
203
203
|
specification_version: 4
|
204
204
|
summary: Enums are terrific, but lack integrity. Let's add some!
|