enumbler 0.6.1 → 0.6.6
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +10 -3
- data/lib/enumbler.rb +12 -0
- data/lib/enumbler/enabler.rb +31 -4
- data/lib/enumbler/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c7090cf447bc470848993d1b219762453e4188c8aea3c11bf3bb4daf3d5ed771
|
4
|
+
data.tar.gz: 2fc9f318859ab4c8f6dc493f00d565b3c68ca41d0d804beeb8ac3f251262423e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 59b4721f1cdbdb5174692d1c13b784cd9eae230a67a99d34f87e4696602d75bd91f005a140ea5ff8f0ec4731102e10f9eb193f2deaaf8537465720c569c10faf
|
7
|
+
data.tar.gz: 704eca26b618018b976e165146182bda016440a9d4ff652ccb7351e70dfacc587c8d9fb1bf96647185adbe439f7a6b59d2f4798a3cbe4d6c4d45d58224daae51
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -73,6 +73,9 @@ Color.find_enumbles(:black, 'does-not-exist') # => [Enumbler::Enumble<:black>, n
|
|
73
73
|
|
74
74
|
Color.find_enumble(:black) # => Enumbler::Enumble<:black>
|
75
75
|
|
76
|
+
# Is pretty flexible with findidng things from strings
|
77
|
+
Color.find_enumble('Dark_Brown') # => Enumbler::Enumble<:dark-brown>
|
78
|
+
|
76
79
|
# raises errors if none found
|
77
80
|
Color.find_enumbles!!(:black, 'does-no-exist') # => raises Enumbler::Error
|
78
81
|
Color.find_enumble!(:does_not_exist) # => raises Enumbler::Error
|
@@ -86,10 +89,14 @@ Color.ids_from_enumbler!(:black, 'does-no-exist') # => raises Enumbler::Error
|
|
86
89
|
Color.id_from_enumbler!(:does_not_exist) # => raises Enumbler::Error
|
87
90
|
|
88
91
|
# Get enumble object by id
|
89
|
-
|
90
92
|
house = House.create!(color: Color.black)
|
91
|
-
|
92
|
-
|
93
|
+
|
94
|
+
# These are all db-free lookups
|
95
|
+
house.color_label # => 'black'
|
96
|
+
house.color_enum # => :black
|
97
|
+
house.color_graphql_enum # => 'BLACK'
|
98
|
+
house.black? # => true
|
99
|
+
house.not_black? # => false
|
93
100
|
|
94
101
|
house2 = House.create!(color: Color.white)
|
95
102
|
House.color(:black, :white) # => ActiveRecord::Relation<house, house2>
|
data/lib/enumbler.rb
CHANGED
@@ -65,6 +65,7 @@ module Enumbler
|
|
65
65
|
|
66
66
|
belongs_to(name, scope, **options)
|
67
67
|
|
68
|
+
define_helper_attributes(name)
|
68
69
|
define_dynamic_methods_for_enumbled_to_models(enumbled_model, prefix: prefix, scope_prefix: scope_prefix)
|
69
70
|
rescue NameError
|
70
71
|
raise Error, "The model #{class_name} cannot be found. Uninitialized constant."
|
@@ -97,5 +98,16 @@ module Enumbler
|
|
97
98
|
define_method(not_method_name) { self[column_name] != enumble.id }
|
98
99
|
end
|
99
100
|
end
|
101
|
+
|
102
|
+
# Add the attirbutes:
|
103
|
+
#
|
104
|
+
# house.color_label #=> 'black'
|
105
|
+
# house.color_enum #=> :black
|
106
|
+
# house.color_graphql_enum #=> 'BLACK'
|
107
|
+
def define_helper_attributes(name)
|
108
|
+
%i[label enum graphql_enum].each do |sym|
|
109
|
+
define_method("#{name}_#{sym}") { send(name).enumble.send(sym) }
|
110
|
+
end
|
111
|
+
end
|
100
112
|
end
|
101
113
|
end
|
data/lib/enumbler/enabler.rb
CHANGED
@@ -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.
|
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
|
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
|
@@ -313,6 +328,15 @@ module Enumbler
|
|
313
328
|
end
|
314
329
|
end
|
315
330
|
|
331
|
+
# I accidentally forgot to provide an id one time and it was confusing as
|
332
|
+
# the last argument became the hash of options. This should help.
|
333
|
+
def validate_id_is_numeric(enum, id)
|
334
|
+
Integer(id)
|
335
|
+
rescue ArgumentError, TypeError
|
336
|
+
raise Enumbler::Error,
|
337
|
+
"You must provide a numeric primary key, like: `enumble :#{enum}, 1 `"
|
338
|
+
end
|
339
|
+
|
316
340
|
def raise_error_if_model_does_not_support_attributes(attributes)
|
317
341
|
return if attributes.blank?
|
318
342
|
|
@@ -322,6 +346,9 @@ module Enumbler
|
|
322
346
|
|
323
347
|
raise Enumbler::Error,
|
324
348
|
"The model #{self} does not support the attribute(s): #{unsupported_attrs.keys.map(&:to_s).to_sentence}"
|
349
|
+
rescue ActiveRecord::StatementInvalid
|
350
|
+
warn "[Enumbler Warning] => Unable to find a table for #{self}."\
|
351
|
+
'This is to be expected if there is a pending migration; however, if there is not then something is amiss.'
|
325
352
|
end
|
326
353
|
end
|
327
354
|
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.6.
|
4
|
+
version: 0.6.6
|
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-08-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|