action_processor 0.0.6 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/action_processor/base.rb +14 -11
- data/lib/action_processor/errors.rb +11 -6
- data/lib/action_processor.rb +2 -2
- metadata +10 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf58c5b403a2ec7095d1b43e07ea206596f7d324375d9cf5720f396b7a448e36
|
4
|
+
data.tar.gz: bb14d7a08d7cab44398433e7539046aa2398167866c1d8e8e4fe0e7c0fba1cc5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 192243d4c626765a18bda4fa4949ad9a239ad85de7c7732e0f919dfd467b4b41b10f127c174b07d6daff41d59935997a3bede59ce5ebf1066ae385bd0e29cb12
|
7
|
+
data.tar.gz: 4cc79bb34950eefb80c32890d9239674d63e9b579928c4d053e9d5567a6473fd46fc343e7a0c3256c268d9b96b0ecbf8f60130aa38bbe327b5000a23c9462062
|
@@ -2,8 +2,9 @@
|
|
2
2
|
|
3
3
|
require "active_support/core_ext/hash/indifferent_access"
|
4
4
|
require "active_record"
|
5
|
+
require_relative "errors"
|
5
6
|
|
6
|
-
module ActionProcessor
|
7
|
+
module ActionProcessor
|
7
8
|
class Base
|
8
9
|
attr_reader :params, :errors
|
9
10
|
|
@@ -14,11 +15,11 @@ module ActionProcessor
|
|
14
15
|
end
|
15
16
|
|
16
17
|
def initialize(params = {})
|
17
|
-
@params = if params.
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
@params = if params.instance_of? Hash
|
19
|
+
params.with_indifferent_access
|
20
|
+
else
|
21
|
+
params
|
22
|
+
end
|
22
23
|
@errors = ActionProcessor::Errors.new
|
23
24
|
@steps_stack = []
|
24
25
|
@transaction_level = ActiveRecord::Base.connection.open_transactions
|
@@ -44,12 +45,12 @@ module ActionProcessor
|
|
44
45
|
|
45
46
|
# in most cases should be overriden to provide relevant data
|
46
47
|
def successful_json
|
47
|
-
{
|
48
|
+
{success: true}
|
48
49
|
end
|
49
50
|
|
50
51
|
# could be overriden to provide some specifics/advices/etc.
|
51
52
|
def failed_json
|
52
|
-
{
|
53
|
+
{success: false, errors: errors.grouped_by_attribute}
|
53
54
|
end
|
54
55
|
|
55
56
|
def step(step_method, **options)
|
@@ -62,7 +63,7 @@ module ActionProcessor
|
|
62
63
|
@steps_stack << (@current_step || :not_specified)
|
63
64
|
@current_step = step_method
|
64
65
|
# performs even if there are errors
|
65
|
-
# useful for:
|
66
|
+
# useful for:
|
66
67
|
# - validation steps to return list of all errors
|
67
68
|
# - errors reporting and making decisions at the end of processing
|
68
69
|
send step_method, **options
|
@@ -76,6 +77,8 @@ module ActionProcessor
|
|
76
77
|
def fail!(errs, attr = :not_specified)
|
77
78
|
if errs.class.ancestors.map(&:to_s).include?("ActiveRecord::Base")
|
78
79
|
fail_active_record!(errs)
|
80
|
+
elsif errs.instance_of? ActionProcessor::Errors
|
81
|
+
@errors.concat errs
|
79
82
|
else
|
80
83
|
@errors.add(errs, @current_step, attr)
|
81
84
|
end
|
@@ -92,7 +95,7 @@ module ActionProcessor
|
|
92
95
|
@list_of_allowed_params ||= []
|
93
96
|
@list_of_allowed_params += list.map(&:to_s)
|
94
97
|
list.map(&:to_s).each do |param|
|
95
|
-
next
|
98
|
+
next unless params[param].nil?
|
96
99
|
|
97
100
|
fail! "Parameter :#{param} should be present", param
|
98
101
|
end
|
@@ -127,4 +130,4 @@ module ActionProcessor
|
|
127
130
|
ActiveRecord::Base.connection.open_transactions > @transaction_level
|
128
131
|
end
|
129
132
|
end
|
130
|
-
end
|
133
|
+
end
|
@@ -12,7 +12,12 @@ module ActionProcessor
|
|
12
12
|
|
13
13
|
def add(messages, step = :not_specified, attribute = :not_specified)
|
14
14
|
step ||= :not_specified # in case we will receive explicit nil as step parameter
|
15
|
-
@all << {
|
15
|
+
@all << {messages: [messages].flatten, step: step.to_sym, attribute: attribute.to_sym}
|
16
|
+
end
|
17
|
+
|
18
|
+
def concat(more_errors)
|
19
|
+
raise ArgumentError, "Expected an ActionProcessor::Errors object" unless more_errors.is_a?(ActionProcessor::Errors)
|
20
|
+
@all += more_errors.all
|
16
21
|
end
|
17
22
|
|
18
23
|
# returns array of strings with user friendly error messages
|
@@ -24,16 +29,16 @@ module ActionProcessor
|
|
24
29
|
all_messages
|
25
30
|
end
|
26
31
|
|
27
|
-
|
32
|
+
alias_method :full_messages, :messages # for compatibility with ActiveRecord::Errors
|
28
33
|
|
29
34
|
def for_attribute(attr)
|
30
|
-
@grouped_by_attribute[attr]
|
35
|
+
@grouped_by_attribute[attr]
|
31
36
|
end
|
32
37
|
|
33
38
|
def grouped_by_attribute
|
34
39
|
return @grouped_by_attribute if @grouped_by_attribute.present?
|
35
40
|
|
36
|
-
# we assume that all errors will be present
|
41
|
+
# we assume that all errors will be present
|
37
42
|
# at the time when this method called first time
|
38
43
|
@grouped_by_attribute = {}.with_indifferent_access
|
39
44
|
@all.each do |err|
|
@@ -41,6 +46,6 @@ module ActionProcessor
|
|
41
46
|
@grouped_by_attribute[err.attribute] += err[:messages]
|
42
47
|
end
|
43
48
|
@grouped_by_attribute
|
44
|
-
end
|
49
|
+
end
|
45
50
|
end
|
46
|
-
end
|
51
|
+
end
|
data/lib/action_processor.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
require_relative "
|
2
|
-
require_relative "
|
1
|
+
require_relative "action_processor/errors"
|
2
|
+
require_relative "action_processor/base"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_processor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ghennadii Mirosnicenco
|
@@ -9,36 +9,36 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2024-02-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- - "
|
18
|
+
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: 7
|
20
|
+
version: '7'
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- - "
|
25
|
+
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: 7
|
27
|
+
version: '7'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: activerecord
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- - "
|
32
|
+
- - ">="
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: 7
|
34
|
+
version: '7'
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- - "
|
39
|
+
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: 7
|
41
|
+
version: '7'
|
42
42
|
description: action_processor
|
43
43
|
email: linkator7@gmail.com
|
44
44
|
executables: []
|