glue_gun_dsl 0.1.22 → 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: d8f609405b59c865b180e5e07a5a74ebbf4867cea468444983820202b17fbcb7
4
- data.tar.gz: b71c914a42fe7175f41b7faa674c30c58d03fcc2c1f54c281bcbf0b8876b1800
3
+ metadata.gz: e2fd5acfe5156d1581b51d68d5598ef181faea52ec3c46d730fef428f7faba15
4
+ data.tar.gz: c6185af99fd906dd841582b283b4559d8f1fbc4e3046086d7cf4a15ac5e1b1d3
5
5
  SHA512:
6
- metadata.gz: 425a1e574b7a54826b2d2881a38d5efc190d14c46e7f5c85cd9b779c17182ad01a1427a746a1dce10780d4243bf575e9ac8e3a467497c41cd1a1e74ac8466be7
7
- data.tar.gz: e819aacabb11e0c6174f167cd9fee3eaf2fdd882797631b0c383abbbf9c0bb69431c28cde2bfc024cd391d6c4e526fb8e5c1fa1862396d28d3d3fc889ae737f4
6
+ metadata.gz: 587486d0afa4a7cde26ffc1a6d6a766123db4c09111b4f2e9d3b4f9f17651856a6a587b81832cb78fe93fa6d0f9f23cae88c145a61ac9905968508ffb6f8b7fc
7
+ data.tar.gz: 14d80f872b13ada859d605068799e8331136c36db88e9dd5ba81eadd93b34b7362918e08eb3e1a48848e8b42ea4477f597d43cc1b3633205dfdc2d2e84d66b5d
@@ -12,7 +12,7 @@ module GlueGun
12
12
  value
13
13
  end
14
14
 
15
- result[key] = compacted unless compacted.blank?
15
+ result[key] = compacted unless compacted.is_a?(String) && compacted.blank?
16
16
  end
17
17
  end
18
18
  end
data/lib/glue_gun/dsl.rb CHANGED
@@ -1,8 +1,11 @@
1
+ require_relative "shared"
1
2
  module GlueGun
2
3
  module DSL
3
4
  extend ActiveSupport::Concern
4
5
 
5
6
  included do
7
+ include GlueGun::Shared
8
+
6
9
  unless ancestors.include?(ActiveRecord::Base)
7
10
  include ActiveModel::Model
8
11
  include ActiveModel::Attributes
@@ -77,6 +80,8 @@ module GlueGun
77
80
  end
78
81
 
79
82
  module ClassMethods
83
+ extend GlueGun::Shared
84
+
80
85
  DEFAULT_TYPE = if ActiveModel.version >= Gem::Version.new("7")
81
86
  nil
82
87
  elsif ActiveModel.version >= Gem::Version.new("5")
@@ -156,7 +161,7 @@ module GlueGun
156
161
  end
157
162
 
158
163
  def detect_root_dir
159
- base_path = Module.const_source_location(name)&.first || ""
164
+ base_path = Module.const_source_location(self.class.name)&.first || ""
160
165
  File.dirname(base_path)
161
166
  end
162
167
  end
@@ -165,11 +170,6 @@ module GlueGun
165
170
  @initialized == true
166
171
  end
167
172
 
168
- def detect_root_dir
169
- base_path = Module.const_source_location(self.class.name)&.first || ""
170
- File.dirname(base_path)
171
- end
172
-
173
173
  def initialize_dependencies(attributes)
174
174
  self.class.dependency_definitions.each do |component_type, definition|
175
175
  value = attributes[component_type] || self.class.hardcoded_dependencies[component_type]
@@ -1,8 +1,11 @@
1
+ require_relative "shared"
1
2
  module GlueGun
2
3
  module Model
3
4
  extend ActiveSupport::Concern
4
5
 
5
6
  included do
7
+ include GlueGun::Shared
8
+
6
9
  before_save :serialize_service_object
7
10
  after_find :deserialize_service_object
8
11
 
@@ -12,6 +15,19 @@ module GlueGun
12
15
 
13
16
  # Set default service attribute name based on the class name
14
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
15
31
  end
16
32
 
17
33
  class_methods do
@@ -33,6 +49,7 @@ module GlueGun
33
49
  record = where(db_attributes).first_or_initialize(attributes)
34
50
 
35
51
  record.save! if record.new_record?
52
+ yield record if block_given?
36
53
 
37
54
  record
38
55
  end
@@ -45,6 +62,7 @@ module GlueGun
45
62
  record = where(db_attributes).first_or_initialize(attributes)
46
63
 
47
64
  record.save if record.new_record?
65
+ yield record if block_given?
48
66
 
49
67
  record
50
68
  end
@@ -59,19 +77,45 @@ module GlueGun
59
77
  end
60
78
 
61
79
  def initialize(attributes = {})
80
+ attributes[:root_dir] = detect_root_dir
62
81
  attributes = attributes.deep_symbolize_keys
