enumbler 0.8.0 → 0.8.1

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: a3b8d2a2e38789cb62f2be5c895aa0fb2776b26b8efbbfe3386e88feaf041393
4
- data.tar.gz: d981a2476760ab1d0afb09bd2eb790c125cf54ba54cad3493a5865f52163648b
3
+ metadata.gz: 012b0ded68a635ef8ac4cb765eec4a648f16a4365abaa1d6def1897d389bcba8
4
+ data.tar.gz: a74d123fa2635a2118e1300cbf75a56d3255756e9bf9384049bb27f74187eb7a
5
5
  SHA512:
6
- metadata.gz: 65c1b774b7fd7fe86e3038c5bfea649608708e57ef626529c933e82e31249a43716b50e54d2a822640d11d6b98321832f032dfedf2efce9a750aebc1e25fd26c
7
- data.tar.gz: c223ff2703dd5693ee9925dd4ffc025c890138962001acca6619f8c1d30b7e6056a5275fa84f5c159f163c052b8fbc546c1f9132917b93a96956c6aba9d1e72e
6
+ metadata.gz: e623093fbb3dbc1645c9d3f713c32100fa561b8ade1a62c03d4da9ae7b6c6a964b45ddcacd4bc158f8c41cc8bdf1150a02c340ce84c9b6e94ec831f798a00a76
7
+ data.tar.gz: '09ef23ba8854c6ff1f01fbffd7e9227e183148acf6a03ce14bd02f0244d9c38f2ef2b9d1e7494320b5b6c8be08849689d1875eff1d68d81545e358299e8ebeb8'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enumbler (0.8.0)
4
+ enumbler (0.8.1)
5
5
  activerecord (>= 5.2.3, < 6.1)
6
6
  activesupport (>= 5.2.3, < 6.1)
7
7
 
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
- * We need to add in support for additional attributes/columns in the enumbled table. For example, following the `Color` concept, we may want to have a column which is `hex` and stores the colors `hex` value (e.g., `FFFFFF`). This should be supported.
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).
@@ -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("any_#{enumble.enum}?") do
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enumbler
4
- VERSION = '0.8.0'
4
+ VERSION = '0.8.1'
5
5
  end
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.0
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-10-26 00:00:00.000000000 Z
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.2
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!