enumbler 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +15 -15
- data/README.md +14 -7
- data/lib/enumbler.rb +1 -0
- data/lib/enumbler/collection.rb +24 -0
- data/lib/enumbler/enabler.rb +16 -10
- data/lib/enumbler/enumble.rb +14 -0
- data/lib/enumbler/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2eb7435b5b57b7fa3b04f7201bc41b3caaea6f148af6998fbdad02bdd44794bd
|
4
|
+
data.tar.gz: cf198237307efcbc457e55c3b046e612ba44a9fef52b86bb47f02a0989350df0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e39538cefdfe366d8c944732796c0354c49f7fbc9930d7f3a4fb7912b6088188a1b1dbbd9f9395a4ea67f2c857a1ea7490f3631285daec68c388c19779b95602
|
7
|
+
data.tar.gz: f490870fc3d1bfe7f7a10bad501619668929f351f2882455e5e7da0581511957bd95fa08c77c09715e4b54f659212f742736256e9bca4706e906d1e0b3629551
|
data/Gemfile.lock
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
enumbler (0.
|
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.
|
12
|
-
activesupport (= 6.0.
|
13
|
-
activerecord (6.0.
|
14
|
-
activemodel (= 6.0.
|
15
|
-
activesupport (= 6.0.
|
16
|
-
activesupport (6.0.
|
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.
|
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.
|
35
|
+
minitest (5.14.1)
|
36
36
|
parallel (1.19.1)
|
37
|
-
parser (2.7.1.
|
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.
|
47
|
-
rspec-support (~> 3.9.
|
48
|
-
rspec-expectations (3.9.
|
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.
|
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
|
58
|
-
Color.black
|
59
|
-
Color.black.black?
|
60
|
-
Color.black.is_black
|
61
|
-
Color.white.not_black?
|
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
|
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
|
-
|
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
|
|
data/lib/enumbler.rb
CHANGED
@@ -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
|
data/lib/enumbler/enabler.rb
CHANGED
@@ -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(
|
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(
|
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) {
|
200
|
-
define_method(not_method_name) {
|
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)
|
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
|
data/lib/enumbler/enumble.rb
CHANGED
@@ -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
|
data/lib/enumbler/version.rb
CHANGED
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
|
+
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-
|
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
|