enumbler 0.6.2 → 0.6.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +13 -3
- data/lib/enumbler.rb +12 -0
- data/lib/enumbler/enabler.rb +34 -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: b325cf47a0a64b573f1347bffbbea7338cc57b7422dfdf3f0da39d412274d8cb
|
4
|
+
data.tar.gz: 10affa6956ef071ec34ad4195379d632b86945745e8d0eb7718fc8735427fe9e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a38a602c49f7a0bf3f69781b4a54d555bc92a23bd61788a27bbf858d75aa554599638a77af3afd8d2088b8e94fcc69768c63d3e2268b7ea0384fc4f74a955dfa
|
7
|
+
data.tar.gz: 98fae8e4a32aa7207f55fe07128ca7e4730c50701cfac378756ff32c27e8b745ab471afb88401cf8096e75b42deffe7ec031d9d9e1d3d0ba97bdddc37774058c
|
data/Gemfile.lock
CHANGED
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
|
@@ -86,10 +92,14 @@ Color.ids_from_enumbler!(:black, 'does-no-exist') # => raises Enumbler::Error
|
|
86
92
|
Color.id_from_enumbler!(:does_not_exist) # => raises Enumbler::Error
|
87
93
|
|
88
94
|
# Get enumble object by id
|
89
|
-
|
90
95
|
house = House.create!(color: Color.black)
|
91
|
-
|
92
|
-
|
96
|
+
|
97
|
+
# These are all db-free lookups
|
98
|
+
house.color_label # => 'black'
|
99
|
+
house.color_enum # => :black
|
100
|
+
house.color_graphql_enum # => 'BLACK'
|
101
|
+
house.black? # => true
|
102
|
+
house.not_black? # => false
|
93
103
|
|
94
104
|
house2 = House.create!(color: Color.white)
|
95
105
|
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
|
@@ -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)
|
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.7
|
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-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|