glue_gun_dsl 0.1.10 → 0.1.12

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: d6e4ef257ecdb7017d0c554a6df1e4c24fcee152a0888973cd52fab9d9b4b21a
4
- data.tar.gz: bd019c542b46e12188b759d8c7322bea99bcb016c0940ac2a67124a33fca31d8
3
+ metadata.gz: a663641fdcec4866446e9d72583c2e3be4e038604cb22466ee094e81fc17bb79
4
+ data.tar.gz: 7cc36ed0ad6903f243a129fddee1426995364f703505dad2812322e890a14322
5
5
  SHA512:
6
- metadata.gz: 3f3522c171606ed23822d792e5ebc1895ac03280a9fa4bde7c9bf79daed22496c14b5d0b9a03d5063105d1611b2f11161c3c5ed74919b9c134e2f32d35b03a9a
7
- data.tar.gz: a170db2ee61ba4c208148bcc8fb46e428bf1b04b8142ffa110f857418d7a762e759b6d13fe51333fcd829c3876d98128b0ad86d48dd9db4cf24ed027a47ba2bf
6
+ metadata.gz: 10571a6ef7a315f566f5d49140aa0e4743433707ab5c8363d862f7fd318d775e32b6d636fb6698900c0d39706a0da2e77fb35bb3812f919aca4bd1d350df383e
7
+ data.tar.gz: 8e0754d69e1ae48e7d0c8ca396d8afd747967c51bbaced995af0b82f203b431a2eeaf8c47784a08273ce8b6596acb330964063a31f8cdaf414b8b1a1f35a4e66
data/lib/glue_gun/dsl.rb CHANGED
@@ -3,11 +3,14 @@ module GlueGun
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- include ActiveModel::Model
7
- include ActiveModel::Attributes
8
- include ActiveModel::Validations
9
- include ActiveModel::AttributeAssignment
10
- include ActiveModel::Dirty
6
+ unless ancestors.include?(ActiveRecord::Base)
7
+ include ActiveModel::Model
8
+ include ActiveModel::Attributes
9
+ include ActiveModel::Validations
10
+ include ActiveModel::AttributeAssignment
11
+ include ActiveModel::Dirty
12
+ include ActiveRecord::Callbacks
13
+ end
11
14
 
12
15
  class_attribute :attribute_definitions, instance_writer: false, default: {}
13
16
  class_attribute :dependency_definitions, instance_writer: false, default: {}
@@ -18,7 +21,6 @@ module GlueGun
18
21
  const_set(:AttributeMethods, attribute_methods_module)
19
22
  prepend attribute_methods_module
20
23
 
21
- # Prepend Initialization module to override initialize
22
24
  prepend Initialization
23
25
 
24
26
  # Prepend ClassMethods into the singleton class of the including class
@@ -31,10 +33,21 @@ module GlueGun
31
33
  super(new_attributes)
32
34
  propagate_changes if initialized?
33
35
  end
36
+
37
+ if ancestors.include?(ActiveRecord::Base)
38
+ columns.each do |column|
39
+ # Skip if already defined via GlueGun::DSL's attribute method
40
+ next if attribute_definitions.key?(column.name.to_sym)
41
+
42
+ # Define a ConfigAttr for each ActiveRecord column
43
+ attribute(column.name.to_sym, column.type)
44
+ end
45
+ end
34
46
  end
35
47
 
36
48
  module Initialization
37
49
  def initialize(attrs = {})
50
+ attrs ||= {}
38
51
  attrs = attrs.symbolize_keys
39
52
  # Separate dependency configurations from normal attributes
40
53
  dependency_attributes = {}
@@ -56,9 +69,6 @@ module GlueGun
56
69
  # Initialize dependencies after attributes have been set
57
70
  initialize_dependencies(dependency_attributes)
58
71
 
59
- # Validate the main object's attributes
60
- validate!
61
-
62
72
  @initialized = true
63
73
  end
64
74
  end
@@ -75,7 +85,7 @@ module GlueGun
75
85
  attribute_definitions[name.to_sym] = { type: type, options: options }
76
86
 
77
87
  # Define dirty tracking for the attribute
78
- define_attribute_methods name
88
+ define_attribute_methods name unless ancestors.include?(ActiveRecord::Base)
79
89
 
80
90
  attribute_methods_module = const_get(:AttributeMethods)
81
91
 
@@ -106,6 +116,13 @@ module GlueGun
106
116
  instance_variable_get("@#{component_type}") ||
107
117
  instance_variable_set("@#{component_type}", initialize_dependency(component_type))
108
118
  end
119
+
120
+ attribute_methods_module = const_get(:AttributeMethods)
121
+ attribute_methods_module.class_eval do
122
+ define_method "#{component_type}=" do |init_args|
123
+ instance_variable_set("@#{component_type}", initialize_dependency(component_type, init_args))
124
+ end
125
+ end
109
126
  end
110
127
 
111
128
  def inherited(subclass)
@@ -167,6 +184,11 @@ module GlueGun
167
184
  # Build dependency attributes, including sourcing from parent
