glue_gun_dsl 0.1.20 → 0.1.22
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 +22 -0
- data/lib/glue_gun/core_ext.rb +5 -0
- data/lib/glue_gun/dsl.rb +8 -21
- data/lib/glue_gun/model.rb +196 -0
- data/lib/glue_gun/my_model.rb +73 -0
- data/lib/glue_gun/version.rb +1 -1
- data/lib/glue_gun.rb +2 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8f609405b59c865b180e5e07a5a74ebbf4867cea468444983820202b17fbcb7
|
4
|
+
data.tar.gz: b71c914a42fe7175f41b7faa674c30c58d03fcc2c1f54c281bcbf0b8876b1800
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 425a1e574b7a54826b2d2881a38d5efc190d14c46e7f5c85cd9b779c17182ad01a1427a746a1dce10780d4243bf575e9ac8e3a467497c41cd1a1e74ac8466be7
|
7
|
+
data.tar.gz: e819aacabb11e0c6174f167cd9fee3eaf2fdd882797631b0c383abbbf9c0bb69431c28cde2bfc024cd391d6c4e526fb8e5c1fa1862396d28d3d3fc889ae737f4
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module GlueGun
|
2
|
+
module HashExtensions
|
3
|
+
def deep_compact
|
4
|
+
each_with_object({}) do |(key, value), result|
|
5
|
+
next if value.nil?
|
6
|
+
|
7
|
+
compacted = if value.is_a?(Hash)
|
8
|
+
value.deep_compact
|
9
|
+
elsif value.is_a?(Array)
|
10
|
+
value.map { |v| v.is_a?(Hash) ? v.deep_compact : v }.compact
|
11
|
+
else
|
12
|
+
value
|
13
|
+
end
|
14
|
+
|
15
|
+
result[key] = compacted unless compacted.blank?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Extend Hash class with our custom method
|
22
|
+
Hash.include GlueGun::HashExtensions
|
data/lib/glue_gun/dsl.rb
CHANGED
@@ -432,14 +432,19 @@ module GlueGun
|
|
432
432
|
end
|
433
433
|
end
|
434
434
|
|
435
|
-
dep_attributes
|
435
|
+
dep_attributes.deep_compact
|
436
436
|
end
|
437
437
|
|
438
438
|
def determine_option_name(init_args, instance)
|
439
439
|
option_name = nil
|
440
440
|
|
441
|
-
|
442
|
-
|
441
|
+
user_specified_option = init_args.is_a?(Hash) && init_args.keys.size == 1
|
442
|
+
user_specified_valid_option = user_specified_option && option_configs.key?(init_args.keys.first)
|
443
|
+
|
444
|
+
if user_specified_valid_option
|
445
|
+
option_name = init_args.keys.first
|
446
|
+
init_args = init_args[option_name] # Extract the inner value
|
447
|
+
elsif when_block.present?
|
443
448
|
result = instance.instance_exec(init_args, &when_block)
|
444
449
|
if result.is_a?(Hash) && result[:option]
|
445
450
|
option_name = result[:option]
|
@@ -448,24 +453,6 @@ module GlueGun
|
|
448
453
|
end
|
449
454
|
end
|
450
455
|
|
451
|
-
# Detect option from user input
|
452
|
-
if option_name.nil? && (init_args.is_a?(Hash) && init_args.keys.size == 1)
|
453
|
-
if option_configs.key?(init_args.keys.first)
|
454
|
-
option_name = init_args.keys.first
|
455
|
-
init_args = init_args[option_name] # Extract the inner value
|
456
|
-
else
|
457
|
-
default_option = get_option(default_option_name)
|
458
|
-
unless default_option.only?
|
459
|
-
raise ArgumentError,
|
460
|
-
"Unknown #{component_type} option: #{init_args.keys.first}."
|
461
|
-
end
|
462
|
-
unless default_option.attributes.keys.include?(init_args.keys.first)
|
463
|
-
raise ArgumentError, "#{default_option.class_name} does not respond to #{init_args.keys.first}"
|
464
|
-
end
|
465
|
-
end
|
466
|
-
end
|
467
|
-
|
468
|
-
# Use default option if none determined
|
469
456
|
option_name ||= default_option_name
|
470
457
|
|
471
458
|
[option_name, init_args]
|
@@ -0,0 +1,196 @@
|
|
1
|
+
module GlueGun
|
2
|
+
module Model
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
before_save :serialize_service_object
|
7
|
+
after_find :deserialize_service_object
|
8
|
+
|
9
|
+
class_attribute :service_class_resolver
|
10
|
+
class_attribute :service_class
|
11
|
+
class_attribute :service_attribute_name
|
12
|
+
|
13
|
+
# Set default service attribute name based on the class name
|
14
|
+
self.service_attribute_name = "#{name.demodulize.underscore}_service".to_sym
|
15
|
+
end
|
16
|
+
|
17
|
+
class_methods do
|
18
|
+
def service(class_or_proc = nil, &block)
|
19
|
+
if class_or_proc.is_a?(Class)
|
20
|
+
self.service_class = class_or_proc
|
21
|
+
elsif block_given?
|
22
|
+
self.service_class_resolver = block
|
23
|
+
else
|
24
|
+
raise ArgumentError, "You must provide a service class, factory, or a block to resolve the service class."
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def find_or_create_by!(attributes)
|
29
|
+
attributes = attributes.deep_symbolize_keys
|
30
|
+
db_attributes = extract_db_attributes(attributes)
|
31
|
+
attributes.except(*db_attributes.keys)
|
32
|
+
|
33
|
+
record = where(db_attributes).first_or_initialize(attributes)
|
34
|
+
|
35
|
+
record.save! if record.new_record?
|
36
|
+
|
37
|
+
record
|
38
|
+
end
|
39
|
+
|
40
|
+
def find_or_create_by(attributes)
|
41
|
+
attributes = attributes.deep_symbolize_keys
|
42
|
+
db_attributes = extract_db_attributes(attributes)
|
43
|
+
attributes.except(*db_attributes.keys)
|
44
|
+
|
45
|
+
record = where(db_attributes).first_or_initialize(attributes)
|
46
|
+
|
47
|
+
record.save if record.new_record?
|
48
|
+
|
49
|
+
record
|
50
|
+
end
|
51
|
+
|
52
|
+
def extract_db_attributes(attributes)
|
53
|
+
# Extract attributes that correspond to database columns or associations
|
54
|
+
column_names = self.column_names.map(&:to_sym)
|
55
|
+
association_names = reflect_on_all_associations.map(&:name)
|
56
|
+
|
57
|
+
attributes.slice(*(column_names + association_names))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def initialize(attributes = {})
|
62
|
+
attributes = attributes.deep_symbolize_keys
|
63
|
+
db_attributes = self.class.extract_db_attributes(attributes)
|
64
|
+
super(db_attributes)
|
65
|
+
self.class.send(:attr_reader, service_attribute_name)
|
66
|
+
build_service_object(attributes)
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def build_service_object(attributes)
|
72
|
+
service_class = resolve_service_class(attributes)
|
73
|
+
service_attributes = extract_service_attributes(attributes, service_class)
|
74
|
+
service_instance = service_class.new(service_attributes)
|
75
|
+
instance_variable_set("@#{service_attribute_name}", service_instance)
|
76
|
+
end
|
77
|
+
|
78
|
+
def resolve_service_class(attributes)
|
79
|
+
if self.class.service_class
|
80
|
+
self.class.service_class
|
81
|
+
elsif self.class.service_class_resolver
|
82
|
+
self.class.service_class_resolver.call(attributes)
|
83
|
+
else
|
84
|
+
raise "Service class not defined for #{self.class.name}"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def extract_service_attributes(attributes, service_class)
|
89
|
+
allowed_attrs = service_attributes(service_class)
|
90
|
+
attrs_and_associations(attributes).slice(*allowed_attrs)
|
91
|
+
end
|
92
|
+
|
93
|
+
def service_attributes(service_class)
|
94
|
+
service_class.dependency_definitions.keys.concat(
|
95
|
+
service_class.attribute_definitions.keys
|
96
|
+
)
|
97
|
+
end
|
98
|
+
|
99
|
+
def attrs_and_associations(attributes)
|
100
|
+
foreign_keys = foreign_key_map
|
101
|
+
attributes.inject({}) do |h, (k, v)|
|
102
|
+
h.tap do
|
103
|
+
if foreign_keys.include?(k)
|
104
|
+
assoc_name = foreign_keys[k]
|
105
|
+
h[assoc_name] = send(assoc_name)
|
106
|
+
else
|
107
|
+
h[k] = v
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def foreign_key_map
|
114
|
+
self.class.reflect_on_all_associations.inject({}) do |h, assoc|
|
115
|
+
h.tap do
|
116
|
+
h[assoc.foreign_key] = assoc.name
|
117
|
+
end
|
118
|
+
end.symbolize_keys
|
119
|
+
end
|
120
|
+
|
121
|
+
def serialize_service_object
|
122
|
+
service_object = instance_variable_get("@#{service_attribute_name}")
|
123
|
+
service_klass = service_object.class
|
124
|
+
attrs = service_klass.respond_to?(:serialize) ? service_klass.serialize(service_object) : service_object.attributes
|
125
|
+
deps = allowed_names(service_object.dependency_definitions.keys).inject({}) do |hash, dep|
|
126
|
+
hash.tap do
|
127
|
+
this_dep = service_object.send(dep)
|
128
|
+
next unless this_dep.present?
|
129
|
+
|
130
|
+
opts = service_object.dependency_definitions[dep].option_configs
|
131
|
+
selected_option = opts.detect do |_k, v|
|
132
|
+
this_dep.class == v.class_name
|
133
|
+
end&.first
|
134
|
+
unless selected_option.present?
|
135
|
+
raise "Don't know how to serialize dependency of type #{dep}, available options are #{opts.keys}. You didn't specify an option."
|
136
|
+
end
|
137
|
+
|
138
|
+
hash[dep] = {
|
139
|
+
selected_option => service_object.send(dep).attributes
|
140
|
+
}
|
141
|
+
end
|
142
|
+
end
|
143
|
+
json = serializable!(attrs.merge(deps.deep_compact).deep_symbolize_keys)
|
144
|
+
write_attribute(:configuration, json.to_json)
|
145
|
+
end
|
146
|
+
|
147
|
+
def serializable!(json)
|
148
|
+
regular_args = json.slice(*allowed_names(json.keys))
|
149
|
+
assoc_names = self.class.reflect_on_all_associations.map(&:name)
|
150
|
+
found_associations = assoc_names & json.keys
|
151
|
+
found_associations.each do |association|
|
152
|
+
regular_args[association] = true
|
153
|
+
end
|
154
|
+
regular_args
|
155
|
+
end
|
156
|
+
|
157
|
+
def deserialize_associations(json)
|
158
|
+
assoc_names = self.class.reflect_on_all_associations.map(&:name)
|
159
|
+
found_associations = assoc_names & json.keys
|
160
|
+
found_associations.each do |association|
|
161
|
+
json[association] = send(association)
|
162
|
+
end
|
163
|
+
json
|
164
|
+
end
|
165
|
+
|
166
|
+
def deserialize_service_object
|
167
|
+
serialized_data = JSON.parse(read_attribute(:configuration) || "{}")
|
168
|
+
serialized_data.deep_symbolize_keys!
|
169
|
+
service_class = resolve_service_class(serialized_data)
|
170
|
+
serialized_data = deserialize_associations(serialized_data)
|
171
|
+
serialized_data = service_class.deserialize(serialized_data) if service_class.respond_to?(:deserialize)
|
172
|
+
service_instance = build_service_object(serialized_data)
|
173
|
+
instance_variable_set("@#{service_attribute_name}", service_instance)
|
174
|
+
end
|
175
|
+
|
176
|
+
def allowed_names(names)
|
177
|
+
assoc_names = self.class.reflect_on_all_associations.map(&:name)
|
178
|
+
[names.map(&:to_sym) - assoc_names.map(&:to_sym)].flatten
|
179
|
+
end
|
180
|
+
|
181
|
+
def method_missing(method_name, *args, **kwargs, &block)
|
182
|
+
service_object = instance_variable_get("@#{service_attribute_name}")
|
183
|
+
|
184
|
+
if service_object && service_object.respond_to?(method_name)
|
185
|
+
service_object.send(method_name, *args, **kwargs, &block)
|
186
|
+
else
|
187
|
+
super
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def respond_to_missing?(method_name, include_private = false)
|
192
|
+
service_object = instance_variable_get("@#{service_attribute_name}")
|
193
|
+
service_object && service_object.respond_to?(method_name) || super
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
@@ -0,0 +1,73 @@
|
|
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
|
data/lib/glue_gun/version.rb
CHANGED
data/lib/glue_gun.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.22
|
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-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -190,7 +190,11 @@ extensions: []
|
|
190
190
|
extra_rdoc_files: []
|
191
191
|
files:
|
192
192
|
- lib/glue_gun.rb
|
193
|
+
- lib/glue_gun/core_ext.rb
|
194
|
+
- lib/glue_gun/core_ext/hash_extensions.rb
|
193
195
|
- lib/glue_gun/dsl.rb
|
196
|
+
- lib/glue_gun/model.rb
|
197
|
+
- lib/glue_gun/my_model.rb
|
194
198
|
- lib/glue_gun/types.rb
|
195
199
|
- lib/glue_gun/types/array_type.rb
|
196
200
|
- lib/glue_gun/types/date_time_type.rb
|