enumbler 0.6.4 → 0.6.9

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: 96cdeb24ac43db515f90d6cf857e497281066c7dad0f5c2cf93d3aada54265dc
4
- data.tar.gz: 13247cd26aa94ad10fe7f739fc8c3690d6e3702c59d77caceb80242a9df722d3
3
+ metadata.gz: 8023f1c813e4ecfd32ca382d1fb6ea0fe4d195d046a232fc3650d185c17e1fc4
4
+ data.tar.gz: 14b35d77a8491b27a2d93059851a8ce01e7e9852c04e5ee22460f6637b3d43e0
5
5
  SHA512:
6
- metadata.gz: 57cf510621d28df43bca6d5e7715e6bd9c1452a7a7457a1fd6ef3ae0f47425927deec6bc16e34dd2ccb94781cdb37f358d62825c8c64aed0294ffcd247fd6a93
7
- data.tar.gz: 007d1e7c93fcf129ec0b13d037edd045e4e18b9d453a6dadc2381425a73f9b64424a83e4d976b0bd316a593ccf3c5e9d3f986c1cbb2bed35f7a9cb1f4dfc94b5
6
+ metadata.gz: fd0044a895d518a5c3615bd3bb021b58262c7c376074bcfa409ad6bf3c78bea42a7afebdeec16f38a27bcc0a62e0b51107c89caec0de45f0c26e874c411b3123
7
+ data.tar.gz: 193c7a98d2c496ce9339f4ff1c6b5bd27b6a3b2a2f9ee7a2182ed61832ee243895c02b0bdd0e0c31d207d4b1f6f0a91601e5f0c9c1c85f5358020aba08d40860
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enumbler (0.6.4)
4
+ enumbler (0.6.9)
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,31 @@ 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
+
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
+
20
38
  # These ClassMethods can be included in any model that you wish to
21
39
  # _Enumble_!
22
40
  #
@@ -159,7 +177,11 @@ module Enumbler
159
177
  @enumbled_model.enumbles.find { |e| e.enum == arg }
160
178
  elsif arg.is_a?(String)
161
179
  @enumbled_model.enumbles.find do |e|
162
- 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
163
185
  end
164
186
  elsif arg.instance_of?(@enumbled_model)
165
187
  arg.enumble
@@ -313,6 +335,12 @@ module Enumbler
313
335
  rescue NoMethodError
314
336
  raise Enumbler::Error, "The attribute #{attr} is not supported on this Enumble."
315
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
316
344
  end
317
345
 
318
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.4'
4
+ VERSION = '0.6.9'
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.4
4
+ version: 0.6.9
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-02 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