168
185
  dep_attributes = build_dependency_attributes(option_config, dep_attributes)
169
186
 
187
+ if dep_attributes.key?(:id)
188
+ raise ArgumentError,
189
+ "cannot bind attribute 'id' between #{self.class.name} and #{option_config.class_name}. ID is reserved for primary keys in Ruby on Rails"
190
+ end
191
+
170
192
  dependency_instance = instantiate_dependency(option_config, dep_attributes)
171
193
 
172
194
  # Keep track of dependencies for attribute binding
@@ -194,11 +216,6 @@ module GlueGun
194
216
  value = attr_config.process_value(value, self) if attr_config.respond_to?(:process_value)
195
217
  dep_attributes[attr_name] = value
196
218
  end
197
-
198
- # After getting the value, check if it's required and nil
199
- if value.nil? && attr_config.required
200
- raise ArgumentError, "Missing required attribute '#{attr_name}' for #{option_config.class_name}"
201
- end
202
219
  end
203
220
 
204
221
  dep_attributes
@@ -242,7 +259,7 @@ module GlueGun
242
259
  def instantiate_dependency(option_config, dep_attributes)
243
260
  dependency_class = option_config.class_name
244
261
  dependency_instance = dependency_class.new(dep_attributes)
245
- dependency_instance.validate! if dependency_instance.respond_to?(:validate!)
262
+ dependency_instance.validate! if false # dependency_instance.respond_to?(:validate!)
246
263
  dependency_instance
247
264
  end
248
265
 
@@ -288,6 +305,25 @@ module GlueGun
288
305
  @dependencies ||= {}
289
306
  end
290
307
 
308
+ def validate_dependencies
309
+ errors.clear
310
+ self.class.dependency_definitions.keys.each do |component_type|
311
+ dependency = send(component_type)
312
+
313
+ # Only validate if the dependency responds to `valid?`
314
+ next unless dependency.respond_to?(:valid?) && !dependency.valid?
315
+
316
+ dependency.errors.each do |error|
317
+ if error.is_a?(ActiveModel::Error)
318
+ attribute = error.attribute
319
+ message = error.message
320
+ end
321
+ errors.add("#{component_type}.#{attribute}", message)
322
+ end
323
+ end
324
+ errors.none?
325
+ end
326
+
291
327
  class ConfigAttr
292
328
  attr_reader :name, :default, :required, :source, :block
293
329
 
@@ -330,8 +366,8 @@ module GlueGun
330
366
  set_default_option_name(:default)
331
367
  end
332
368
 
333
- def attribute(name, default: nil, required: false, source: nil, &block)
334
- single_option.attribute(name, default: default, required: required, source: source, &block)
369
+ def bind_attribute(name, default: nil, required: false, source: nil, &block)
370
+ single_option.bind_attribute(name, default: default, required: required, source: source, &block)
335
371
  end
336
372
 
337
373
  def get_option(name)
@@ -398,7 +434,7 @@ module GlueGun
398
434
  end
399
435
  end
400
436
 
401
- def attribute(name, default: nil, required: false, source: nil, &block)
437
+ def bind_attribute(name, default: nil, required: false, source: nil, &block)
402
438
  attr = ConfigAttr.new(name, default: default, required: required, source: source, &block)
403
439
  @attributes[name.to_sym] = attr
404
440
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GlueGun
4
- VERSION = "0.1.10"
4
+ VERSION = "0.1.12"
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.10
4
+ version: 0.1.12
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-05 00:00:00.000000000 Z
11
+ date: 2024-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -30,6 +30,26 @@ dependencies:
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '8'
33
+ - !ruby/object:Gem::Dependency
34
+ name: activerecord
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '6'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '8'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '6'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '8'
33
53
  - !ruby/object:Gem::Dependency
34
54
  name: activesupport
35
55
  requirement: !ruby/object:Gem::Requirement
@@ -64,6 +84,20 @@ dependencies:
64
84
  - - ">="
65
85
  - !ruby/object:Gem::Version
66
86
  version: '0'
87
+ - !ruby/object:Gem::Dependency
88
+ name: combustion
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '1.3'
94
+ type: :development
95
+ prerelease: false
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - "~>"
99
+ - !ruby/object:Gem::Version
100
+ version: '1.3'
67
101
  - !ruby/object:Gem::Dependency
68
102
  name: guard
69
103
  requirement: !ruby/object:Gem::Requirement
@@ -134,6 +168,20 @@ dependencies:
134
168
  - - "~>"
135
169
  - !ruby/object:Gem::Version
136
170
  version: '3.0'
171
+ - !ruby/object:Gem::Dependency
172
+ name: sqlite3
173
+ requirement: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - "~>"
176
+ - !ruby/object:Gem::Version
177
+ version: '1.4'
178
+ type: :development
179
+ prerelease: false
180
+ version_requirements: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - "~>"
183
+ - !ruby/object:Gem::Version
184
+ version: '1.4'
137
185
  description: GlueGun makes dependency injection and hydration a first-order concern
138
186
  email:
139
187
  - brett.shollenberger@gmail.com