amazing-activist 0.2.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.adoc +1 -1
- data/lib/amazing_activist/base.rb +12 -10
- data/lib/amazing_activist/broken_contract_error.rb +7 -0
- data/lib/amazing_activist/contractable.rb +35 -0
- data/lib/amazing_activist/irresistible.rb +42 -0
- data/lib/amazing_activist/outcome/failure.rb +22 -12
- data/lib/amazing_activist/outcome/success.rb +4 -4
- data/lib/amazing_activist/rescuable.rb +57 -0
- data/lib/amazing_activist/unwrap_error.rb +5 -0
- data/lib/amazing_activist/version.rb +1 -1
- metadata +9 -6
- data/CHANGELOG.md +0 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ccad58a6e900c0d926811f0f5520a807665c3966efff845ac4359c00493cf0cf
|
4
|
+
data.tar.gz: 79b51504d7c1bea9dc76bcefe0d78697d7a3e30f15bd21c3dac8684ebaf81dac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36d6f413e5a39dbde9bf7c0aa6ef40bb58f438096ba659963536c7a2c059c135a00f88f765058edfdb53bdc572352f48f5ac6ebd3d7e4444205b9c34c85ccf51
|
7
|
+
data.tar.gz: 56347f7ea9356a1edfdff9f41816b34036f25184ccc7e102ebb11524371b7e3e2e1f134d17bf6644c47ebc7ffd1f36fc369f12f438c62571837f9ae48e8684b2
|
data/README.adoc
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative "./irresistible"
|
3
4
|
require_relative "./outcome"
|
4
5
|
|
5
6
|
module AmazingActivist
|
@@ -18,16 +19,17 @@ module AmazingActivist
|
|
18
19
|
# failure(:invalid_params, user: user)
|
19
20
|
# end
|
20
21
|
# end
|
22
|
+
#
|
23
|
+
# case OnboardActivity.call(email: "user@example.com")
|
24
|
+
# in success: user
|
25
|
+
# Current.user = user
|
26
|
+
# redirect_to dashboard_url
|
27
|
+
# else
|
28
|
+
# render :new, status: :unprocessable_entity
|
29
|
+
# end
|
21
30
|
# ----
|
22
31
|
class Base
|
23
|
-
|
24
|
-
# Convenience method to initialize and immediatelly call the activity.
|
25
|
-
# @see #initialize
|
26
|
-
# @see #call
|
27
|
-
def call(...)
|
28
|
-
new(...).call
|
29
|
-
end
|
30
|
-
end
|
32
|
+
extend Irresistible
|
31
33
|
|
32
34
|
# @param params [Hash{Symbol => Object}]
|
33
35
|
def initialize(**params)
|
@@ -53,8 +55,8 @@ module AmazingActivist
|
|
53
55
|
# @param code (see Outcome::Failure#initialize)
|
54
56
|
# @param context (see Outcome::Failure#initialize)
|
55
57
|
# @return [Outcome::Failure]
|
56
|
-
def failure(code,
|
57
|
-
Outcome::Failure.new(code, activity: self, context: context)
|
58
|
+
def failure(code, message: nil, exception: nil, context: {})
|
59
|
+
Outcome::Failure.new(code, activity: self, message: message, exception: exception, context: context)
|
58
60
|
end
|
59
61
|
end
|
60
62
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./broken_contract_error"
|
4
|
+
|
5
|
+
module AmazingActivist
|
6
|
+
module Contractable
|
7
|
+
DEFAULT_BROKEN_OUTCOME_HANDLER = lambda do |outcome|
|
8
|
+
raise BrokenContractError, "#{self.class}#call returned #{outcome.class} instead of Outcome"
|
9
|
+
end
|
10
|
+
private_constant :DEFAULT_BROKEN_OUTCOME_HANDLER
|
11
|
+
|
12
|
+
protected
|
13
|
+
|
14
|
+
# @api internal
|
15
|
+
def broken_contract_handler
|
16
|
+
return @broken_contract_handler if defined?(@broken_contract_handler)
|
17
|
+
|
18
|
+
ancestors.each do |klass|
|
19
|
+
next unless klass < Base && klass != self
|
20
|
+
|
21
|
+
return @broken_contract_handler = klass.broken_contract_handler
|
22
|
+
end
|
23
|
+
|
24
|
+
@broken_contract_handler = DEFAULT_BROKEN_OUTCOME_HANDLER
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def on_broken_outcome(&block)
|
30
|
+
raise ArgumentError, "Handler block required." unless block
|
31
|
+
|
32
|
+
@broken_contract_handler = block
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./broken_contract_error"
|
4
|
+
require_relative "./contractable"
|
5
|
+
require_relative "./rescuable"
|
6
|
+
require_relative "./unwrap_error"
|
7
|
+
|
8
|
+
module AmazingActivist
|
9
|
+
module Irresistible
|
10
|
+
include Contractable
|
11
|
+
include Rescuable
|
12
|
+
|
13
|
+
# Initialize and call activity.
|
14
|
+
#
|
15
|
+
# @see #initialize
|
16
|
+
# @see #call
|
17
|
+
def call(...)
|
18
|
+
activity = new(...)
|
19
|
+
outcome = irresistible_call(activity)
|
20
|
+
|
21
|
+
unless outcome.is_a?(Outcome::Failure) || outcome.is_a?(Outcome::Success)
|
22
|
+
return activity.instance_exec(outcome, &broken_contract_handler)
|
23
|
+
end
|
24
|
+
|
25
|
+
outcome
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# @api internal
|
31
|
+
def irresistible_call(activity)
|
32
|
+
activity.call
|
33
|
+
rescue UnwrapError => e
|
34
|
+
e.failure
|
35
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
36
|
+
handler = rescue_handler_for(e)
|
37
|
+
raise unless handler
|
38
|
+
|
39
|
+
activity.instance_exec(e, &handler)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -9,19 +9,26 @@ module AmazingActivist
|
|
9
9
|
# @return [Symbol]
|
10
10
|
attr_reader :code
|
11
11
|
|
12
|
-
# @return [AmazingActivist::
|
12
|
+
# @return [AmazingActivist::Base]
|
13
13
|
attr_reader :activity
|
14
14
|
|
15
|
-
# @return [
|
15
|
+
# @return [Exception, nil]
|
16
|
+
attr_reader :exception
|
17
|
+
|
18
|
+
# @return [Hash]
|
16
19
|
attr_reader :context
|
17
20
|
|
18
21
|
# @param code [#to_sym]
|
19
22
|
# @param activity [AmazingActivist::Activity]
|
20
|
-
# @param
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
@
|
23
|
+
# @param message [#to_s, nil]
|
24
|
+
# @param exception [Exception, nil]
|
25
|
+
# @param context [Hash]
|
26
|
+
def initialize(code, activity:, message:, exception:, context:)
|
27
|
+
@code = code.to_sym
|
28
|
+
@activity = activity
|
29
|
+
@message = message&.to_s
|
30
|
+
@exception = exception
|
31
|
+
@context = context
|
25
32
|
end
|
26
33
|
|
27
34
|
# @return [true]
|
@@ -35,15 +42,15 @@ module AmazingActivist
|
|
35
42
|
end
|
36
43
|
|
37
44
|
# @api internal
|
38
|
-
# @return [Array
|
45
|
+
# @return [Array]
|
39
46
|
def deconstruct
|
40
|
-
[:failure, @code, @activity
|
47
|
+
[:failure, @code, @activity]
|
41
48
|
end
|
42
49
|
|
43
50
|
# @api internal
|
44
|
-
# @return [Hash
|
51
|
+
# @return [Hash]
|
45
52
|
def deconstruct_keys(_)
|
46
|
-
{ failure: @code, activity: @activity, context:
|
53
|
+
{ failure: @code, activity: @activity, message: message, exception: exception, context: context }
|
47
54
|
end
|
48
55
|
|
49
56
|
# @yieldparam self [self]
|
@@ -56,8 +63,11 @@ module AmazingActivist
|
|
56
63
|
raise UnwrapError, self
|
57
64
|
end
|
58
65
|
|
66
|
+
# Failure message.
|
67
|
+
#
|
68
|
+
# @return [String]
|
59
69
|
def message
|
60
|
-
@
|
70
|
+
@message || Polyglot.new(@activity).message(@code, **context)
|
61
71
|
end
|
62
72
|
end
|
63
73
|
end
|
@@ -3,11 +3,11 @@
|
|
3
3
|
module AmazingActivist
|
4
4
|
module Outcome
|
5
5
|
class Success
|
6
|
-
# @return [AmazingActivist::
|
6
|
+
# @return [AmazingActivist::Base]
|
7
7
|
attr_reader :activity
|
8
8
|
|
9
9
|
# @param value [Object]
|
10
|
-
# @param activity [AmazingActivist::
|
10
|
+
# @param activity [AmazingActivist::Base]
|
11
11
|
def initialize(value, activity:)
|
12
12
|
@value = value
|
13
13
|
@activity = activity
|
@@ -24,13 +24,13 @@ module AmazingActivist
|
|
24
24
|
end
|
25
25
|
|
26
26
|
# @api internal
|
27
|
-
# @return [Array
|
27
|
+
# @return [Array]
|
28
28
|
def deconstruct
|
29
29
|
[:success, @value, @activity]
|
30
30
|
end
|
31
31
|
|
32
32
|
# @api internal
|
33
|
-
# @return [Hash
|
33
|
+
# @return [Hash]
|
34
34
|
def deconstruct_keys(_)
|
35
35
|
{ success: @value, activity: @activity }
|
36
36
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AmazingActivist
|
4
|
+
module Rescuable
|
5
|
+
protected
|
6
|
+
|
7
|
+
# @api internal
|
8
|
+
def rescue_handlers
|
9
|
+
return @rescue_handlers if defined?(@rescue_handlers)
|
10
|
+
|
11
|
+
ancestors.each do |klass|
|
12
|
+
next unless klass < Base && klass != self
|
13
|
+
|
14
|
+
return @rescue_handlers = klass.rescue_handlers
|
15
|
+
end
|
16
|
+
|
17
|
+
@rescue_handlers = [].freeze
|
18
|
+
end
|
19
|
+
|
20
|
+
# @api internal
|
21
|
+
def rescue_handlers=(new_handlers)
|
22
|
+
@rescue_handlers = new_handlers.freeze
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def rescue_from(*klasses, &block)
|
28
|
+
raise ArgumentError, "Handler block required." unless block
|
29
|
+
|
30
|
+
klasses.each do |klass|
|
31
|
+
klass_name =
|
32
|
+
case klass
|
33
|
+
when Module then klass.name
|
34
|
+
when String then klass
|
35
|
+
else raise ArgumentError, "#{klass.inspect} must be an Exception class or a String referencing a class."
|
36
|
+
end
|
37
|
+
|
38
|
+
self.rescue_handlers += [[klass_name, block].freeze]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# @api internal
|
43
|
+
def rescue_handler_for(exception)
|
44
|
+
while exception
|
45
|
+
rescue_handlers.reverse_each do |klass, handler|
|
46
|
+
return handler if exception.is_a?(const_get(klass))
|
47
|
+
rescue StandardError
|
48
|
+
# do nothing
|
49
|
+
end
|
50
|
+
|
51
|
+
exception = exception.cause
|
52
|
+
end
|
53
|
+
|
54
|
+
nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amazing-activist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexey Zapparov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -45,19 +45,22 @@ executables: []
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
-
- CHANGELOG.md
|
49
48
|
- LICENSE.txt
|
50
49
|
- README.adoc
|
51
50
|
- lib/amazing-activist.rb
|
52
51
|
- lib/amazing_activist.rb
|
53
52
|
- lib/amazing_activist/base.rb
|
53
|
+
- lib/amazing_activist/broken_contract_error.rb
|
54
|
+
- lib/amazing_activist/contractable.rb
|
54
55
|
- lib/amazing_activist/error.rb
|
56
|
+
- lib/amazing_activist/irresistible.rb
|
55
57
|
- lib/amazing_activist/locale/en.yml
|
56
58
|
- lib/amazing_activist/locale/gl.yml
|
57
59
|
- lib/amazing_activist/outcome.rb
|
58
60
|
- lib/amazing_activist/outcome/failure.rb
|
59
61
|
- lib/amazing_activist/outcome/success.rb
|
60
62
|
- lib/amazing_activist/polyglot.rb
|
63
|
+
- lib/amazing_activist/rescuable.rb
|
61
64
|
- lib/amazing_activist/unwrap_error.rb
|
62
65
|
- lib/amazing_activist/version.rb
|
63
66
|
homepage: https://github.com/ixti/amazing-activist
|
@@ -65,9 +68,9 @@ licenses:
|
|
65
68
|
- MIT
|
66
69
|
metadata:
|
67
70
|
homepage_uri: https://github.com/ixti/amazing-activist
|
68
|
-
source_code_uri: https://github.com/ixti/amazing-activist/tree/v0.
|
71
|
+
source_code_uri: https://github.com/ixti/amazing-activist/tree/v0.4.0
|
69
72
|
bug_tracker_uri: https://github.com/ixti/amazing-activist/issues
|
70
|
-
changelog_uri: https://github.com/ixti/amazing-activist/blob/v0.
|
73
|
+
changelog_uri: https://github.com/ixti/amazing-activist/blob/v0.4.0/CHANGES.md
|
71
74
|
rubygems_mfa_required: 'true'
|
72
75
|
post_install_message:
|
73
76
|
rdoc_options: []
|
@@ -84,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
87
|
- !ruby/object:Gem::Version
|
85
88
|
version: '0'
|
86
89
|
requirements: []
|
87
|
-
rubygems_version: 3.
|
90
|
+
rubygems_version: 3.5.4
|
88
91
|
signing_key:
|
89
92
|
specification_version: 4
|
90
93
|
summary: Your friendly neighborhood activist.
|
data/CHANGELOG.md
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# Changelog
|
2
|
-
|
3
|
-
All notable changes to this project will be documented in this file.
|
4
|
-
|
5
|
-
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
6
|
-
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
-
|
8
|
-
## [Unreleased]
|
9
|
-
|
10
|
-
|
11
|
-
## [0.2.0] - 2024-01-28
|
12
|
-
|
13
|
-
## Added
|
14
|
-
|
15
|
-
- Generate default failure messages with
|
16
|
-
[i18n](https://github.com/ruby-i18n/i18n).
|
17
|
-
|
18
|
-
|
19
|
-
## [0.1.0] - 2024-01-27
|
20
|
-
|
21
|
-
## Added
|
22
|
-
|
23
|
-
- Initial release.
|
24
|
-
|
25
|
-
[0.1.0]: https://github.com/ixti/sidekiq-antidote/tree/v0.1.0
|