enumbler 0.4.2 → 0.5.0

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: d6cb3204d93f558b4286bd533ca6ac0cd4e70170f3678d28848777526e44732d
4
- data.tar.gz: e21fbf9da2af04a6133df3559307892f259324fbad8fe0077facbf4024b63185
3
+ metadata.gz: 2eb7435b5b57b7fa3b04f7201bc41b3caaea6f148af6998fbdad02bdd44794bd
4
+ data.tar.gz: cf198237307efcbc457e55c3b046e612ba44a9fef52b86bb47f02a0989350df0
5
5
  SHA512:
6
- metadata.gz: e6dcafa85a85b9e304810e8554b81435c0414ed984bde78063e8aeceb397d3bfcb77534d4da2f04153dccd99cc7f2403e61156722f9743fe485fc242d8e26ba6
7
- data.tar.gz: 07fc687fa545b3bf402622d57fd3f0c95d961dede6b6358131cf39524c7fcd7ed6036a24baade2448f01599ecde2e91354ed43803e7657d40b1d0b9dbded6265
6
+ metadata.gz: e39538cefdfe366d8c944732796c0354c49f7fbc9930d7f3a4fb7912b6088188a1b1dbbd9f9395a4ea67f2c857a1ea7490f3631285daec68c388c19779b95602
7
+ data.tar.gz: f490870fc3d1bfe7f7a10bad501619668929f351f2882455e5e7da0581511957bd95fa08c77c09715e4b54f659212f742736256e9bca4706e906d1e0b3629551
@@ -1,27 +1,27 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enumbler (0.4.2)
4
+ enumbler (0.5.0)
5
5
  activerecord (~> 6.0.2)
6
6
  activesupport (~> 6.0.2)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (6.0.2.2)
12
- activesupport (= 6.0.2.2)
13
- activerecord (6.0.2.2)
14
- activemodel (= 6.0.2.2)
15
- activesupport (= 6.0.2.2)
16
- activesupport (6.0.2.2)
11
+ activemodel (6.0.3.1)
12
+ activesupport (= 6.0.3.1)
13
+ activerecord (6.0.3.1)
14
+ activemodel (= 6.0.3.1)
15
+ activesupport (= 6.0.3.1)
16
+ activesupport (6.0.3.1)
17
17
  concurrent-ruby (~> 1.0, >= 1.0.2)
18
18
  i18n (>= 0.7, < 2)
19
19
  minitest (~> 5.1)
20
20
  tzinfo (~> 1.1)
21
- zeitwerk (~> 2.2)
21
+ zeitwerk (~> 2.2, >= 2.2.2)
22
22
  ast (2.4.0)
23
23
  concurrent-ruby (1.1.6)
24
- database_cleaner (1.8.4)
24
+ database_cleaner (1.8.5)
25
25
  database_cleaner-active_record (1.8.0)
26
26
  activerecord
27
27
  database_cleaner (~> 1.8.0)
@@ -32,9 +32,9 @@ GEM
32
32
  i18n (1.8.2)
33
33
  concurrent-ruby (~> 1.0)
34
34
  jaro_winkler (1.5.4)
35
- minitest (5.14.0)
35
+ minitest (5.14.1)
36
36
  parallel (1.19.1)
37
- parser (2.7.1.0)
37
+ parser (2.7.1.3)
38
38
  ast (~> 2.4.0)
39
39
  rainbow (3.0.0)
40
40
  rake (12.3.3)
@@ -43,15 +43,15 @@ GEM
43
43
  rspec-core (~> 3.9.0)
44
44
  rspec-expectations (~> 3.9.0)
45
45
  rspec-mocks (~> 3.9.0)
46
- rspec-core (3.9.1)
47
- rspec-support (~> 3.9.1)
48
- rspec-expectations (3.9.1)
46
+ rspec-core (3.9.2)
47
+ rspec-support (~> 3.9.3)
48
+ rspec-expectations (3.9.2)
49
49
  diff-lcs (>= 1.2.0, < 2.0)
50
50
  rspec-support (~> 3.9.0)
51
51
  rspec-mocks (3.9.1)
52
52
  diff-lcs (>= 1.2.0, < 2.0)
53
53
  rspec-support (~> 3.9.0)
54
- rspec-support (3.9.2)
54
+ rspec-support (3.9.3)
55
55
  rubocop (0.81.0)
56
56
  jaro_winkler (~> 1.5.1)
57
57
  parallel (~> 1.10)
data/README.md CHANGED
@@ -54,17 +54,23 @@ class House < ApplicationRecord
54
54
  end
55
55
 
56
56
  # This gives you some power:
57
- Color::BLACK # => 1
58
- Color.black # => equivalent to Color.find(1)
59
- Color.black.black? # => true
60
- Color.black.is_black # => true
61
- Color.white.not_black? # => true
57
+ Color::BLACK # => 1
58
+ Color.black # => equivalent to Color.find(1)
59
+ Color.black.black? # => true
60
+ Color.black.is_black # => true
61
+ Color.white.not_black? # => true
62
+
63
+ Color.black(:id) # => 1
64
+ Color.black(:enum) # => :black
65
+ Color.black(:label) # => 'black'
66
+ Color.black(:graphql_enum) # => 'BLACK'
62
67
 
