glue_gun_dsl 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/glue_gun/dsl.rb +24 -5
- data/lib/glue_gun/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a2fbc67d781829a58d7b42eb3ef5e53a2b278460386c450241f9a39a3cf0886
|
4
|
+
data.tar.gz: aa40b26fd78038cd82d99dfe511ee87609d118e719cd23849f8bc9f190e13d44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: abc26b337ee2eebd874892406532936634cb78f366dd2f91f972eef3de010b69b79c8c616021c1a52d871349fbcdb8b372fe78008b4cc4d1440fe732d432cc73
|
7
|
+
data.tar.gz: 3685c64f5fad1a4b19dab651762e47401a95d628548e5095910315d2a77a60b22354144100a838a01bae7cee6cd0d1850410e761e10f285e2f4be1c80ce5194e
|
data/lib/glue_gun/dsl.rb
CHANGED
@@ -7,6 +7,7 @@ module GlueGun
|
|
7
7
|
include ActiveModel::Attributes
|
8
8
|
include ActiveModel::Validations
|
9
9
|
include ActiveModel::AttributeAssignment
|
10
|
+
include ActiveModel::Dirty
|
10
11
|
|
11
12
|
class_attribute :attribute_definitions, instance_writer: false, default: {}
|
12
13
|
class_attribute :dependency_definitions, instance_writer: false, default: {}
|
@@ -24,16 +25,22 @@ module GlueGun
|
|
24
25
|
class << self
|
25
26
|
prepend ClassMethods
|
26
27
|
end
|
28
|
+
|
29
|
+
# Overriding ActiveModel.assign_attributes to ensure change propagation to dependenices
|
30
|
+
def assign_attributes(new_attributes)
|
31
|
+
super(new_attributes)
|
32
|
+
propagate_changes if initialized?
|
33
|
+
end
|
27
34
|
end
|
28
35
|
|
29
36
|
module Initialization
|
30
|
-
def initialize(
|
31
|
-
|
37
|
+
def initialize(attrs = {})
|
38
|
+
attrs = attrs.symbolize_keys
|
32
39
|
# Separate dependency configurations from normal attributes
|
33
40
|
dependency_attributes = {}
|
34
41
|
normal_attributes = {}
|
35
42
|
|
36
|
-
|
43
|
+
attrs.each do |key, value|
|
37
44
|
if self.class.dependency_definitions.key?(key)
|
38
45
|
dependency_attributes[key] = value
|
39
46
|
else
|
@@ -58,11 +65,13 @@ module GlueGun
|
|
58
65
|
|
59
66
|
module ClassMethods
|
60
67
|
# Override the attribute method to define custom setters
|
61
|
-
def attribute(name, type =
|
62
|
-
# Call the original attribute method from ActiveModel::Attributes
|
68
|
+
def attribute(name, type = nil, **options)
|
63
69
|
super(name, type, **options)
|
64
70
|
attribute_definitions[name.to_sym] = { type: type, options: options }
|
65
71
|
|
72
|
+
# Define dirty tracking for the attribute
|
73
|
+
define_attribute_methods name
|
74
|
+
|
66
75
|
attribute_methods_module = const_get(:AttributeMethods)
|
67
76
|
|
68
77
|
attribute_methods_module.class_eval do
|
@@ -234,6 +243,16 @@ module GlueGun
|
|
234
243
|
dependency_instance
|
235
244
|
end
|
236
245
|
|
246
|
+
def propagate_changes
|
247
|
+
changed_attributes.each do |attr_name, _old_value|
|
248
|
+
new_value = read_attribute(attr_name)
|
249
|
+
propagate_attribute_change(attr_name, new_value)
|
250
|
+
end
|
251
|
+
|
252
|
+
# Clear the changes after propagation
|
253
|
+
changes_applied
|
254
|
+
end
|
255
|
+
|
237
256
|
def propagate_attribute_change(attr_name, value)
|
238
257
|
self.class.dependency_definitions.each do |component_type, _dependency_builder|
|
239
258
|
dependency_instance = send(component_type)
|
data/lib/glue_gun/version.rb
CHANGED