glue_gun_dsl 0.1.18 → 0.1.20

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: 1cf96906f1af3fd62ba4c0e7aa061d9d109aee45ced7284e7776cb74d7dca313
4
- data.tar.gz: 47f2454a802ce7a9bd335e7413fd2851551eae2437d4de9bebb0f1f31ad82b53
3
+ metadata.gz: d372ee94e1c1e2eea155447eb1cdb85a2e0b064e2645528dc8c624cc48a10825
4
+ data.tar.gz: 766a7cdad82a723ad9e0dd38bc151be81782ef6f3420473c5a912319fe648691
5
5
  SHA512:
6
- metadata.gz: 8f4be572fe72494ab001d7ea71ec0f35064d1914a7982ab026ffd12f40126dd01051c1594e9baf78ae3a6415b3bfb44d7524f512986af53dcaa0db3ab5924ba3
7
- data.tar.gz: fcf9a586af6236a3595b6a215daca49239a5e7407ab4086626dbcc105f755a76a82ae45de536047a7a8464be0035a89b81e8c4ca5bb11dd1c7aca31eae049b97
6
+ metadata.gz: 8776e38f53fa08187a26349fced6d516b57afb72469dca06421d583aa9c50b0d53cef3be513bb9bdb131c7b69e44ba4637afc0ec66634aa06f3527f3808ad8ac
7
+ data.tar.gz: 15faafc27a3812f4ec9ddb9b76d56c79512737e2960b8f746e680bd88954484b1274ef79f735b45fa116edd831a18ea564962e489cdba2983b2923a421d81737
data/lib/glue_gun/dsl.rb CHANGED
@@ -107,7 +107,7 @@ module GlueGun
107
107
  options = {}
108
108
  end
109
109
 
110
- dependency_builder = DependencyBuilder.new(component_type)
110
+ dependency_builder = DependencyBuilder.new(component_type, options)
111
111
 
112
112
  if factory_class.present?
113
113
  dependency_builder.set_factory_class(factory_class)
@@ -182,7 +182,12 @@ module GlueGun
182
182
  is_array = init_args.is_a?(Array)
183
183
  is_hash = definition.is_hash?(init_args)
184
184
 
185
- return nil if init_args.nil? && definition.default_option_name.nil?
185
+ if init_args.nil? && definition.default_option_name.nil?
186
+ return nil if definition.lazy?
187
+ if definition.when_block.nil?
188
+ raise "No default option or when block present for component_type #{component_type}. Don't know how to build!"
189
+ end
190
+ end
186
191
 
187
192
  if is_array
188
193
  dep = []
@@ -267,6 +272,10 @@ module GlueGun
267
272
  @dependencies ||= {}
268
273
  end
269
274
 
275
+ def inspect
276
+ "#<#{self.class} #{attributes.map { |k, v| "#{k}: #{v.inspect}" }.join(", ")}>"
277
+ end
278
+
270
279
  def validate_dependencies
271
280
  errors.clear
272
281
  self.class.dependency_definitions.keys.each do |component_type|
@@ -312,14 +321,15 @@ module GlueGun
312
321
  end
313
322
 
314
323
  class DependencyBuilder
315
- attr_reader :component_type, :option_configs, :when_block, :is_only, :factory_class
324
+ attr_reader :component_type, :option_configs, :when_block, :is_only, :factory_class, :lazy, :parent
316
325
 
317
- def initialize(component_type)
326
+ def initialize(component_type, options)
318
327
  @component_type = component_type
319
328
  @option_configs = {}
320
329
  @default_option_name = nil
321
330
  @single_option = nil
322
331
  @is_only = false
332
+ @lazy = options.key?(:lazy) ? options.dig(:lazy) : true
323
333
  end
324
334
 
325
335
  def set_factory_class(factory_class)
@@ -356,6 +366,10 @@ module GlueGun
356
366
  !factory?
357
367
  end
358
368
 
369
+ def lazy?
370
+ @lazy == true
371
+ end
372
+
359
373
  def is_hash?(init_args)
360
374
  return false unless init_args.is_a?(Hash)
361
375
 
@@ -369,33 +383,33 @@ module GlueGun
369
383
  end
370
384
  end
371
385
 
372
- def initialize_factory_dependency(init_args, parent)
373
- builder.initialize_single_dependency(init_args, parent)
386
+ def initialize_factory_dependency(init_args, instance)
387
+ builder.initialize_single_dependency(init_args, instance)
374
388
  end
375
389
 
376
- def initialize_builder_dependency(init_args, parent)
390
+ def initialize_builder_dependency(init_args, instance)
377
391
  if init_args && init_args.is_a?(Hash) && init_args.key?(:option_name)
378
392
  option_name = init_args[:option_name]
379
393
  init_args = init_args[:value]
380
394
  else
381
- option_name, init_args = determine_option_name(init_args)
395
+ option_name, init_args = determine_option_name(init_args, instance)
382
396
  end
383
397
 
384
398
  option_config = option_configs[option_name]
385
399
 
386
400
  raise ArgumentError, "Unknown #{component_type} option '#{option_name}'" unless option_config
387
401
 
388
- [instantiate_dependency(option_config, init_args, parent), option_config]
402
+ [instantiate_dependency(option_config, init_args, instance), option_config]
389
403
  end
390
404
 
391
- def initialize_single_dependency(init_args, parent)
405
+ def initialize_single_dependency(init_args, instance)
392
406
  if dependency_injected?(init_args)
393
407
  dep = init_args
394
408
  option_config = injected_dependency(init_args)
395
409
  elsif factory?
396
- dep, option_config = initialize_factory_dependency(init_args, parent)
410
+ dep, option_config = initialize_factory_dependency(init_args, instance)
397
411
  else
398
- dep, option_config = initialize_builder_dependency(init_args, parent)
412
+ dep, option_config = initialize_builder_dependency(init_args, instance)
399
413
  end
400
414
 
401
415
  [dep, option_config]
@@ -404,7 +418,7 @@ module GlueGun
404
418
  def build_dependency_attributes(option_config, dep_attributes, parent)
405
419
  option_config.attributes.each do |attr_name, attr_config|
406
420
  if dep_attributes.key?(attr_name)
407
- value = dep_attributes[attr_name]
421
+ dep_attributes[attr_name]
408
422
  else
409
423
  value = if attr_config.source && parent.respond_to?(attr_config.source)
410
424
  parent.send(attr_config.source)
@@ -421,12 +435,12 @@ module GlueGun
421
435
  dep_attributes
422
436
  end
423
437
 
424
- def determine_option_name(init_args)
438
+ def determine_option_name(init_args, instance)
425
439
  option_name = nil
426
440
 
427
441
  # Use when block if defined
428
442
  if when_block
429
- result = instance_exec(init_args, &when_block)
443
+ result = instance.instance_exec(init_args, &when_block)
430
444
  if result.is_a?(Hash) && result[:option]
431
445
  option_name = result[:option]
432
446
  as_attr = result[:as]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GlueGun
4
- VERSION = "0.1.18"
4
+ VERSION = "0.1.20"
5
5
  end
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.18
4
+ version: 0.1.20
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-17 00:00:00.000000000 Z
11
+ date: 2024-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel