glue_gun_dsl 0.1.1 → 0.1.3

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: 8aa5ea204121b89b5eb7006dfcabd9816a9fa73523e31fcd92c714f2812ab562
4
- data.tar.gz: a995bb16c873adaf819ad459e937930dce7d1addadfe337044833f5810232f14
3
+ metadata.gz: e97baa6c4e171ff7016d72e17e6aa8bbaac29f1787334e2adf179286e6e3acab
4
+ data.tar.gz: bf07da1a28c264674715852b26548c82b91c8a752079ce81ab463dddf33dd54c
5
5
  SHA512:
6
- metadata.gz: 2f6aa771fc215e8cf9c09f62dfa605684f4677722be474da08309221a0a2da55c3db7fc6b6518ab7d6cd8dd6befb06f4ea3b1d9a973d54aa5375db663848482c
7
- data.tar.gz: 2527ad788ef9dc415664e70723233f13b999256c2e5a19e73092e33e1edef10d1fc04c7627650421df7734c2160a21aa579b88639225158930ca82e3243be0c6
6
+ metadata.gz: 773d89754959c7bcdc8aa211e52ba5f62d51803388bbf0938081efea5acbb0eba69fae543a5ef9b41315018fcaa530a2f709e78afe89615f57c85912848fc2e3
7
+ data.tar.gz: c85ffa384466da228d4442b303db20bd07ae83262981b24b10675d67eaca168f7bdf6a14f88ee617c780402086b10677fc0302b8d08096851bfbcebb5294a2bf
data/lib/glue_gun/dsl.rb CHANGED
@@ -166,20 +166,29 @@ module GlueGun
166
166
 
167
167
  def build_dependency_attributes(option_config, dep_attributes)
168
168
  option_config.attributes.each do |attr_name, attr_config|
169
- next if dep_attributes.key?(attr_name)
169
+ # If the attribute is already provided, use it
170
+ if dep_attributes.key?(attr_name)
171
+ value = dep_attributes[attr_name]
172
+ else
173
+ value = if attr_config.source && respond_to?(attr_config.source)
174
+ send(attr_config.source)
175
+ elsif respond_to?(attr_name)
176
+ send(attr_name)
177
+ else
178
+ attr_config.default
179
+ end
170
180
 
171
- value = if attr_config.source && respond_to?(attr_config.source)
172
- send(attr_config.source)
173
- elsif respond_to?(attr_name)
174
- send(attr_name)
175
- else
176
- attr_config.default
177
- end
181
+ value = attr_config.process_value(value, self) if attr_config.respond_to?(:process_value)
178
182
 
179
- value = attr_config.process_value(value, self) if attr_config.respond_to?(:process_value)
183
+ dep_attributes[attr_name] = value
184
+ end
180
185
 
181
- dep_attributes[attr_name] = value
186
+ # After getting the value, check if it's required and nil
187
+ if value.nil? && attr_config.required
188
+ raise ArgumentError, "Missing required attribute '#{attr_name}' for #{option_config.class_name}"
189
+ end
182
190
  end
191
+
183
192
  dep_attributes
184
193
  end
185
194
 
@@ -209,7 +218,6 @@ module GlueGun
209
218
  unless default_option.attributes.keys.include?(init_args.keys.first)
210
219
  raise ArgumentError, "#{default_option.class_name} does not respond to #{init_args.keys.first}"
211
220
  end
212
-
213
221
  end
214
222
  end
215
223
 
@@ -255,11 +263,12 @@ module GlueGun
255
263
  end
256
264
 
257
265
  class ConfigAttr
258
- attr_reader :name, :default, :source, :block
266
+ attr_reader :name, :default, :required, :source, :block
259
267
 
260
- def initialize(name, default: nil, source: nil, &block)
268
+ def initialize(name, default: nil, required: false, source: nil, &block)
261
269
  @name = name.to_sym
262
270
  @default = default
271
+ @required = required
263
272
  @source = source
264
273
  @block = block
265
274
  end
@@ -289,14 +298,14 @@ module GlueGun
289
298
  @is_only = false
290
299
  end
291
300
 
292
- # Support set_class and define_attr for single-option dependencies
301
+ # Support set_class and attribute for single-option dependencies
293
302
  def set_class(class_name)
294
303
  single_option.set_class(class_name)
295
304
  set_default_option_name(:default)
296
305
  end
297
306
 
298
- def attribute(name, default: nil, source: nil, &block)
299
- single_option.attribute(name, default: default, source: source, &block)
307
+ def attribute(name, default: nil, required: false, source: nil, &block)
308
+ single_option.attribute(name, default: default, required: required, source: source, &block)
300
309
  end
301
310
 
302
311
  def get_option(name)
@@ -363,8 +372,8 @@ module GlueGun
363
372
  end
364
373
  end
365
374
 
366
- def attribute(name, default: nil, source: nil, &block)
367
- attr = ConfigAttr.new(name, default: default, source: source, &block)
375
+ def attribute(name, default: nil, required: false, source: nil, &block)
376
+ attr = ConfigAttr.new(name, default: default, required: required, source: source, &block)
368
377
  @attributes[name.to_sym] = attr
369
378
  end
370
379
 
@@ -0,0 +1,28 @@
1
+ module GlueGun
2
+ module Types
3
+ class ArrayType < ActiveModel::Type::Value
4
+ def cast(value)
5
+ case value
6
+ when String
7
+ parse_string(value)
8
+ when Array
9
+ value
10
+ else
11
+ []
12
+ end
13
+ end
14
+
15
+ def serialize(value)
16
+ value.to_json
17
+ end
18
+
19
+ private
20
+
21
+ def parse_string(value)
22
+ JSON.parse(value)
23
+ rescue JSON::ParserError
24
+ []
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ module GlueGun
2
+ module Types
3
+ require_relative "glue_gun/types/hash_type"
4
+ ActiveModel::Type.register(:hash, GlueGun::Types::HashType)
5
+
6
+ require_relative "glue_gun/types/array_type"
7
+ ActiveModel::Type.register(:array, GlueGun::Types::ArrayType)
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GlueGun
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/glue_gun.rb CHANGED
@@ -2,8 +2,6 @@ require "active_model"
2
2
  require "active_support/concern"
3
3
 
4
4
  module GlueGun
5
- require_relative "glue_gun/types/hash_type"
6
- ActiveModel::Type.register(:hash, GlueGun::Types::HashType)
7
-
5
+ require_relative "glue_gun/types"
8
6
  require_relative "glue_gun/dsl"
9
7
  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.1
4
+ version: 0.1.3
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-03 00:00:00.000000000 Z
11
+ date: 2024-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -50,6 +50,20 @@ dependencies:
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
52
  version: '8'
53
+ - !ruby/object:Gem::Dependency
54
+ name: guard
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
53
67
  - !ruby/object:Gem::Dependency
54
68
  name: ostruct
55
69
  requirement: !ruby/object:Gem::Requirement
@@ -115,6 +129,8 @@ extra_rdoc_files: []
115
129
  files:
116
130
  - lib/glue_gun.rb
117
131
  - lib/glue_gun/dsl.rb
132
+ - lib/glue_gun/types.rb
133
+ - lib/glue_gun/types/array_type.rb
118
134
  - lib/glue_gun/types/hash_type.rb
119
135
  - lib/glue_gun/version.rb
120
136
  homepage: https://github.com/brettshollenberger/glue_gun