glue_gun_dsl 0.1.16 → 0.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc6aaef2a48bc81e0383a4b88f0afc691a3f99097e12877579c7d579953cfea7
4
- data.tar.gz: ceb228b7381a0fbc7e2b3c07fe650cc05d89524dcec7199490fafb8813117bcc
3
+ metadata.gz: '0962a5666114730f40d7b78a9e70e2762480f318cf4790641c47e8406d6af3aa'
4
+ data.tar.gz: 161576a11d2ff360e5c4fc34327c9db3ecf3201fe9f81fd864e84c34ce223c12
5
5
  SHA512:
6
- metadata.gz: 7624649374c85a5822bb6a74096e50c69947db98bd1097ec020e3e82edde37c35a9057905534271da63385ef1c37d43b479712d0442247837151c0ab23ef9cbc
7
- data.tar.gz: 1f18555d5938dc3fb16bed600e3216502601e0b97c8a7c0998b22dd636cea40263f5ec7225767232e873778690c5ca0d752786c4e5017b371c35b17d191277aa
6
+ metadata.gz: 3231f0758ae02faea304068b16bd9da17b6910a2f2f3ccd3ddec5c08ee6c9af651b36fc376e9e9a80d330d24448c4d18389cab87a76ade9fbd5db52eeaabb1d6
7
+ data.tar.gz: 1f5f68fd1879dc28a708214a794d0d2f39410b912e0659d63a67c2244dffb82b89dea4084bfa4db028c0e0aef46a501a4b017a000c64c7fc8027798a669c303e
data/lib/glue_gun/dsl.rb CHANGED
@@ -106,15 +106,13 @@ module GlueGun
106
106
  factory_class = options
107
107
  options = {}
108
108
  end
109
- is_array = options[:array] || false
110
- is_hash = options[:hash] || false
111
109
 
112
110
  if factory_class.present?
113
- dependency_definitions[component_type] = { factory_class: factory_class, array: is_array, hash: is_hash }
111
+ dependency_definitions[component_type] = { factory_class: factory_class }
114
112
  else
115
113
  dependency_builder = DependencyBuilder.new(component_type)
116
114
  dependency_builder.instance_eval(&block)
117
- dependency_definitions[component_type] = { builder: dependency_builder, array: is_array, hash: is_hash }
115
+ dependency_definitions[component_type] = { builder: dependency_builder }
118
116
  end
119
117
 
120
118
  # Define singleton method to allow hardcoding dependencies in subclasses
@@ -178,10 +176,51 @@ module GlueGun
178
176
  end
179
177
  end
180
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
+
181
220
  def initialize_dependency(component_type, init_args = {}, definition = nil)
182
221
  definition ||= self.class.dependency_definitions[component_type]
183
- is_array = definition[:array]
184
- is_hash = definition[:hash]
222
+ is_array = init_args.is_a?(Array)
223
+ is_hash = is_hash?(init_args, definition)
185
224
 
186
225
  if is_array
187
226
  dep = []
@@ -194,6 +233,8 @@ module GlueGun
194
233
  elsif is_hash
195
234
  dep = {}
196
235
  config = {}
236
+ validate_hash_dependencies(init_args, definition, component_type)
237
+
197
238
  init_args.each do |key, args|
198
239
  d, c = initialize_single_dependency(component_type, args, definition)
199
240
  dep[key] = d
@@ -390,14 +431,14 @@ module GlueGun
390
431
  else
391
432
  factory.dependency_definitions.values.first.values.first.option_configs
392
433
  end
393
- option_configs.values.select do |option|
434
+ option_configs.values.detect do |option|
394
435
  option_class = option.class_name
395
436
  value.is_a?(option_class)
396
437
  end
397
438
  end
398
439
 
399
440
  def dependency_injected?(component_type, value)
400
- injected_dependency(component_type, value).any?
441
+ injected_dependency(component_type, value).present?
401
442
  end
402
443
 
403
444
  def dependencies
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GlueGun
4
- VERSION = "0.1.16"
4
+ VERSION = "0.1.17"
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.16
4
+ version: 0.1.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brett Shollenberger