enumbler 0.6.5 → 0.6.10

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: 3c4dbada62dea66bb3e424ee945eb02dd5b84c63d04381efeafdda8a4e7a976c
4
- data.tar.gz: 9cebdafc82d22fc9d8da9cbeec18eb2a62f1169a30206868bde27cccf3003a8e
3
+ metadata.gz: 3f5c38e7d7fa5b50711b36695b14990509fcee834dfbf91025bf9c23b68f0164
4
+ data.tar.gz: 2a03beaa5e2a8ae713af29b9a39358122b586cfa9b7fa75583e15655806aeb80
5
5
  SHA512:
6
- metadata.gz: df90746c7c1145fcb67136ea92ca6f0047ba8b5903329d3adcce39c63f76de88847a6ed12065c5a1ca9e43f9ea8541533854e6608e82771cf230634df000fe77
7
- data.tar.gz: 12cb3873ca0a196155e3a953bdcc797a1621b03baee4962149368a2eb8b5f9efb743d73c88322d3976a016d4253ce89bb5f17dac4a58aa7435ae55c6ee44ffa2
6
+ metadata.gz: 428b407fdec8f44eec45e14503acbdfa98356a6b8b4d4b08ece07a355b4e6ae2de147c3807e0f3d7aeccb3fd2c81681f718bd8ada45e7d4c57103b7e53a1c30b
7
+ data.tar.gz: 2a3d3b91abe35d68dfb97b76bbddec356debe6f02ba9687f89c3b6f6c34d5f217365a61f846df5cafaa6630d045e9517d5d47b2e288d865475f4df010abe170b
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enumbler (0.6.5)
4
+ enumbler (0.6.10)
5
5
  activerecord (>= 5.2.3, < 6.1)
6
6
  activesupport (>= 5.2.3, < 6.1)
7
7
 
data/README.md CHANGED
@@ -61,6 +61,9 @@ Color.black.black? # => true
61
61
  Color.black.is_black # => true
62
62
  Color.white.not_black? # => true
63
63
 
64
+ Color.all.any_black? # => true
65
+ Color.where.not(id: Color::BLACK).any_black? # => false
66
+
64
67
  # Get attributes without hitting the database
65
68
  Color.black(:id) # => 1
66
69
  Color.black(:enum) # => :black
@@ -73,6 +76,9 @@ Color.find_enumbles(:black, 'does-not-exist') # => [Enumbler::Enumble<:black>, n
73
76
 
74
77
  Color.find_enumble(:black) # => Enumbler::Enumble<:black>
75
78
 
79
+ # Is pretty flexible with findidng things from strings
80
+ Color.find_enumble('Dark_Brown') # => Enumbler::Enumble<:dark-brown>
81
+
76
82
  # raises errors if none found
77
83
  Color.find_enumbles!!(:black, 'does-no-exist') # => raises Enumbler::Error
78
84
  Color.find_enumble!(:does_not_exist) # => raises Enumbler::Error
@@ -123,6 +129,18 @@ class Feeling < ApplicationRecord
123
129
  end
124
130
  ```
125
131
 
132
+ ## Core ext
133
+
134
+ Adds case equality power to the `Symbol` class allowing you to use case methods directly against an enabled instance:
135
+
136
+ ```ruby
137
+ case Color.black
138
+ when :black
139
+ 'not surprised'
140
+ when :blue, :purple
141
+ 'very surprised'
142
+ end
143
+
126
144
  ## Development
127
145
 
128
146
  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.
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'enumbler/core_ext/symbol/case_equality_operator'
4
+
3
5
  require 'enumbler/collection'
4
6
  require 'enumbler/enumble'
5
7
  require 'enumbler/enabler'
@@ -55,7 +57,7 @@ module Enumbler
55
57
  # example: `where_by` will make it `House.where_by_color(:black)`
56
58
  # @param **options [Hash] additional options passed to `belongs_to`
57
59
  def enumbled_to(name, scope = nil, prefix: false, scope_prefix: nil, **options)
58
- class_name = name.to_s.classify
60
+ class_name = options.fetch(:class_name, name.to_s.classify)
59
61
  enumbled_model = class_name.constantize
60
62
 
61
63
  unless enumbled_model.respond_to?(:enumbles)
@@ -66,7 +68,7 @@ module Enumbler
66
68
  belongs_to(name, scope, **options)
67
69
 
68
70
  define_helper_attributes(name)
69
- define_dynamic_methods_for_enumbled_to_models(enumbled_model, prefix: prefix, scope_prefix: scope_prefix)
71
+ define_dynamic_methods_for_enumbled_to_models(name, enumbled_model, prefix: prefix, scope_prefix: scope_prefix)
70
72
  rescue NameError
71
73
  raise Error, "The model #{class_name} cannot be found. Uninitialized constant."
