glue_gun_dsl 0.1.26 → 0.1.27
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 +59 -18
- data/lib/glue_gun/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 697e6798ae2b2c4c3db3f4e3cd9b2c65ccc8081386636c08366e4d023cb04afe
|
4
|
+
data.tar.gz: f7a025244f1b5627691676d784658d2a14a289914533e17f4c1daca896c6ebdf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d32f478f2e294ae9510abb04b0ac5b5490c8468918879363f790eebc5167d7c3e28ec5edf9675301a2a211b7f5493bcf24c1884295598e9cf859f5b6b146f21
|
7
|
+
data.tar.gz: ee627464ed957cffac221bd2d701e9a9deba768e1cac558349878d101b67281a2c5977379f79496af43ed7bdbc392489c60e7d3fec2d09445b2e35526d2d256f
|
data/lib/glue_gun/model.rb
CHANGED
@@ -112,6 +112,7 @@ module GlueGun
|
|
112
112
|
end
|
113
113
|
|
114
114
|
def initialize(attributes = {})
|
115
|
+
attributes = {} if attributes.nil?
|
115
116
|
attributes[:root_dir] ||= detect_root_dir
|
116
117
|
attributes = attributes.deep_symbolize_keys
|
117
118
|
attributes[option_key] ||= resolve_service_type(attributes, true)
|
@@ -210,30 +211,45 @@ module GlueGun
|
|
210
211
|
end.symbolize_keys
|
211
212
|
end
|
212
213
|
|
214
|
+
def serialize_dependency(dependency, dep_instance = nil)
|
215
|
+
dep_instance = service_object.send(dependency) if dep_instance.nil?
|
216
|
+
return nil unless dep_instance.present?
|
217
|
+
|
218
|
+
return dep_instance.map { |dep| serialize_dependency(dependency, dep) } if dep_instance.is_a?(Array)
|
219
|
+
|
220
|
+
opts = service_object.dependency_definitions[dependency].option_configs
|
221
|
+
selected_option = opts.detect do |_k, v|
|
222
|
+
dep_instance.class == v.class_name
|
223
|
+
end&.first
|
224
|
+
unless selected_option.present?
|
225
|
+
raise "Don't know how to serialize dependency of type #{dependency}, available options are #{opts.keys}. You didn't specify an option."
|
226
|
+
end
|
227
|
+
|
228
|
+
serialized = dep_instance.respond_to?(:serialize) ? dep_instance.serialize : dep_instance.attributes.deep_compact
|
229
|
+
{
|
230
|
+
selected_option => serialized
|
231
|
+
}
|
232
|
+
end
|
233
|
+
|
234
|
+
def service_object
|
235
|
+
instance_variable_get("@#{service_attribute_name}")
|
236
|
+
end
|
237
|
+
|
238
|
+
def service_class
|
239
|
+
service_object.class
|
240
|
+
end
|
241
|
+
|
213
242
|
def serialize_service_object
|
214
|
-
|
215
|
-
service_klass = service_object.class
|
216
|
-
attrs = service_klass.respond_to?(:serialize) ? service_klass.serialize(service_object) : service_object.attributes
|
243
|
+
attrs = service_object.respond_to?(:serialize) ? service_object.serialize : service_object.attributes.deep_compact
|
217
244
|
deps = allowed_names(service_object.dependency_definitions.keys).inject({}) do |hash, dep|
|
218
245
|
hash.tap do
|
219
|
-
|
220
|
-
next
|
221
|
-
|
222
|
-
opts = service_object.dependency_definitions[dep].option_configs
|
223
|
-
selected_option = opts.detect do |_k, v|
|
224
|
-
this_dep.class == v.class_name
|
225
|
-
end&.first
|
226
|
-
unless selected_option.present?
|
227
|
-
raise "Don't know how to serialize dependency of type #{dep}, available options are #{opts.keys}. You didn't specify an option."
|
228
|
-
end
|
246
|
+
serialized = serialize_dependency(dep)
|
247
|
+
next if serialized.nil?
|
229
248
|
|
230
|
-
|
231
|
-
hash[dep] = {
|
232
|
-
selected_option => serialized
|
233
|
-
}
|
249
|
+
hash[dep] = serialized
|
234
250
|
end
|
235
251
|
end
|
236
|
-
json = serializable!(attrs.merge(deps
|
252
|
+
json = serializable!(attrs.merge(deps).deep_symbolize_keys)
|
237
253
|
write_attribute(:configuration, json.to_json)
|
238
254
|
end
|
239
255
|
|
@@ -256,12 +272,37 @@ module GlueGun
|
|
256
272
|
json
|
257
273
|
end
|
258
274
|
|
275
|
+
def deserialize_dependency(serialized, definition)
|
276
|
+
return serialized.map { |dep| deserialize_dependency(dep, definition) } if serialized.is_a?(Array)
|
277
|
+
|
278
|
+
dep_name = serialized.keys.first
|
279
|
+
selected_option = definition.option_configs[dep_name]
|
280
|
+
dependency_class = selected_option.class_name
|
281
|
+
arguments = serialized[dep_name]
|
282
|
+
|
283
|
+
dependency_class.respond_to?(:deserialize) ? dependency_class.deserialize(arguments) : arguments
|
284
|
+
{
|
285
|
+
dep_name => arguments
|
286
|
+
}
|
287
|
+
end
|
288
|
+
|
289
|
+
def deserialize_dependencies(serialized_data, service_class)
|
290
|
+
serialized_deps = (serialized_data.keys & allowed_names(service_class.dependency_definitions.keys))
|
291
|
+
serialized_deps.each do |name|
|
292
|
+
serialized = serialized_data[name]
|
293
|
+
definition = service_class.dependency_definitions[name]
|
294
|
+
serialized_data[name] = deserialize_dependency(serialized, definition)
|
295
|
+
end
|
296
|
+
serialized_data
|
297
|
+
end
|
298
|
+
|
259
299
|
def deserialize_service_object
|
260
300
|
serialized_data = JSON.parse(read_attribute(:configuration) || "{}")
|
261
301
|
serialized_data.deep_symbolize_keys!
|
262
302
|
service_class = resolve_service_class(serialized_data)
|
263
303
|
serialized_data = deserialize_associations(serialized_data)
|
264
304
|
serialized_data = service_class.deserialize(serialized_data) if service_class.respond_to?(:deserialize)
|
305
|
+
serialized_data = deserialize_dependencies(serialized_data, service_class)
|
265
306
|
service_instance = build_service_object(serialized_data)
|
266
307
|
instance_variable_set("@#{service_attribute_name}", service_instance)
|
267
308
|
end
|
data/lib/glue_gun/version.rb
CHANGED