glue_gun_dsl 0.1.15 → 0.1.17

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: a573097a1db8e4af5573a1b0374e43dc12dcadc22f4e4a950bd6d5fe4a9084a1
4
- data.tar.gz: 1db836fbee10ab0594495e5f137f0337ec62a8b90eeabc29bc55be0dab6ade9b
3
+ metadata.gz: '0962a5666114730f40d7b78a9e70e2762480f318cf4790641c47e8406d6af3aa'
4
+ data.tar.gz: 161576a11d2ff360e5c4fc34327c9db3ecf3201fe9f81fd864e84c34ce223c12
5
5
  SHA512:
6
- metadata.gz: ad634073ed8206277b1eb502b2da7eff90e92cc820adcabc1dc647dee8e2f0c7cdf4ea7c2cc9fdd4b8a3b5f46caff7a6d4c55f82d3780a180b719af5729d4a51
7
- data.tar.gz: 3048844dcbae257696ab142798ead0957b97bda029c151da4d9e5181b90d3da43099ecd6ec79784efb2499b0fd699a0dca2e24513e1fc20c461dae3648a4390d
6
+ metadata.gz: 3231f0758ae02faea304068b16bd9da17b6910a2f2f3ccd3ddec5c08ee6c9af651b36fc376e9e9a80d330d24448c4d18389cab87a76ade9fbd5db52eeaabb1d6
7
+ data.tar.gz: 1f5f68fd1879dc28a708214a794d0d2f39410b912e0659d63a67c2244dffb82b89dea4084bfa4db028c0e0aef46a501a4b017a000c64c7fc8027798a669c303e
data/lib/glue_gun/dsl.rb CHANGED
@@ -106,14 +106,13 @@ module GlueGun
106
106
  factory_class = options
107
107
  options = {}
108
108
  end
109
- is_array = options[:array] || false
110
109
 
111
110
  if factory_class.present?
112
- dependency_definitions[component_type] = { factory_class: factory_class, array: is_array }
111
+ dependency_definitions[component_type] = { factory_class: factory_class }
113
112
  else
114
113
  dependency_builder = DependencyBuilder.new(component_type)
115
114
  dependency_builder.instance_eval(&block)
116
- dependency_definitions[component_type] = { builder: dependency_builder, array: is_array }
115
+ dependency_definitions[component_type] = { builder: dependency_builder }
117
116
  end
118
117
 
119
118
  # Define singleton method to allow hardcoding dependencies in subclasses
@@ -177,9 +176,51 @@ module GlueGun
177
176
  end
178
177
  end
179
178
 
179
+ def allowed_configurations(init_args, definition)
180
+ if definition[:factory_class]
181
+ factory_instance = definition[:factory_class].new
182
+ dep_defs = factory_instance.dependency_definitions
183
+ definition = dep_defs[dep_defs.keys.first]
184
+ return allowed_configurations(init_args, definition)
185
+ elsif definition[:builder]
186
+ builder = definition[:builder]
187
+ allowed_configs = builder.option_configs.keys
188
+ end
189
+
190
+ allowed_configs
191
+ end
192
+
193
+ def is_hash?(init_args, definition)
194
+ return false unless init_args.is_a?(Hash)
195
+
196
+ allowed_configs = allowed_configurations(init_args, definition)
197
+ return false if allowed_configs.count == 1 && allowed_configs == [:default]
198
+
199
+ if init_args.key?(:option_name)
200
+ allowed_configs.exclude?(init_args[:option_name])
201
+ else
202
+ init_args.keys.none? { |k| allowed_configs.include?(k) }
203
+ end
204
+ end
205
+
206
+ def validate_hash_dependencies(init_args, definition, component_type)
207
+ allowed_configs = allowed_configurations(init_args, definition)
208
+
209
+ init_args.each do |_named_key, configuration|
210
+ next unless configuration.is_a?(Hash)
211
+
212
+ key = configuration.keys.first
213
+ if key.nil? || allowed_configs.exclude?(key)
214
+ raise ArgumentError,
215
+ "Unknown #{component_type} option: #{init_args.keys.first}."
216
+ end
217
+ end
218
+ end
219
+
180
220
  def initialize_dependency(component_type, init_args = {}, definition = nil)
181
221
  definition ||= self.class.dependency_definitions[component_type]
182
- is_array = definition[:array]
222
+ is_array = init_args.is_a?(Array)
223
+ is_hash = is_hash?(init_args, definition)
183
224
 
184
225
  if is_array
185
226
  dep = []
@@ -189,6 +230,16 @@ module GlueGun
189
230
  dep.push(d)
190
231
  config.push(c)
191
232
  end
233
+ elsif is_hash
234
+ dep = {}
235
+ config = {}
236
+ validate_hash_dependencies(init_args, definition, component_type)
237
+
238
+ init_args.each do |key, args|
239
+ d, c = initialize_single_dependency(component_type, args, definition)
240
+ dep[key] = d
241
+ config[key] = c
242
+ end
192
243
  else
193
244
  dep, config = initialize_single_dependency(component_type, init_args, definition)
194
245
  end
@@ -341,6 +392,12 @@ module GlueGun
341
392
  dependency_instance.zip(option_config).each do |dep, opt|
342
393
  propagate_attribute_to_instance(attr_name, value, dep, opt)
343
394
  end
395
+ elsif dependency_instance.is_a?(Hash)
396
+ option_config = dependencies.dig(component_type, :option)
397
+
398
+ dependency_instance.each do |key, dep|
399
+ propagate_attribute_to_instance(attr_name, value, dep, option_config[key])
400
+ end
344
401
  else
345
402
  option_config = dependencies.dig(component_type, :option)
346
403
  next unless option_config
@@ -374,14 +431,14 @@ module GlueGun
374
431
  else
375
432
  factory.dependency_definitions.values.first.values.first.option_configs
376
433
  end
377
- option_configs.values.select do |option|
434
+ option_configs.values.detect do |option|
378
435
  option_class = option.class_name
379
436
  value.is_a?(option_class)
380
437
  end
381
438
  end
382
439
 
383
440
  def dependency_injected?(component_type, value)
384
- injected_dependency(component_type, value).any?
441
+ injected_dependency(component_type, value).present?
385
442
  end
386
443
 
387
444
  def dependencies
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GlueGun
4
- VERSION = "0.1.15"
4
+ VERSION = "0.1.17"
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.15
4
+ version: 0.1.17
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-15 00:00:00.000000000 Z
11
+ date: 2024-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel