glue_gun_dsl 0.1.4 → 0.1.6

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: 87d462fd4e0019174b6d06d77c582e67dfd16ddb64e65a3ccb7cf176bdd18f22
4
- data.tar.gz: f067c0c223ad8a3fafc76fe74a9e42b4abdf6978b219f32d58108ef8bc278e5e
3
+ metadata.gz: 1a2fbc67d781829a58d7b42eb3ef5e53a2b278460386c450241f9a39a3cf0886
4
+ data.tar.gz: aa40b26fd78038cd82d99dfe511ee87609d118e719cd23849f8bc9f190e13d44
5
5
  SHA512:
6
- metadata.gz: 6433593537948d2bf7cda82c8f0d0cdc0b2883c8584f4e0447ccedf32353b3f57c400ce904d97cedf34b1929d0fdcb044da80390b1845d7536292ab390efc80d
7
- data.tar.gz: 443140a736857306a802c2058b657b793b4a102ec8d06151853a2d18c0fbbccff40c67b24743321f2b152dd9895c2b155c34ba483112e2e03927814c3d50ed1b
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(attributes = {})
31
- attributes = attributes.symbolize_keys
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
- attributes.each do |key, value|
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 = :string, **options)
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)
@@ -0,0 +1,26 @@
1
+ module GlueGun
2
+ module Types
3
+ class DateTimeType < ActiveModel::Type::Value
4
+ def cast(value)
5
+ case value
6
+ when String
7
+ parse_string(value)
8
+ when Date, DateTime
9
+ value
10
+ end
11
+ end
12
+
13
+ def serialize(value)
14
+ value.to_s
15
+ end
16
+
17
+ private
18
+
19
+ def parse_string(value)
20
+ DateTime.parse(value).utc
21
+ rescue ArgumentError
22
+ nil
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ module GlueGun
2
+ module Types
3
+ class DateType < ActiveModel::Type::Value
4
+ def cast(value)
5
+ case value
6
+ when String
7
+ parse_string(value).to_date
8
+ when Date, DateTime
9
+ value.to_date
10
+ end
11
+ end
12
+
13
+ def serialize(value)
14
+ value.to_s
15
+ end
16
+
17
+ private
18
+
19
+ def parse_string(value)
20
+ DateTime.parse(value).utc
21
+ rescue ArgumentError
22
+ nil
23
+ end
24
+ end
25
+ end
26
+ end
@@ -5,5 +5,11 @@ module GlueGun
5
5
 
6
6
  require_relative "types/array_type"
7
7
  ActiveModel::Type.register(:array, GlueGun::Types::ArrayType)
8
+
9
+ require_relative "types/date_type"
10
+ ActiveModel::Type.register(:date, GlueGun::Types::DateType)
11
+
12
+ require_relative "types/date_time_type"
13
+ ActiveModel::Type.register(:datetime, GlueGun::Types::DateTimeType)
8
14
  end
9
15
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GlueGun
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.6"
5
5
  end
data/lib/glue_gun.rb CHANGED
@@ -2,6 +2,7 @@ require "active_model"
2
2
  require "active_support/concern"
3
3
 
4
4
  module GlueGun
5
+ require_relative "glue_gun/version"
5
6
  require_relative "glue_gun/types"
6
7
  require_relative "glue_gun/dsl"
7
8
  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.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brett Shollenberger
@@ -131,6 +131,8 @@ files:
131
131
  - lib/glue_gun/dsl.rb
132
132
  - lib/glue_gun/types.rb
133
133
  - lib/glue_gun/types/array_type.rb
134
+ - lib/glue_gun/types/date_time_type.rb
135
+ - lib/glue_gun/types/date_type.rb
134
136
  - lib/glue_gun/types/hash_type.rb
135
137
  - lib/glue_gun/version.rb
136
138
  homepage: https://github.com/brettshollenberger/glue_gun