72
74
  end
@@ -78,11 +80,10 @@ module Enumbler
78
80
  # @todo - we should check for naming conflicts!
79
81
  # dangerous_attribute_method?(method_name)
80
82
  # method_defined_within?(method_name, self, Module)
81
- def define_dynamic_methods_for_enumbled_to_models(enumbled_model, prefix: false, scope_prefix: nil)
82
- model_name = enumbled_model.to_s.underscore
83
- column_name = "#{model_name}_id"
83
+ def define_dynamic_methods_for_enumbled_to_models(name, enumbled_model, prefix: false, scope_prefix: nil)
84
+ column_name = "#{name}_id"
84
85
 
85
- cmethod = scope_prefix.blank? ? model_name : "#{scope_prefix}_#{model_name}"
86
+ cmethod = scope_prefix.blank? ? name : "#{scope_prefix}_#{name}"
86
87
  define_singleton_method(cmethod) do |*args|
87
88
  where(column_name => enumbled_model.ids_from_enumbler(args))
88
89
  end
@@ -92,8 +93,8 @@ module Enumbler
92
93
  end
93
94
 
94
95
  enumbled_model.enumbles.each do |enumble|
95
- method_name = prefix ? "#{model_name}_#{enumble.enum}?" : "#{enumble.enum}?"
96
- not_method_name = prefix ? "#{model_name}_not_#{enumble.enum}?" : "not_#{enumble.enum}?"
96
+ method_name = prefix ? "#{name}_#{enumble.enum}?" : "#{enumble.enum}?"
97
+ not_method_name = prefix ? "#{name}_not_#{enumble.enum}?" : "not_#{enumble.enum}?"
97
98
  define_method(method_name) { self[column_name] == enumble.id }
98
99
  define_method(not_method_name) { self[column_name] != enumble.id }
99
100
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Use case equality operator with an enabled class.
4
+ #
5
+ # case House.black
6
+ # when :black
7
+ # 'this is true'
8
+ # when :blue, :purple
9
+ # 'this is not'
10
+ # end
11
+ class Symbol
12
+ def ===(other)
13
+ super ||
14
+ other.class.included_modules.include?(Enumbler::Enabler) &&
15
+ other.enumble.enum == self
16
+
17
+ # Calling #enumble on a new instance that has not been defined raises an
18
+ # error, so catching that edge case here
19
+ rescue Enumbler::Error
20
+ false
21
+ end
22
+ end
@@ -26,6 +26,15 @@ module Enumbler
26
26
  super
27
27
  end
28
28
 
29
+ # The enumble symbol if it exists.
30
+ # @return [Symbol]
31
+ def to_sym
32
+ enumble = self.class.find_enumble(id)
33
+ return enumble.enum if enumble.present?
34
+
35
+ super
36
+ end
37
+
29
38
  # These ClassMethods can be included in any model that you wish to
30
39
  # _Enumble_!
31
40
  #
@@ -168,7 +177,11 @@ module Enumbler
168
177
  @enumbled_model.enumbles.find { |e| e.enum == arg }
169
178
  elsif arg.is_a?(String)
170
179
  @enumbled_model.enumbles.find do |e|
171
- case_sensitive ? e.label == arg : arg.casecmp?(e.label)
180
+ if case_sensitive
181
+ [e.label, e.enum.to_s].include?(arg)
182
+ else
183
+ arg.casecmp?(e.label) || arg.casecmp?(e.enum.to_s)
184
+ end
172
185
  end
173
186
  elsif arg.instance_of?(@enumbled_model)
174
187
  arg.enumble
@@ -322,6 +335,12 @@ module Enumbler
322
335
  rescue NoMethodError
323
336
  raise Enumbler::Error, "The attribute #{attr} is not supported on this Enumble."
324
337
  end
338
+
339
+ define_singleton_method("any_#{enumble.enum}?") do
340
+ where(id: enumble.id).exists?
341
+ rescue NoMethodError
342
+ raise Enumbler::Error, "The attribute #{attr} is not supported on this Enumble."
343
+ end
325
344
  end
326
345
 
327
346
  # I accidentally forgot to provide an id one time and it was confusing as
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enumbler
4
- VERSION = '0.6.5'
4
+ VERSION = '0.6.10'
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.6.5
4
+ version: 0.6.10
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-08-04 00:00:00.000000000 Z
11
+ date: 2020-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -157,6 +157,7 @@ files:
157
157
  - enumbler.gemspec
158
158
  - lib/enumbler.rb
159
159
  - lib/enumbler/collection.rb
160
+ - lib/enumbler/core_ext/symbol/case_equality_operator.rb
160
161
  - lib/enumbler/enabler.rb
161
162
  - lib/enumbler/enumble.rb
162
163
  - lib/enumbler/version.rb