enumbler 0.6.6 → 0.6.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c7090cf447bc470848993d1b219762453e4188c8aea3c11bf3bb4daf3d5ed771
4
- data.tar.gz: 2fc9f318859ab4c8f6dc493f00d565b3c68ca41d0d804beeb8ac3f251262423e
3
+ metadata.gz: 55e4caeaa0c8dc41e011352ed5931651f3f74d203f54a2a747034c384d065aea
4
+ data.tar.gz: 6e772d0c3b1c8c1403298ac244a3aad601d8150497a2a76ca924f2bfecc4a661
5
5
  SHA512:
6
- metadata.gz: 59b4721f1cdbdb5174692d1c13b784cd9eae230a67a99d34f87e4696602d75bd91f005a140ea5ff8f0ec4731102e10f9eb193f2deaaf8537465720c569c10faf
7
- data.tar.gz: 704eca26b618018b976e165146182bda016440a9d4ff652ccb7351e70dfacc587c8d9fb1bf96647185adbe439f7a6b59d2f4798a3cbe4d6c4d45d58224daae51
6
+ metadata.gz: b2312aec90691d56a46e99a89706eca87f82a7b4fd4e9a319e30486daad09554a41d1d40b8daf855fb099d23a4e371a276b94b53d225d782bdd6b1a912845bff
7
+ data.tar.gz: 85508fb5286ab683ca10b9ffde36133d52780d5d66cbff40f59ce6b86ef191a7c63f18b6a398a8981f21614e77804c744af7e7efc7853437329d2759ed712011
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enumbler (0.6.6)
4
+ enumbler (0.6.11)
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
@@ -126,6 +129,18 @@ class Feeling < ApplicationRecord
126
129
  end
127
130
  ```
128
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
+
129
144
  ## Development
130
145
 
131
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
@@ -17,13 +17,29 @@ module Enumbler
17
17
  @enumble
18
18
  end
19
19
 
20
+ # The enumble graphql_enum if it exists.
21
+ # @return [Symbol]
22
+ def to_graphql_enum
23
+ to_enumble_attribute(:graphql_enum) || super
24
+ end
25
+
20
26
  # The enumble label if it exists.
21
27
  # @return [String]
22
28
  def to_s
23
- enumble = self.class.find_enumble(id)
24
- return enumble.label if enumble.present?
29
+ to_enumble_attribute(:label) || super
30
+ end
31
+
32
+ # The enumble symbol if it exists.
33
+ # @return [Symbol]
34
+ def to_sym
35
+ to_enumble_attribute(:enum) || super
36
+ end
25
37
 
26
- super
38
+ private
39
+
40
+ def to_enumble_attribute(attribute)
41
+ enumble = self.class.find_enumble(id)
42
+ return enumble.send(attribute) if enumble.present?
27
43
  end
28
44
 
29
45
  # These ClassMethods can be included in any model that you wish to
@@ -326,6 +342,12 @@ module Enumbler
326
342
  rescue NoMethodError
327
343
  raise Enumbler::Error, "The attribute #{attr} is not supported on this Enumble."
328
344
  end
345
+
346
+ define_singleton_method("any_#{enumble.enum}?") do
347
+ where(id: enumble.id).exists?
348
+ rescue NoMethodError
349
+ raise Enumbler::Error, "The attribute #{attr} is not supported on this Enumble."
350
+ end
329
351
  end
330
352
 
331
353
  # 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.6'
4
+ VERSION = '0.6.11'
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.6
4
+ version: 0.6.11
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-14 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