enumbler 0.6.3 → 0.6.8

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: 29989e16025afc87b0db8fd70347b21978c683ab44c6ffa2d1f44a22c20fb166
4
- data.tar.gz: 807a16dd6e6ff6e8c95b3f5337acf2f1ea456d07378dc702b8f2672c0b862276
3
+ metadata.gz: 4ddaace87d45b05778db532d5d0d4d4b28a475a02ec7079f1dff8820edc05d24
4
+ data.tar.gz: b24dd09f614e8e32bed14b17bd9a36a8db1907dd4cb6e7614f73897394de3ef6
5
5
  SHA512:
6
- metadata.gz: 2c8dbf495025d467ee30172289097169d84d23776320a1624d5e3920f2f66e738f606a15e61e2bbce696051c6d87ec99c811c45b3d9cdcb631f7e3bd1b961409
7
- data.tar.gz: 3a6d12fe63ff37e1274b5ec3ec31023aa08f520f455b44695746a5555a20bc063283fbd6afaf2f9831d740588bcf090426647f052c1bc87e98d829a071a9fc7d
6
+ metadata.gz: 98349a3658d0cef8674e4a63a577a4b9e71279a4051d292f9b817fd5d8e1a2c0c837fdbb3b3cc01cac6de6219558a567eaafc9b0567d35e09bfe7b5d1689ce2c
7
+ data.tar.gz: 2a52456e1524c22fd224e18421fea42bff5f45fd35f016dddce701df59c7ae5edb3738555faa26ce2fee4afaae7d94043997a6e25d15414cba5bd7872f97a237
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enumbler (0.6.3)
4
+ enumbler (0.6.8)
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'
@@ -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
@@ -10,13 +10,22 @@ module Enumbler
10
10
  # The Enumble definition that this record defined.
11
11
  # @return [Enumbler::Enumble]
12
12
  def enumble
13
- @enumble = self.class.enumbles.find { |enumble| enumble.id == id }
13
+ @enumble = self.class.find_enumble(id)
14
14
 
15
15
  raise Error, 'An enumble is not defined for this record!' if @enumble.nil?
16
16
 
17
17
  @enumble
18
18
  end
19
19
 
20
+ # The enumble label if it exists.
21
+ # @return [String]
22
+ def to_s
23
+ enumble = self.class.find_enumble(id)
24
+ return enumble.label if enumble.present?
25
+
26
+ super
27
+ end
28
+
20
29
  # These ClassMethods can be included in any model that you wish to
21
30
  # _Enumble_!
22
31
  #
@@ -58,12 +67,14 @@ module Enumbler
58
67
  # @param **attributes [Hash] optional: additional attributes and values that
59
68
  # will be saved to the database for this enumble record
60
69
  def enumble(enum, id, label: nil, **attributes)
70
+ raise_error_if_model_does_not_support_attributes(attributes)
71
+
72
+ id = validate_id_is_numeric(enum, id)
73
+
61
74
  @enumbles ||= Enumbler::Collection.new
62
75
  @enumbled_model = self
63
76
  @enumbler_label_column_name ||= :label
64
77
 
65
- raise_error_if_model_does_not_support_attributes(attributes)
66
-
67
78
  enumble = Enumble.new(enum, id, label: label, label_column_name: @enumbler_label_column_name, **attributes)
68
79
 
69
80
  if @enumbles.include?(enumble)
@@ -157,7 +168,11 @@ module Enumbler
157
168
  @enumbled_model.enumbles.find { |e| e.enum == arg }
158
169
  elsif arg.is_a?(String)
159
170
  @enumbled_model.enumbles.find do |e|
160
- case_sensitive ? e.label == arg : arg.casecmp?(e.label)
171
+ if case_sensitive
172
+ [e.label, e.enum.to_s].include?(arg)
173
+ else
174
+ arg.casecmp?(e.label) || arg.casecmp?(e.enum.to_s)
175
+ end
161
176
  end
162
177
  elsif arg.instance_of?(@enumbled_model)
163
178
  arg.enumble
@@ -311,6 +326,21 @@ module Enumbler
311
326
  rescue NoMethodError
312
327
  raise Enumbler::Error, "The attribute #{attr} is not supported on this Enumble."
313
328
  end
329
+
330
+ define_singleton_method("any_#{enumble.enum}?") do
331
+ where(id: enumble.id).exists?
332
+ rescue NoMethodError
333
+ raise Enumbler::Error, "The attribute #{attr} is not supported on this Enumble."
334
+ end
335
+ end
336
+
337
+ # I accidentally forgot to provide an id one time and it was confusing as
338
+ # the last argument became the hash of options. This should help.
339
+ def validate_id_is_numeric(enum, id)
340
+ Integer(id)
341
+ rescue ArgumentError, TypeError
342
+ raise Enumbler::Error,
343
+ "You must provide a numeric primary key, like: `enumble :#{enum}, 1 `"
314
344
  end
315
345
 
316
346
  def raise_error_if_model_does_not_support_attributes(attributes)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enumbler
4
- VERSION = '0.6.3'
4
+ VERSION = '0.6.8'
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.3
4
+ version: 0.6.8
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-07-06 00:00:00.000000000 Z
11
+ date: 2020-09-02 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