glue_gun_dsl 0.1.21 → 0.1.22

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: 1757d5bf22062e71a309eb62fd4b55aa830d36a7e7cde695f8525b960d5a0a89
4
- data.tar.gz: 8f591ab19ba6b88118834b65aa210166408bc127afba0551bb2792505e7abc64
3
+ metadata.gz: d8f609405b59c865b180e5e07a5a74ebbf4867cea468444983820202b17fbcb7
4
+ data.tar.gz: b71c914a42fe7175f41b7faa674c30c58d03fcc2c1f54c281bcbf0b8876b1800
5
5
  SHA512:
6
- metadata.gz: c25769bebf3f9e450970506b45fa13c82281d223f3da07f1d3e667597678903da636422c24924525456f1063b4d142722729fc9bb240eec9801a6d67a1a5fae9
7
- data.tar.gz: 13e57e0b3cc63e2ed15e26d480d0622e6413476dae3fff9ba8bf1686fc6fa4c314baad22452b157982cd10699cbb6f6574c84130168d83874d817aef0dbc98e9
6
+ metadata.gz: 425a1e574b7a54826b2d2881a38d5efc190d14c46e7f5c85cd9b779c17182ad01a1427a746a1dce10780d4243bf575e9ac8e3a467497c41cd1a1e74ac8466be7
7
+ data.tar.gz: e819aacabb11e0c6174f167cd9fee3eaf2fdd882797631b0c383abbbf9c0bb69431c28cde2bfc024cd391d6c4e526fb8e5c1fa1862396d28d3d3fc889ae737f4
data/lib/glue_gun/dsl.rb CHANGED
@@ -438,8 +438,13 @@ module GlueGun
438
438
  def determine_option_name(init_args, instance)
439
439
  option_name = nil
440
440
 
441
- # Use when block if defined
442
- if when_block
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]
@@ -24,11 +24,43 @@ module GlueGun
24
24
  raise ArgumentError, "You must provide a service class, factory, or a block to resolve the service class."
25
25
  end
26
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
27
59
  end
28
60
 
29
61
  def initialize(attributes = {})
30
62
  attributes = attributes.deep_symbolize_keys
31
- db_attributes = extract_db_attributes(attributes)
63
+ db_attributes = self.class.extract_db_attributes(attributes)
32
64
  super(db_attributes)
33
65
  self.class.send(:attr_reader, service_attribute_name)
34
66
  build_service_object(attributes)
@@ -36,14 +68,6 @@ module GlueGun
36
68
 
37
69
  private
38
70
 
39
- def extract_db_attributes(attributes)
40
- # Extract attributes that correspond to database columns or associations
41
- column_names = self.class.column_names.map(&:to_sym)
42
- association_names = self.class.reflect_on_all_associations.map(&:name)
43
-
44
- attributes.slice(*(column_names + association_names))
45
- end
46
-
47
71
  def build_service_object(attributes)
48
72
  service_class = resolve_service_class(attributes)
49
73
  service_attributes = extract_service_attributes(attributes, service_class)
@@ -116,16 +140,36 @@ module GlueGun
116
140
  }
117
141
  end
118
142
  end
119
- json = attrs.merge(deps.deep_compact).deep_symbolize_keys
143
+ json = serializable!(attrs.merge(deps.deep_compact).deep_symbolize_keys)
120
144
  write_attribute(:configuration, json.to_json)
121
145
  end
122
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
+
123
166
  def deserialize_service_object
124
167
  serialized_data = JSON.parse(read_attribute(:configuration) || "{}")
125
168
  serialized_data.deep_symbolize_keys!
126
169
  service_class = resolve_service_class(serialized_data)
170
+ serialized_data = deserialize_associations(serialized_data)
127
171
  serialized_data = service_class.deserialize(serialized_data) if service_class.respond_to?(:deserialize)
128
- service_instance = service_class.new(serialized_data)
172
+ service_instance = build_service_object(serialized_data)
129
173
  instance_variable_set("@#{service_attribute_name}", service_instance)
130
174
  end
131
175
 
@@ -134,23 +178,11 @@ module GlueGun
134
178
  [names.map(&:to_sym) - assoc_names.map(&:to_sym)].flatten
135
179
  end
136
180
 
137
- def serialize
138
- dataset_service.attributes
139
- write_attribute(:configuration, json.to_json)
140
- end
141
-
142
- def deserialize
143
- options = JSON.parse(read_attribute(:configuration))
144
- options.deep_symbolize_keys!
145
-
146
- build_dataset_service(options)
147
- end
148
-
149
- def method_missing(method_name, *args, &block)
181
+ def method_missing(method_name, *args, **kwargs, &block)
150
182
  service_object = instance_variable_get("@#{service_attribute_name}")
151
183
 
152
184
  if service_object && service_object.respond_to?(method_name)
153
- service_object.send(method_name, *args, &block)
185
+ service_object.send(method_name, *args, **kwargs, &block)
154
186
  else
155
187
  super
156
188
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GlueGun
4
- VERSION = "0.1.21"
4
+ VERSION = "0.1.22"
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.21
4
+ version: 0.1.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brett Shollenberger