glue_gun_dsl 0.1.23 → 0.1.24

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: e38dfcee3bde86f31b597d6dccf5138fb7200050a05904a055b7d8f201192ab0
4
- data.tar.gz: 9b031f91fd59f57d8a10edef069d0ec656e04b8440d55af1354dda0cbfa124e4
3
+ metadata.gz: e2fd5acfe5156d1581b51d68d5598ef181faea52ec3c46d730fef428f7faba15
4
+ data.tar.gz: c6185af99fd906dd841582b283b4559d8f1fbc4e3046086d7cf4a15ac5e1b1d3
5
5
  SHA512:
6
- metadata.gz: b25d5acc5cc955c9c3b20147c6bbf81c2911860f7e7f32b48b4f21273bf7c1f992f05c6b21ed2e6353b3ace8ac39023f87dcbd100fc9041731bea3a62b5bdf2b
7
- data.tar.gz: 69736bb98acb9e1365879c0f68f7c401cc6b675e4c7c21af9fee87c0ea074dc76fc103413a386b5b76b1c8e44534f2c3e2f163ed2242389e14dc80fc7c02e923
6
+ metadata.gz: 587486d0afa4a7cde26ffc1a6d6a766123db4c09111b4f2e9d3b4f9f17651856a6a587b81832cb78fe93fa6d0f9f23cae88c145a61ac9905968508ffb6f8b7fc
7
+ data.tar.gz: 14d80f872b13ada859d605068799e8331136c36db88e9dd5ba81eadd93b34b7362918e08eb3e1a48848e8b42ea4477f597d43cc1b3633205dfdc2d2e84d66b5d
@@ -15,6 +15,19 @@ module GlueGun
15
15
 
16
16
  # Set default service attribute name based on the class name
17
17
  self.service_attribute_name = "#{name.demodulize.underscore}_service".to_sym
18
+
19
+ def assign_attributes(attributes)
20
+ return if attributes.blank?
21
+
22
+ attributes = attributes.deep_symbolize_keys
23
+ db_attributes = self.class.extract_db_attributes(attributes)
24
+
25
+ # Assign database attributes
26
+ super(db_attributes)
27
+
28
+ assign_service_attributes(attributes)
29
+ end
30
+ alias_method :attributes=, :assign_attributes
18
31
  end
19
32
 
20
33
  class_methods do
@@ -35,11 +48,7 @@ module GlueGun
35
48
 
36
49
  record = where(db_attributes).first_or_initialize(attributes)
37
50
 
38
- if record.new_record?
39
- record.save!
40
- else
41
- record.send(:build_service_object, attributes)
42
- end
51
+ record.save! if record.new_record?
43
52
  yield record if block_given?
44
53
 
45
54
  record
@@ -52,11 +61,7 @@ module GlueGun
52
61
 
53
62
  record = where(db_attributes).first_or_initialize(attributes)
54
63
 
55
- if record.new_record?
56
- record.save
57
- else
58
- record.send(:build_service_object, attributes)
59
- end
64
+ record.save if record.new_record?
60
65
  yield record if block_given?
61
66
 
62
67
  record
@@ -79,6 +84,22 @@ module GlueGun
79
84
  build_service_object(attributes)
80
85
  end
81
86
 
87
+ def assign_service_attributes(attributes)
88
+ return if attributes.blank?
89
+
90
+ service_object = instance_variable_get("@#{service_attribute_name}")
91
+ return unless service_object.present?
92
+
93
+ extract_service_attributes(attributes, service_object.class).each do |attr_name, value|
94
+ unless service_object.respond_to?("#{attr_name}=")
95
+ raise NoMethodError, "Undefined attribute #{attr_name} for #{service_object.class.name}"
96
+ end
97
+
98
+ service_object.send("#{attr_name}=", value)
99
+ attribute_will_change!(attr_name.to_s)
100
+ end
101
+ end
102
+
82
103
  private
83
104
 
84
105
  def build_service_object(attributes)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GlueGun
4
- VERSION = "0.1.23"
4
+ VERSION = "0.1.24"
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.23
4
+ version: 0.1.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brett Shollenberger