63
68
  house = House.create!(color: Color.black)
64
69
  house.black?
65
70
  house.not_black?
66
71
 
67
- House.color(:black) # => [house]
72
+ house2 = House.create!(color: Color.white)
73
+ House.color(:black, :white) # => [house, house2]
68
74
  ```
69
75
 
70
76
  ### Use a column other than `label`
@@ -98,7 +104,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
98
104
 
99
105
  ## Roadmap
100
106
 
101
- 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.
107
+ * 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.
108
+ * 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.
102
109
 
103
110
  ## Contributing
104
111
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'enumbler/collection'
3
4
  require 'enumbler/enumble'
4
5
  require 'enumbler/enabler'
5
6
  require 'enumbler/version'
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enumbler
4
+ # Not sure if this will be needed but was leaning toward a custom wrapper for
5
+ # our array holding the `enumbles` for our model. As it is, allows you to
6
+ # query them based on the enum:
7
+ #
8
+ # ```
9
+ # Color.enumbles.black # => [Enumbler::Enumble]
10
+ # ```
11
+ class Collection < Array
12
+ def method_missing(method_name, *args, &block)
13
+ enumble = find { |e| e.enum == method_name }
14
+ return enumble if enumble.present?
15
+
16
+ super
17
+ end
18
+
19
+ def respond_to_missing?(method_name, include_private = false)
20
+ enumble = find { |e| e.enum == method_name }
21
+ enumble.present? || super
22
+ end
23
+ end
24
+ end
@@ -58,7 +58,7 @@ module Enumbler
58
58
  # @param **options [Hash] optional: additional attributes and values that
59
59
  # will be saved to the database for this enumble record
60
60
  def enumble(enum, id, label: nil, **options)
61
- @enumbles ||= []
61
+ @enumbles ||= Enumbler::Collection.new
62
62
  @enumbled_model = self
63
63
  @enumbler_label_column_name ||= :label
64
64
 
@@ -68,7 +68,7 @@ module Enumbler
68
68
  raise Error, "You cannot add the same Enumble twice! Attempted to add: #{enum}, #{id}."
69
69
  end
70
70
 
71
- define_dynamic_methods_and_constants_for_enumbled_model(enum, id)
71
+ define_dynamic_methods_and_constants_for_enumbled_model(enumble)
72
72
 
73
73
  @enumbles << enumble
74
74
  end
@@ -190,16 +190,22 @@ module Enumbler
190
190
 
191
191
  private
192
192
 
193
- def define_dynamic_methods_and_constants_for_enumbled_model(enum, id)
194
- method_name = "#{enum}?"
195
- not_method_name = "not_#{enum}?"
196
- alias_method_name = "is_#{enum}"
193
+ def define_dynamic_methods_and_constants_for_enumbled_model(enumble)
194
+ method_name = "#{enumble.enum}?"
195
+ not_method_name = "not_#{enumble.enum}?"
196
+ alias_method_name = "is_#{enumble.enum}"
197
197
 
198
- const_set(enum.to_s.upcase, id)
199
- define_method(method_name) { self.id == id }
200
- define_method(not_method_name) { self.id != id }
198
+ const_set(enumble.enum.to_s.upcase, enumble.id)
199
+ define_method(method_name) { id == enumble.id }
200
+ define_method(not_method_name) { id != enumble.id }
201
201
  alias_method alias_method_name, method_name
202
- define_singleton_method(enum) { find(id) }
202
+ define_singleton_method(enumble.enum) do |attr = nil|
203
+ return find(enumble.id) if attr.nil?
204
+
205
+ enumble.send(attr)
206
+ rescue NoMethodError
207
+ raise Enumbler::Error, "The attribute #{attr} is not supported on this Enumble."
208
+ end
203
209
  end
204
210
  end
205
211
  end
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Enumbler
4
4
  # Class that holds each row of Enumble data.
5
+ #
6
+ # @todo We need to support additional options/attributes beyond the id/label
7
+ # pairs. Is on the backburner for a moment.
5
8
  class Enumble
6
9
  attr_reader :id, :enum, :label, :label_column_name, :options
7
10
 
@@ -29,5 +32,16 @@ module Enumbler
29
32
  other.class == self.class &&
30
33
  (other.id == id || other.enum == enum || other.label == label)
31
34
  end
35
+
36
+ # Standardizing the enum for a GraphQL schema with an uppercase string
37
+ # value.
38
+ # @return [String]
39
+ def graphql_enum
40
+ enum.to_s.upcase
41
+ end
42
+
43
+ def to_s
44
+ enum
45
+ end
32
46
  end
33
47
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enumbler
4
- VERSION = '0.4.2'
4
+ VERSION = '0.5.0'
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.4.2
4
+ version: 0.5.0
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-05-22 00:00:00.000000000 Z
11
+ date: 2020-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -144,6 +144,7 @@ files:
144
144
  - bin/setup
145
145
  - enumbler.gemspec
146
146
  - lib/enumbler.rb
147
+ - lib/enumbler/collection.rb
147
148
  - lib/enumbler/enabler.rb
148
149
  - lib/enumbler/enumble.rb
149
150
  - lib/enumbler/version.rb