glue_gun_dsl 0.1.22 → 0.1.23

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: e38dfcee3bde86f31b597d6dccf5138fb7200050a05904a055b7d8f201192ab0
4
+ data.tar.gz: 9b031f91fd59f57d8a10edef069d0ec656e04b8440d55af1354dda0cbfa124e4
5
5
  SHA512:
6
- metadata.gz: 425a1e574b7a54826b2d2881a38d5efc190d14c46e7f5c85cd9b779c17182ad01a1427a746a1dce10780d4243bf575e9ac8e3a467497c41cd1a1e74ac8466be7
7
- data.tar.gz: e819aacabb11e0c6174f167cd9fee3eaf2fdd882797631b0c383abbbf9c0bb69431c28cde2bfc024cd391d6c4e526fb8e5c1fa1862396d28d3d3fc889ae737f4
6
+ metadata.gz: b25d5acc5cc955c9c3b20147c6bbf81c2911860f7e7f32b48b4f21273bf7c1f992f05c6b21ed2e6353b3ace8ac39023f87dcbd100fc9041731bea3a62b5bdf2b
7
+ data.tar.gz: 69736bb98acb9e1365879c0f68f7c401cc6b675e4c7c21af9fee87c0ea074dc76fc103413a386b5b76b1c8e44534f2c3e2f163ed2242389e14dc80fc7c02e923
@@ -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
 
@@ -32,7 +35,12 @@ module GlueGun
32
35
 
33
36
  record = where(db_attributes).first_or_initialize(attributes)
34
37
 
35
- record.save! if record.new_record?
38
+ if record.new_record?
39
+ record.save!
40
+ else
41
+ record.send(:build_service_object, attributes)
42
+ end
43
+ yield record if block_given?
36
44
 
37
45
  record
38
46
  end
@@ -44,7 +52,12 @@ module GlueGun
44
52
 
45
53
  record = where(db_attributes).first_or_initialize(attributes)
46
54
 
47
- record.save if record.new_record?
55
+ if record.new_record?
56
+ record.save
57
+ else
58
+ record.send(:build_service_object, attributes)
59
+ end
60
+ yield record if block_given?
48
61
 
49
62
  record
50
63
  end
@@ -59,19 +72,29 @@ module GlueGun
59
72
  end
60
73
 
61
74
  def initialize(attributes = {})
75
+ attributes[:root_dir] = detect_root_dir
62
76
  attributes = attributes.deep_symbolize_keys
63
77
  db_attributes = self.class.extract_db_attributes(attributes)
64
78
  super(db_attributes)
65
- self.class.send(:attr_reader, service_attribute_name)
66
79
  build_service_object(attributes)
67
80
  end
68
81
 
69
82
  private
70
83
 
71
84
  def build_service_object(attributes)
85
+ self.class.send(:attr_reader, service_attribute_name)
72
86
  service_class = resolve_service_class(attributes)
87
+ raise "Unable to find service class for #{self.class} given #{attributes}" unless service_class.present?
88
+
73
89
  service_attributes = extract_service_attributes(attributes, service_class)
74
- service_instance = service_class.new(service_attributes)
90
+ begin
91
+ service_instance = service_class.new(service_attributes)
92
+ rescue StandardError => e
93
+ ap %(Error building service object #{service_class}:)
94
+ ap e.message
95
+ ap e.backtrace
96
+ raise e
97
+ end
75
98
  instance_variable_set("@#{service_attribute_name}", service_instance)
76
99
  end
77
100
 
@@ -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.23"
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.23
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