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 +4 -4
- data/lib/glue_gun/core_ext/hash_extensions.rb +1 -1
- data/lib/glue_gun/dsl.rb +6 -6
- data/lib/glue_gun/model.rb +46 -2
- data/lib/glue_gun/shared.rb +8 -0
- data/lib/glue_gun/version.rb +1 -1
- metadata +17 -3
- data/lib/glue_gun/my_model.rb +0 -73
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2fd5acfe5156d1581b51d68d5598ef181faea52ec3c46d730fef428f7faba15
|
4
|
+
data.tar.gz: c6185af99fd906dd841582b283b4559d8f1fbc4e3046086d7cf4a15ac5e1b1d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 587486d0afa4a7cde26ffc1a6d6a766123db4c09111b4f2e9d3b4f9f17651856a6a587b81832cb78fe93fa6d0f9f23cae88c145a61ac9905968508ffb6f8b7fc
|
7
|
+
data.tar.gz: 14d80f872b13ada859d605068799e8331136c36db88e9dd5ba81eadd93b34b7362918e08eb3e1a48848e8b42ea4477f597d43cc1b3633205dfdc2d2e84d66b5d
|
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]
|
data/lib/glue_gun/model.rb
CHANGED
@@ -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
|
-
|
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
|
|
data/lib/glue_gun/version.rb
CHANGED
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.
|
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-
|
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/
|
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
|
data/lib/glue_gun/my_model.rb
DELETED
@@ -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
|