service_pattern 1.0.6 → 1.0.9
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc1d86311bd4cf4a8bd24e9261f1e7cc7bde638786699be7d859c1b6a8259e6f
|
4
|
+
data.tar.gz: 7874ffb81a44c4d6c05f281026e7926e5cf5b62b64aa0479d4eb76fc3620ed87
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b16d78da76eb279b919e28fa1dcdb9ec8d4a84cd44b2008bd002522112be7fdaef68037fe80646d1d87398e02cf86775f40b38510ea9d39452d58cc9c58228c8
|
7
|
+
data.tar.gz: 28f526d8a9516d1bff202a708e789f3fec76452d1240b8281ade421b4d7fb3b3b8979c92ebb93b38e355cd9a5765fa8732954ff90205bb6c09e4c7f330e9dc64
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class ServicePattern::ModelsSave < ServicePattern::Service
|
2
|
+
attr_reader :models, :simple_model_errors
|
3
|
+
|
4
|
+
def initialize(models:, simple_model_errors: false)
|
5
|
+
@models = models
|
6
|
+
@simple_model_errors = simple_model_errors
|
7
|
+
end
|
8
|
+
|
9
|
+
def perform
|
10
|
+
errors = []
|
11
|
+
|
12
|
+
models.first.transaction do
|
13
|
+
models.each do |model|
|
14
|
+
next if model.save
|
15
|
+
|
16
|
+
if simple_model_errors
|
17
|
+
errors += ServicePattern::SimpleModelErrors.execute!(model: model)
|
18
|
+
else
|
19
|
+
errors += models.map(&:errors).map(&:full_messages).flatten
|
20
|
+
end
|
21
|
+
|
22
|
+
raise ActiveRecord::Rollback if errors.any?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
fail! errors.uniq if errors.any?
|
27
|
+
succeed!
|
28
|
+
end
|
29
|
+
end
|
@@ -39,6 +39,46 @@ class ServicePattern::Service
|
|
39
39
|
raise error
|
40
40
|
end
|
41
41
|
|
42
|
+
def self.arguments(*arguments)
|
43
|
+
arguments.each do |argument_name|
|
44
|
+
argument argument_name
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.argument(argument_name, **args)
|
49
|
+
attr_accessor argument_name
|
50
|
+
|
51
|
+
@arguments ||= {}
|
52
|
+
@arguments[argument_name] ||= {}
|
53
|
+
|
54
|
+
args.each do |key, value|
|
55
|
+
if key == :default
|
56
|
+
@arguments[argument_name][key] = value
|
57
|
+
else
|
58
|
+
raise ArgumentError, "Invalid argument: #{argument_name}"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def initialize(**args)
|
64
|
+
arguments = self.class.instance_variable_get(:@arguments)
|
65
|
+
arguments&.each do |argument_name, argument_options|
|
66
|
+
next if args.key?(argument_name)
|
67
|
+
|
68
|
+
if argument_options.key?(:default)
|
69
|
+
__send__("#{argument_name}=", argument_options.fetch(:default))
|
70
|
+
else
|
71
|
+
raise ArgumentError, "missing keyword: #{argument_name}"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
args.each do |key, value|
|
76
|
+
raise ArgumentError, "unknown keyword: #{key}" unless arguments&.key?(key)
|
77
|
+
|
78
|
+
__send__("#{key}=", value)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
42
82
|
def can_execute?
|
43
83
|
succeed!
|
44
84
|
end
|
@@ -73,6 +113,10 @@ class ServicePattern::Service
|
|
73
113
|
ServicePattern::Service.fail!(errors)
|
74
114
|
end
|
75
115
|
|
116
|
+
def save_models_or_fail(*models, simple_model_errors: false)
|
117
|
+
ServicePattern::ModelsSave.execute!(models: models, simple_model_errors: simple_model_errors)
|
118
|
+
end
|
119
|
+
|
76
120
|
def succeed!(result = nil)
|
77
121
|
ServicePattern::Response.new(result: result)
|
78
122
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class ServicePattern::SimpleModelErrors
|
2
|
+
attr_reader :additional_attributes, :errors, :model, :models_inspected
|
3
|
+
|
4
|
+
def initialize(model:, additional_attributes: [])
|
5
|
+
@additional_attributes = additional_attributes
|
6
|
+
@model = model
|
7
|
+
@errors = []
|
8
|
+
@models_inspected = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def perform
|
12
|
+
inspect_model(model)
|
13
|
+
errors
|
14
|
+
end
|
15
|
+
|
16
|
+
def inspect_model(model_to_inspect)
|
17
|
+
return if models_inspected.include?(model_to_inspect)
|
18
|
+
|
19
|
+
model_to_inspect.valid? if model_to_inspect.errors.empty? # Generates the errors on the model so we can detect them
|
20
|
+
models_inspected << model_to_inspect
|
21
|
+
|
22
|
+
model_to_inspect.errors.messages.each do |attribute_name, attribute_errors|
|
23
|
+
if attribute_name == :base
|
24
|
+
@errors += attribute_errors
|
25
|
+
else
|
26
|
+
next if model_to_inspect.attribute_names.exclude?(attribute_name.to_s) && additional_attributes.exclude?(attribute_name)
|
27
|
+
|
28
|
+
attribute_errors.each do |message|
|
29
|
+
errors << "#{model_to_inspect.class.human_attribute_name(attribute_name)} #{message}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
collect_errors_from_associations(model_to_inspect)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def collect_errors_from_associations(model_to_scan_reflections_on)
|
40
|
+
model_to_scan_reflections_on._reflections.each_key do |association_name|
|
41
|
+
target = model_to_scan_reflections_on.association(association_name.to_sym).target
|
42
|
+
|
43
|
+
if target.is_a?(ActiveRecord::Base)
|
44
|
+
inspect_model(target)
|
45
|
+
else
|
46
|
+
target&.each do |sub_model|
|
47
|
+
inspect_model(sub_model)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/service_pattern.rb
CHANGED
@@ -5,6 +5,8 @@ module ServicePattern
|
|
5
5
|
|
6
6
|
autoload :FailError, "#{path}/fail_error"
|
7
7
|
autoload :FailedError, "#{path}/failed_error"
|
8
|
+
autoload :ModelsSave, "#{path}/models_save"
|
8
9
|
autoload :Response, "#{path}/response"
|
9
10
|
autoload :Service, "#{path}/service"
|
11
|
+
autoload :SimpleModelsError, "#{path}/simple_models_errors"
|
10
12
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: service_pattern
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kaspernj
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: appraisal
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name: pry
|
42
|
+
name: pry-rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
@@ -163,8 +163,10 @@ files:
|
|
163
163
|
- lib/service_pattern.rb
|
164
164
|
- lib/service_pattern/fail_error.rb
|
165
165
|
- lib/service_pattern/failed_error.rb
|
166
|
+
- lib/service_pattern/models_save.rb
|
166
167
|
- lib/service_pattern/response.rb
|
167
168
|
- lib/service_pattern/service.rb
|
169
|
+
- lib/service_pattern/simple_models_errors.rb
|
168
170
|
- lib/service_pattern/version.rb
|
169
171
|
- lib/tasks/service_pattern_tasks.rake
|
170
172
|
homepage: https://www.github.com/kaspernj/service_pattern
|