63
82
  db_attributes = self.class.extract_db_attributes(attributes)
64
83
  super(db_attributes)
65
- self.class.send(:attr_reader, service_attribute_name)
66
84
  build_service_object(attributes)
67
85
  end
68
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
+
69
103
  private
70
104
 
71
105
  def build_service_object(attributes)
106
+ self.class.send(:attr_reader, service_attribute_name)
72
107
  service_class = resolve_service_class(attributes)
108
+ raise "Unable to find service class for #{self.class} given #{attributes}" unless service_class.present?
109
+
73
110
  service_attributes = extract_service_attributes(attributes, service_class)
74
- service_instance = service_class.new(service_attributes)
111
+ begin
112
+ service_instance = service_class.new(service_attributes)
113
+ rescue StandardError => e
114
+ ap %(Error building service object #{service_class}:)
115
+ ap e.message
116
+ ap e.backtrace
117
+ raise e
118
+ end
75
119
  instance_variable_set("@#{service_attribute_name}", service_instance)
76
120
  end
77
121
 
@@ -0,0 +1,8 @@
1
+ module GlueGun
2
+ module Shared
3
+ def detect_root_dir
4
+ base_path = Module.const_source_location(self.class.name)&.first || ""
5
+ File.dirname(base_path)
6
+ end
7
+ end
8
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GlueGun
4
- VERSION = "0.1.22"
4
+ VERSION = "0.1.24"
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.22
4
+ version: 0.1.24
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-22 00:00:00.000000000 Z
11
+ date: 2024-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -70,6 +70,20 @@ dependencies:
70
70
  - - "<"
71
71
  - !ruby/object:Gem::Version
72
72
  version: '8'
73
+ - !ruby/object:Gem::Dependency
74
+ name: awesome_print
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ type: :runtime
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
73
87
  - !ruby/object:Gem::Dependency
74
88
  name: appraisal
75
89
  requirement: !ruby/object:Gem::Requirement
@@ -194,7 +208,7 @@ files:
194
208
  - lib/glue_gun/core_ext/hash_extensions.rb
195
209
  - lib/glue_gun/dsl.rb
196
210
  - lib/glue_gun/model.rb
197
- - lib/glue_gun/my_model.rb
211
+ - lib/glue_gun/shared.rb
198
212
  - lib/glue_gun/types.rb
199
213
  - lib/glue_gun/types/array_type.rb
200
214
  - lib/glue_gun/types/date_time_type.rb
@@ -1,73 +0,0 @@
1
- module EasyML
2
- class Dataset < ActiveRecord::Base
3
- before_save :serialize
4
- after_find :deserialize
5
-
6
- validates :name, presence: true
7
- belongs_to :datasource,
8
- foreign_key: :datasource_id,
9
- class_name: "EasyML::Datasource"
10
-
11
- def initialize(options = {})
12
- super(EasyML::DbOptions.parse(options, self))
13
- build_dataset_service(options)
14
- end
15
-
16
- attr_accessor :dataset_service
17
-
18
- private
19
-
20
- def build_dataset_service(options)
21
- options.deep_symbolize_keys!
22
- service_klass = EasyML::Data::Dataset
23
-
24
- allowed_attr_names = service_klass.dependency_definitions.keys.concat(
25
- service_klass.attribute_definitions.keys
26
- )
27
-
28
- allowed_attrs = options.slice(*allowed_attr_names)
29
- @dataset_service = service_klass.new(allowed_attrs)
30
- define_dataset_service_delegators(allowed_attr_names)
31
- end
32
-
33
- def define_dataset_service_delegators(attr_names)
34
- allowed_names(attr_names).each do |attr_name|
35
- define_singleton_method(attr_name) do
36
- dataset_service.send(attr_name)
37
- end
38
- end
39
- end
40
-
41
- def allowed_names(names)
42
- assoc_names = self.class.reflect_on_all_associations.map(&:name)
43
- [names.map(&:to_sym) - assoc_names.map(&:to_sym)].flatten
44
- end
45
-
46
- def serialize
47
- attrs = dataset_service.attributes
48
- deps = allowed_names(dataset_service.dependency_definitions.keys).inject({}) do |hash, dep|
49
- hash.tap do
50
- this_dep = dataset_service.send(dep)
51
- next unless this_dep.present?
52
-
53
- opts = dataset_service.dependency_definitions[dep].option_configs
54
- selected_option = opts.detect do |_k, v|
55
- this_dep.class == v.class_name
56
- end.first
57
- hash[dep] = {
58
- selected_option => dataset_service.send(dep).attributes
59
- }
60
- end
61
- end
62
- json = attrs.merge(deps).deep_symbolize_keys.deep_compact
63
- write_attribute(:configuration, json.to_json)
64
- end
65
-
66
- def deserialize
67
- options = JSON.parse(read_attribute(:configuration))
68
- options.deep_symbolize_keys!
69
-
70
- build_dataset_service(options)
71
- end
72
- end
73
- end