glue_gun_dsl 0.1.26 → 0.1.28

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea68a31c6e7206e0aeeb14467b1335a39047b49d870fd79cde4ce334c7c6d860
4
- data.tar.gz: 67eb48676fc5502ca0869041ecd6c5a7d653005ea14777fe315828eca035fd42
3
+ metadata.gz: b5b050524f016e18d7b9937fbc5bbba5e8bf989690fdfda10a6bd0adf09aeb86
4
+ data.tar.gz: 2eab6853e671117b5253de7a88b9edb0f5274c5688ddd5817ce35ad77d8185de
5
5
  SHA512:
6
- metadata.gz: 6830bf72b7f05306b46eb9b987f2bd15facc0510c1e2983b2c0a51802807f39518a5d6829dc499a54138a47e797e433ea16ee2b7ef1131b594e50677a64fcefc
7
- data.tar.gz: 450f8dd49be6d2b0cdc9d840f4c03f404c1b7aaf69664ed3666b7e0cbef948952754e71ee12484a634b4661cb2e3a42e99eabc2e3d246319f3cc75e7c7598969
6
+ metadata.gz: 30b842fe667aa8f8785a903d1b61f3e3fb65799cc417214392ecde7022251fd3ab212f81d5d26ea9d8818ca3241649729eb92b8e6454c128a8659f12d3f2f96a
7
+ data.tar.gz: c000b550317b50a2c17c985f8d7e574e4bef04444e20f9cfdecef46420efa14e931a11f01334e1ad06f04889a60a3dedd29cdd3118fab478236b0703a06b3bbb
@@ -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,57 @@ module GlueGun
210
211
  end.symbolize_keys
211
212
  end
212
213
 
214
+ def serialize_object(object)
215
+ if object.respond_to?(:serialize)
216
+ object.serialize
217
+ elsif object.respond_to?(:attributes)
218
+ object.attributes.deep_compact
219
+ else
220
+ Hash[object.instance_variables.map do |var|
221
+ [var.to_s.delete("@"), object.instance_variable_get(var)]
222
+ end].deep_compact
223
+ end
224
+ end
225
+
226
+ def serialize_dependency(dependency, dep_instance = nil)
227
+ dep_instance = service_object.send(dependency) if dep_instance.nil?
228
+ return nil unless dep_instance.present?
229
+
230
+ return dep_instance.map { |dep| serialize_dependency(dependency, dep) } if dep_instance.is_a?(Array)
231
+
232
+ opts = service_object.dependency_definitions[dependency].option_configs
233
+ selected_option = opts.detect do |_k, v|
234
+ dep_instance.class == v.class_name
235
+ end&.first
236
+ unless selected_option.present?
237
+ raise "Don't know how to serialize dependency of type #{dependency}, available options are #{opts.keys}. You didn't specify an option."
238
+ end
239
+
240
+ serialized = serialize_object(dep_instance)
241
+ {
242
+ selected_option => serialized
243
+ }
244
+ end
245
+
246
+ def service_object
247
+ instance_variable_get("@#{service_attribute_name}")
248
+ end
249
+
250
+ def service_class
251
+ service_object.class
252
+ end
253
+
213
254
  def serialize_service_object
214
- service_object = instance_variable_get("@#{service_attribute_name}")
215
- service_klass = service_object.class
216
- attrs = service_klass.respond_to?(:serialize) ? service_klass.serialize(service_object) : service_object.attributes
255
+ attrs = serialize_object(service_object)
217
256
  deps = allowed_names(service_object.dependency_definitions.keys).inject({}) do |hash, dep|
218
257
  hash.tap do
219
- this_dep = service_object.send(dep)
220
- next unless this_dep.present?
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
258
+ serialized = serialize_dependency(dep)
259
+ next if serialized.nil?
229
260
 
230
- serialized = this_dep.respond_to?(:serialize) ? this_dep.serialize : this_dep.attributes
231
- hash[dep] = {
232
- selected_option => serialized
233
- }
261
+ hash[dep] = serialized
234
262
  end
235
263
  end
236
- json = serializable!(attrs.merge(deps.deep_compact).deep_symbolize_keys)
264
+ json = serializable!(attrs.merge(deps).deep_symbolize_keys)
237
265
  write_attribute(:configuration, json.to_json)
238
266
  end
239
267
 
@@ -256,12 +284,37 @@ module GlueGun
256
284
  json
257
285
  end
258
286
 
287
+ def deserialize_dependency(serialized, definition)
288
+ return serialized.map { |dep| deserialize_dependency(dep, definition) } if serialized.is_a?(Array)
289
+
290
+ dep_name = serialized.keys.first
291
+ selected_option = definition.option_configs[dep_name]
292
+ dependency_class = selected_option.class_name
293
+ arguments = serialized[dep_name]
294
+
295
+ dependency_class.respond_to?(:deserialize) ? dependency_class.deserialize(arguments) : arguments
296
+ {
297
+ dep_name => arguments
298
+ }
299
+ end
300
+
301
+ def deserialize_dependencies(serialized_data, service_class)
302
+ serialized_deps = (serialized_data.keys & allowed_names(service_class.dependency_definitions.keys))
303
+ serialized_deps.each do |name|
304
+ serialized = serialized_data[name]
305
+ definition = service_class.dependency_definitions[name]
306
+ serialized_data[name] = deserialize_dependency(serialized, definition)
307
+ end
308
+ serialized_data
309
+ end
310
+
259
311
  def deserialize_service_object
260
312
  serialized_data = JSON.parse(read_attribute(:configuration) || "{}")
261
313
  serialized_data.deep_symbolize_keys!
262
314
  service_class = resolve_service_class(serialized_data)
263
315
  serialized_data = deserialize_associations(serialized_data)
264
316
  serialized_data = service_class.deserialize(serialized_data) if service_class.respond_to?(:deserialize)
317
+ serialized_data = deserialize_dependencies(serialized_data, service_class)
265
318
  service_instance = build_service_object(serialized_data)
266
319
  instance_variable_set("@#{service_attribute_name}", service_instance)
267
320
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GlueGun
4
- VERSION = "0.1.26"
4
+ VERSION = "0.1.28"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glue_gun_dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.26
4
+ version: 0.1.28
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brett Shollenberger