glue_gun_dsl 0.1.24 → 0.1.26
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/glue_gun/model.rb +65 -16
- data/lib/glue_gun/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: ea68a31c6e7206e0aeeb14467b1335a39047b49d870fd79cde4ce334c7c6d860
|
4
|
+
data.tar.gz: 67eb48676fc5502ca0869041ecd6c5a7d653005ea14777fe315828eca035fd42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6830bf72b7f05306b46eb9b987f2bd15facc0510c1e2983b2c0a51802807f39518a5d6829dc499a54138a47e797e433ea16ee2b7ef1131b594e50677a64fcefc
|
7
|
+
data.tar.gz: 450f8dd49be6d2b0cdc9d840f4c03f404c1b7aaf69664ed3666b7e0cbef948952754e71ee12484a634b4661cb2e3a42e99eabc2e3d246319f3cc75e7c7598969
|
data/lib/glue_gun/model.rb
CHANGED
@@ -3,6 +3,40 @@ module GlueGun
|
|
3
3
|
module Model
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
|
+
class ServiceRegistry
|
7
|
+
attr_accessor :services
|
8
|
+
|
9
|
+
def register(k, v)
|
10
|
+
@services ||= {}
|
11
|
+
@services[k.to_sym] = v
|
12
|
+
end
|
13
|
+
|
14
|
+
def options
|
15
|
+
services.keys
|
16
|
+
end
|
17
|
+
|
18
|
+
def []=(k, v)
|
19
|
+
register(k, v)
|
20
|
+
end
|
21
|
+
|
22
|
+
def [](k)
|
23
|
+
return nil if k.nil?
|
24
|
+
|
25
|
+
@services ||= {}
|
26
|
+
@services.dig(k.to_sym)
|
27
|
+
end
|
28
|
+
|
29
|
+
def default_key
|
30
|
+
return unless services.keys.count == 1
|
31
|
+
|
32
|
+
services.keys.first
|
33
|
+
end
|
34
|
+
|
35
|
+
def default
|
36
|
+
default_key.present? ? services[default_key] : nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
6
40
|
included do
|
7
41
|
include GlueGun::Shared
|
8
42
|
|
@@ -16,6 +50,13 @@ module GlueGun
|
|
16
50
|
# Set default service attribute name based on the class name
|
17
51
|
self.service_attribute_name = "#{name.demodulize.underscore}_service".to_sym
|
18
52
|
|
53
|
+
class_attribute :service_registry
|
54
|
+
self.service_registry = ServiceRegistry.new
|
55
|
+
|
56
|
+
# Set default option key based on the class name
|
57
|
+
class_attribute :option_key
|
58
|
+
self.option_key = "#{name.demodulize.underscore}_type".to_sym
|
59
|
+
|
19
60
|
def assign_attributes(attributes)
|
20
61
|
return if attributes.blank?
|
21
62
|
|
@@ -31,14 +72,8 @@ module GlueGun
|
|
31
72
|
end
|
32
73
|
|
33
74
|
class_methods do
|
34
|
-
def service(
|
35
|
-
|
36
|
-
self.service_class = class_or_proc
|
37
|
-
elsif block_given?
|
38
|
-
self.service_class_resolver = block
|
39
|
-
else
|
40
|
-
raise ArgumentError, "You must provide a service class, factory, or a block to resolve the service class."
|
41
|
-
end
|
75
|
+
def service(key, service_class)
|
76
|
+
service_registry[key] = service_class
|
42
77
|
end
|
43
78
|
|
44
79
|
def find_or_create_by!(attributes)
|
@@ -77,8 +112,9 @@ module GlueGun
|
|
77
112
|
end
|
78
113
|
|
79
114
|
def initialize(attributes = {})
|
80
|
-
attributes[:root_dir]
|
115
|
+
attributes[:root_dir] ||= detect_root_dir
|
81
116
|
attributes = attributes.deep_symbolize_keys
|
117
|
+
attributes[option_key] ||= resolve_service_type(attributes, true)
|
82
118
|
db_attributes = self.class.extract_db_attributes(attributes)
|
83
119
|
super(db_attributes)
|
84
120
|
build_service_object(attributes)
|
@@ -119,14 +155,26 @@ module GlueGun
|
|
119
155
|
instance_variable_set("@#{service_attribute_name}", service_instance)
|
120
156
|
end
|
121
157
|
|
158
|
+
def resolve_service_type(attributes, initializing = false)
|
159
|
+
attrs = if initializing || !persisted? || attributes.key?(self.class.option_key)
|
160
|
+
attributes
|
161
|
+
else
|
162
|
+
{ self.class.option_key => send(self.class.option_key) }
|
163
|
+
end
|
164
|
+
attrs[self.class.option_key] || self.class.service_registry.default_key
|
165
|
+
end
|
166
|
+
|
122
167
|
def resolve_service_class(attributes)
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
raise
|
168
|
+
type = resolve_service_type(attributes)
|
169
|
+
service_class = self.class.service_registry[type] || self.class.service_registry.default
|
170
|
+
|
171
|
+
unless service_class
|
172
|
+
available_types = self.class.service_registry.options
|
173
|
+
raise ArgumentError,
|
174
|
+
"#{self.class} requires argument #{self.class.option_key}. Invalid option key received: #{type}. Allowed options are: #{available_types}"
|
129
175
|
end
|
176
|
+
|
177
|
+
service_class
|
130
178
|
end
|
131
179
|
|
132
180
|
def extract_service_attributes(attributes, service_class)
|
@@ -179,8 +227,9 @@ module GlueGun
|
|
179
227
|
raise "Don't know how to serialize dependency of type #{dep}, available options are #{opts.keys}. You didn't specify an option."
|
180
228
|
end
|
181
229
|
|
230
|
+
serialized = this_dep.respond_to?(:serialize) ? this_dep.serialize : this_dep.attributes
|
182
231
|
hash[dep] = {
|
183
|
-
selected_option =>
|
232
|
+
selected_option => serialized
|
184
233
|
}
|
185
234
|
end
|
186
235
|
end
|
data/lib/glue_gun/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: glue_gun_dsl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.26
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brett Shollenberger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-10-
|
11
|
+
date: 2024-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|