mime_actor 0.6.1 → 0.6.3.alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/lib/mime_actor/action.rb +9 -5
- data/lib/mime_actor/callbacks.rb +201 -0
- data/lib/mime_actor/dispatcher.rb +63 -0
- data/lib/mime_actor/errors.rb +2 -0
- data/lib/mime_actor/rescue.rb +55 -14
- data/lib/mime_actor/scene.rb +7 -15
- data/lib/mime_actor/stage.rb +22 -32
- data/lib/mime_actor/validator.rb +15 -1
- data/lib/mime_actor/version.rb +6 -4
- data/lib/mime_actor.rb +15 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5221de8b1d1149a88de730ab8e6c6c764a22e7067b9e57b6265ce801c671ce3f
|
4
|
+
data.tar.gz: e98f9d6f3e5a8aa04f16cf77921edad0b4a61893ddb3c019157885079462fcde
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f6ce7e70ab30a4be726f346beb517c8036c95a1d516c3012d9ed62723a14c130d4af05cdf3695244f5fcb56d33f83cf719369ead9d05eabeba89e5335de1f3a
|
7
|
+
data.tar.gz: 90e8ca69ac9ff99e9813c1dac19bc0826893f106968bb6c6684201a84523b13ab9df225fd597daf96379825080f9164ecfb19a6f7b48c8eabf217e5411654530
|
data/.rubocop.yml
CHANGED
data/lib/mime_actor/action.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# :markup: markdown
|
4
|
+
|
5
|
+
require "mime_actor/callbacks"
|
6
|
+
require "mime_actor/logging"
|
3
7
|
require "mime_actor/scene"
|
4
8
|
require "mime_actor/stage"
|
5
|
-
require "mime_actor/rescue"
|
6
9
|
|
7
10
|
require "active_support/concern"
|
8
11
|
require "active_support/core_ext/object/blank"
|
@@ -23,9 +26,9 @@ module MimeActor
|
|
23
26
|
include AbstractController::Rendering # required by MimeResponds
|
24
27
|
include ActionController::MimeResponds
|
25
28
|
|
29
|
+
include Callbacks
|
26
30
|
include Scene
|
27
31
|
include Stage
|
28
|
-
include Rescue
|
29
32
|
include Logging
|
30
33
|
|
31
34
|
# The core logic where rendering logics are collected as `Proc` and passed over to `ActionController::MimeResponds`
|
@@ -44,7 +47,6 @@ module MimeActor
|
|
44
47
|
# end
|
45
48
|
#
|
46
49
|
def start_scene(action)
|
47
|
-
action = action&.to_sym
|
48
50
|
formats = acting_scenes.fetch(action, {})
|
49
51
|
|
50
52
|
if formats.empty?
|
@@ -54,8 +56,10 @@ module MimeActor
|
|
54
56
|
|
55
57
|
respond_to do |collector|
|
56
58
|
formats.each do |format, actor|
|
57
|
-
dispatch =
|
58
|
-
|
59
|
+
dispatch = lambda do
|
60
|
+
run_act_callbacks(format) do
|
61
|
+
cue_actor(actor.presence || "#{action}_#{format}", action:, format:)
|
62
|
+
end
|
59
63
|
end
|
60
64
|
collector.public_send(format, &dispatch)
|
61
65
|
end
|
@@ -0,0 +1,201 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# :markup: markdown
|
4
|
+
|
5
|
+
require "mime_actor/validator"
|
6
|
+
|
7
|
+
require "active_support/callbacks"
|
8
|
+
require "active_support/concern"
|
9
|
+
require "active_support/core_ext/array/wrap"
|
10
|
+
require "active_support/core_ext/object/blank"
|
11
|
+
|
12
|
+
module MimeActor
|
13
|
+
# # MimeActor Callbacks
|
14
|
+
#
|
15
|
+
# MimeActor provides hooks during the life cycle of an act. Available callbacks are:
|
16
|
+
#
|
17
|
+
# - before_act
|
18
|
+
# - around_act
|
19
|
+
# - after_act
|
20
|
+
#
|
21
|
+
# NOTE: Calling the same callback multiple times will overwrite previous callback definitions.
|
22
|
+
#
|
23
|
+
module Callbacks
|
24
|
+
extend ActiveSupport::Concern
|
25
|
+
|
26
|
+
include ActiveSupport::Callbacks
|
27
|
+
include MimeActor::Validator
|
28
|
+
|
29
|
+
included do
|
30
|
+
define_callbacks :act, skip_after_callbacks_if_terminated: true
|
31
|
+
|
32
|
+
%i[before after around].each { |kind| define_act_callbacks(kind) }
|
33
|
+
end
|
34
|
+
|
35
|
+
module ClassMethods
|
36
|
+
class ActionMatcher
|
37
|
+
def initialize(actions)
|
38
|
+
@actions = Array.wrap(actions).to_set(&:to_s)
|
39
|
+
end
|
40
|
+
|
41
|
+
def match?(controller)
|
42
|
+
@actions.include?(controller.action_name)
|
43
|
+
end
|
44
|
+
|
45
|
+
alias after match?
|
46
|
+
alias before match?
|
47
|
+
alias around match?
|
48
|
+
end
|
49
|
+
|
50
|
+
def callback_chain_name(format = nil)
|
51
|
+
if format.present?
|
52
|
+
validate!(:format, format)
|
53
|
+
:"act_#{format}"
|
54
|
+
else
|
55
|
+
:act
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def callback_chain_defined?(name)
|
60
|
+
!!get_callbacks(name)
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def define_callback_chain(name)
|
66
|
+
return if callback_chain_defined?(name)
|
67
|
+
|
68
|
+
define_callbacks name, skip_after_callbacks_if_terminated: true
|
69
|
+
end
|
70
|
+
|
71
|
+
def configure_callbacks(callbacks, actions, formats, block)
|
72
|
+
options = {}
|
73
|
+
options[:if] = ActionMatcher.new(actions) if actions.present?
|
74
|
+
callbacks.push(block) if block
|
75
|
+
|
76
|
+
formats = Array.wrap(formats)
|
77
|
+
formats << nil if formats.empty?
|
78
|
+
|
79
|
+
callbacks.each do |callback|
|
80
|
+
formats.each do |format|
|
81
|
+
chain = callback_chain_name(format)
|
82
|
+
define_callback_chain(chain)
|
83
|
+
yield chain, callback, options
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def validate_callback_options!(action, format)
|
89
|
+
validate!(:action_or_actions, action) if action.present?
|
90
|
+
validate!(:format_or_formats, format) if format.present?
|
91
|
+
end
|
92
|
+
|
93
|
+
def define_act_callbacks(kind)
|
94
|
+
module_eval(
|
95
|
+
# def self.before_act(*callbacks, action: nil, format: nil, &block)
|
96
|
+
# validate_callback_options!(action, format)
|
97
|
+
# configure_callbacks(callbacks, action, format, block) do |chain, callback, options|
|
98
|
+
# set_callback(chain, :before, callback, options)
|
99
|
+
# end
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
# def self.prepend_before_act(*callbacks, action: nil, format: nil, &block)
|
103
|
+
# validate_callback_options!(action, format)
|
104
|
+
# configure_callbacks(callbacks, action, format, block) do |chain, callback, options|
|
105
|
+
# set_callback(chain, :before, callback, options.merge!(prepend: true))
|
106
|
+
# end
|
107
|
+
# end
|
108
|
+
<<-RUBY, __FILE__, __LINE__ + 1
|
109
|
+
def self.#{kind}_act(*callbacks, action: nil, format: nil, &block)
|
110
|
+
validate_callback_options!(action, format)
|
111
|
+
configure_callbacks(callbacks, action, format, block) do |chain, callback, options|
|
112
|
+
set_callback(chain, :#{kind}, callback, options)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.prepend_#{kind}_act(*callbacks, action: nil, format: nil, &block)
|
117
|
+
validate_callback_options!(action, format)
|
118
|
+
configure_callbacks(callbacks, action, format, block) do |chain, callback, options|
|
119
|
+
set_callback(chain, :#{kind}, callback, options.merge!(prepend: true))
|
120
|
+
end
|
121
|
+
end
|
122
|
+
RUBY
|
123
|
+
)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# Callbacks invocation sequence depends on the order of callback definition.
|
128
|
+
# (except for callbacks with `format` filter).
|
129
|
+
#
|
130
|
+
# @example callbacks with/without action filter
|
131
|
+
# before_act :my_before_act_one
|
132
|
+
# before_act :my_before_act_two, action: :create
|
133
|
+
# before_act :my_before_act_three
|
134
|
+
#
|
135
|
+
# around_act :my_around_act_one
|
136
|
+
# around_act :my_around_act_two, action: :create
|
137
|
+
# around_act :my_around_act_three
|
138
|
+
#
|
139
|
+
# after_act :my_after_act_one
|
140
|
+
# after_act :my_after_act_two, action: :create
|
141
|
+
# after_act :my_after_act_three
|
142
|
+
#
|
143
|
+
# # actual sequence:
|
144
|
+
# # - my_before_act_one
|
145
|
+
# # - my_before_act_two
|
146
|
+
# # - my_before_act_three
|
147
|
+
# # - my_around_act_one
|
148
|
+
# # - my_around_act_two
|
149
|
+
# # - my_around_act_three
|
150
|
+
# # - my_after_act_three
|
151
|
+
# # - my_after_act_two
|
152
|
+
# # - my_after_act_one
|
153
|
+
#
|
154
|
+
# @example callbacks with format filter
|
155
|
+
# before_act :my_before_act_one
|
156
|
+
# before_act :my_before_act_two, action: :create
|
157
|
+
# before_act :my_before_act_three, action: :create, format: :html
|
158
|
+
# before_act :my_before_act_four
|
159
|
+
#
|
160
|
+
# around_act :my_around_act_one
|
161
|
+
# around_act :my_around_act_two, action: :create, format: :html
|
162
|
+
# around_act :my_around_act_three, action: :create
|
163
|
+
# around_act :my_around_act_four
|
164
|
+
#
|
165
|
+
# after_act :my_after_act_one, format: :html
|
166
|
+
# after_act :my_after_act_two
|
167
|
+
# after_act :my_after_act_three, action: :create
|
168
|
+
# after_act :my_after_act_four
|
169
|
+
#
|
170
|
+
# # actual sequence:
|
171
|
+
# # - my_before_act_one
|
172
|
+
# # - my_before_act_two
|
173
|
+
# # - my_before_act_four
|
174
|
+
# # - my_around_act_one
|
175
|
+
# # - my_around_act_three
|
176
|
+
# # - my_around_act_four
|
177
|
+
# # - my_before_act_three
|
178
|
+
# # - my_around_act_two
|
179
|
+
# # - my_after_act_one
|
180
|
+
# # - my_after_act_four
|
181
|
+
# # - my_after_act_three
|
182
|
+
# # - my_after_act_two
|
183
|
+
#
|
184
|
+
def run_act_callbacks(format)
|
185
|
+
action_chain = self.class.callback_chain_name
|
186
|
+
format_chain = self.class.callback_chain_name(format)
|
187
|
+
|
188
|
+
if self.class.callback_chain_defined?(format_chain)
|
189
|
+
run_callbacks action_chain do
|
190
|
+
run_callbacks format_chain do
|
191
|
+
yield if block_given?
|
192
|
+
end
|
193
|
+
end
|
194
|
+
else
|
195
|
+
run_callbacks action_chain do
|
196
|
+
yield if block_given?
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# :markup: markdown
|
4
|
+
|
5
|
+
require "mime_actor/errors"
|
6
|
+
|
7
|
+
module MimeActor
|
8
|
+
module Dispatcher
|
9
|
+
class MethodCall
|
10
|
+
attr_reader :method_name, :args
|
11
|
+
|
12
|
+
def initialize(method_name, *args)
|
13
|
+
@method_name = method_name
|
14
|
+
@args = args
|
15
|
+
validate!
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(target)
|
19
|
+
raise MimeActor::ActorNotFound, method_name unless target.respond_to?(method_name)
|
20
|
+
|
21
|
+
method_call = target.method(method_name)
|
22
|
+
filtered_args = method_call.arity.negative? ? args : args.take(method_call.arity)
|
23
|
+
method_call.call(*filtered_args)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def validate!
|
29
|
+
raise ArgumentError, "invalid method name: #{method_name.inspect}" unless method_name in String | Symbol
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class InstanceExec
|
34
|
+
attr_reader :block, :args
|
35
|
+
|
36
|
+
def initialize(block, *args)
|
37
|
+
@block = block
|
38
|
+
@args = args
|
39
|
+
validate!
|
40
|
+
end
|
41
|
+
|
42
|
+
def call(target)
|
43
|
+
filtered_args = block.arity.negative? ? args : args.take(block.arity)
|
44
|
+
target.instance_exec(*filtered_args, &block)
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def validate!
|
50
|
+
raise ArgumentError, "invalid block: #{block.inspect}" unless block in Proc
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.build(callable, *args)
|
55
|
+
case callable
|
56
|
+
when String, Symbol
|
57
|
+
MethodCall.new(callable, *args)
|
58
|
+
when Proc
|
59
|
+
InstanceExec.new(callable, *args)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/mime_actor/errors.rb
CHANGED
data/lib/mime_actor/rescue.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# :markup: markdown
|
4
4
|
|
5
|
-
require "mime_actor/
|
5
|
+
require "mime_actor/dispatcher"
|
6
6
|
require "mime_actor/validator"
|
7
7
|
|
8
8
|
require "active_support/concern"
|
@@ -22,7 +22,6 @@ module MimeActor
|
|
22
22
|
module Rescue
|
23
23
|
extend ActiveSupport::Concern
|
24
24
|
|
25
|
-
include Stage
|
26
25
|
include Validator
|
27
26
|
|
28
27
|
included do
|
@@ -50,19 +49,11 @@ module MimeActor
|
|
50
49
|
raise ArgumentError, "error filter is required" if klazzes.empty?
|
51
50
|
raise ArgumentError, "provide either the with: argument or a block" unless with.present? ^ block_given?
|
52
51
|
|
53
|
-
if
|
54
|
-
|
55
|
-
else
|
56
|
-
validate!(:with, with)
|
57
|
-
end
|
52
|
+
validate!(:with, with) if with.present?
|
53
|
+
with = block if block_given?
|
58
54
|
|
59
|
-
if action.present?
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
if format.present?
|
64
|
-
format.is_a?(Enumerable) ? validate!(:formats, format) : validate!(:format, format)
|
65
|
-
end
|
55
|
+
validate!(:action_or_actions, action) if action.present?
|
56
|
+
validate!(:format_or_formats, format) if format.present?
|
66
57
|
|
67
58
|
klazzes.each do |klazz|
|
68
59
|
validate!(:klazz, klazz)
|
@@ -139,5 +130,55 @@ module MimeActor
|
|
139
130
|
end
|
140
131
|
end
|
141
132
|
end
|
133
|
+
|
134
|
+
# Resolve the error provided with the registered handlers.
|
135
|
+
#
|
136
|
+
# The handled error will be returned to indicate successful handling.
|
137
|
+
#
|
138
|
+
# @param error the error instance to rescue
|
139
|
+
# @param action the `action` filter
|
140
|
+
# @param format the `format` filter
|
141
|
+
# @param visited the errors to skip after no rescue handler matched the filter
|
142
|
+
#
|
143
|
+
def rescue_actor(error, action:, format:, visited: [])
|
144
|
+
return if visited.include?(error)
|
145
|
+
|
146
|
+
visited << error
|
147
|
+
rescuer = find_rescuer(error, format:, action:)
|
148
|
+
if (dispatch = MimeActor::Dispatcher.build(rescuer, error, format, action))
|
149
|
+
dispatch.call(self)
|
150
|
+
error
|
151
|
+
elsif error&.cause
|
152
|
+
rescue_actor(error.cause, format:, action:, visited:)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
private
|
157
|
+
|
158
|
+
def find_rescuer(error, format:, action:)
|
159
|
+
return unless error
|
160
|
+
|
161
|
+
*_, rescuer = actor_rescuers.reverse_each.detect do |rescuee, format_filter, action_filter|
|
162
|
+
next if action_filter.present? && !Array.wrap(action_filter).include?(action)
|
163
|
+
next if format_filter.present? && !Array.wrap(format_filter).include?(format)
|
164
|
+
next unless (klazz = constantize_rescuee(rescuee))
|
165
|
+
|
166
|
+
error.is_a?(klazz)
|
167
|
+
end
|
168
|
+
rescuer
|
169
|
+
end
|
170
|
+
|
171
|
+
def constantize_rescuee(class_or_name)
|
172
|
+
case class_or_name
|
173
|
+
when String, Symbol
|
174
|
+
begin
|
175
|
+
const_get(class_or_name)
|
176
|
+
rescue NameError
|
177
|
+
class_or_name.safe_constantize
|
178
|
+
end
|
179
|
+
else
|
180
|
+
class_or_name
|
181
|
+
end
|
182
|
+
end
|
142
183
|
end
|
143
184
|
end
|
data/lib/mime_actor/scene.rb
CHANGED
@@ -6,7 +6,6 @@ require "mime_actor/errors"
|
|
6
6
|
require "mime_actor/validator"
|
7
7
|
|
8
8
|
require "active_support/concern"
|
9
|
-
require "active_support/core_ext/array/extract_options"
|
10
9
|
require "active_support/core_ext/array/wrap"
|
11
10
|
require "active_support/core_ext/module/attribute_accessors"
|
12
11
|
require "active_support/core_ext/object/blank"
|
@@ -87,26 +86,19 @@ module MimeActor
|
|
87
86
|
|
88
87
|
raise ArgumentError, "provide either the with: argument or a block" if with.present? && block_given?
|
89
88
|
|
90
|
-
if
|
91
|
-
|
92
|
-
elsif with.present?
|
93
|
-
validate!(:with, with)
|
94
|
-
end
|
89
|
+
validate!(:with, with) if with.present?
|
90
|
+
with = block if block_given?
|
95
91
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
when Symbol, String
|
100
|
-
validate!(:action, actions)
|
101
|
-
else
|
102
|
-
raise ArgumentError, "action is required"
|
103
|
-
end
|
92
|
+
raise ArgumentError, "action is required" unless (actions = on).present?
|
93
|
+
|
94
|
+
validate!(:action_or_actions, actions)
|
104
95
|
|
105
96
|
Array.wrap(actions).each do |action|
|
106
97
|
formats.each { |format| compose_scene(action, format, with) }
|
107
98
|
end
|
108
99
|
end
|
109
100
|
|
101
|
+
# TODO: remove on next breaking change release
|
110
102
|
alias act_on_format respond_act_to
|
111
103
|
|
112
104
|
private
|
@@ -122,7 +114,7 @@ module MimeActor
|
|
122
114
|
end
|
123
115
|
|
124
116
|
def define_scene(action)
|
125
|
-
|
117
|
+
module_eval(
|
126
118
|
# def index
|
127
119
|
# self.respond_to?(:start_scene) && self.start_scene(:index)
|
128
120
|
# end
|
data/lib/mime_actor/stage.rb
CHANGED
@@ -2,8 +2,9 @@
|
|
2
2
|
|
3
3
|
# :markup: markdown
|
4
4
|
|
5
|
-
require "mime_actor/
|
5
|
+
require "mime_actor/dispatcher"
|
6
6
|
require "mime_actor/logging"
|
7
|
+
require "mime_actor/rescue"
|
7
8
|
|
8
9
|
require "active_support/concern"
|
9
10
|
require "active_support/core_ext/module/attribute_accessors"
|
@@ -16,10 +17,11 @@ module MimeActor
|
|
16
17
|
module Stage
|
17
18
|
extend ActiveSupport::Concern
|
18
19
|
|
20
|
+
include Rescue
|
19
21
|
include Logging
|
20
22
|
|
21
23
|
included do
|
22
|
-
mattr_accessor :
|
24
|
+
mattr_accessor :raise_on_actor_error, instance_writer: false, default: false
|
23
25
|
end
|
24
26
|
|
25
27
|
module ClassMethods
|
@@ -46,13 +48,13 @@ module MimeActor
|
|
46
48
|
# @param block the `block` to be evaluated
|
47
49
|
#
|
48
50
|
# @example Dispatch a cue that prints out a text
|
49
|
-
# dispatch = self.class.
|
51
|
+
# dispatch = self.class.dispatch_act(action: :create, format: :json, context: self) do
|
50
52
|
# puts "completed the dispatch"
|
51
53
|
# end
|
52
54
|
#
|
53
55
|
# dispatch.call == "completed the dispatch" # true
|
54
56
|
#
|
55
|
-
def
|
57
|
+
def dispatch_act(action: nil, format: nil, context: self, &block)
|
56
58
|
raise ArgumentError, "block must be provided" unless block_given?
|
57
59
|
|
58
60
|
lambda do
|
@@ -61,6 +63,9 @@ module MimeActor
|
|
61
63
|
(respond_to?(:rescue_actor) && rescue_actor(e, action:, format:, context:)) || raise
|
62
64
|
end
|
63
65
|
end
|
66
|
+
|
67
|
+
# TODO: remove on next breaking change release
|
68
|
+
alias dispatch_cue dispatch_act
|
64
69
|
end
|
65
70
|
|
66
71
|
# Calls the `actor` and passing arguments to it.
|
@@ -68,40 +73,25 @@ module MimeActor
|
|
68
73
|
#
|
69
74
|
# NOTE: method call on actor if it is String or Symbol. Proc#call if actor is Proc
|
70
75
|
#
|
71
|
-
# @param actor either a method name or a
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
actor_proc_call(actor, *args)
|
78
|
-
else
|
79
|
-
raise TypeError, "invalid actor, got: #{actor.inspect}"
|
80
|
-
end
|
76
|
+
# @param actor either a method name or a Proc to evaluate
|
77
|
+
# @param args arguments to be passed when calling the actor
|
78
|
+
#
|
79
|
+
def cue_actor(actor, *args, action:, format:)
|
80
|
+
dispatcher = MimeActor::Dispatcher.build(actor, *args)
|
81
|
+
raise TypeError, "invalid actor, got: #{actor.inspect}" unless dispatcher
|
81
82
|
|
83
|
+
result = dispatcher.call(self)
|
82
84
|
if block_given?
|
83
85
|
yield result
|
84
86
|
else
|
85
87
|
result
|
86
88
|
end
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
raise MimeActor::ActorNotFound, actor_method if raise_on_missing_actor
|
94
|
-
|
95
|
-
logger.warn { "actor #{actor_method.inspect} not found" }
|
96
|
-
return
|
97
|
-
end
|
98
|
-
|
99
|
-
public_send(actor_method, *args)
|
100
|
-
end
|
101
|
-
|
102
|
-
def actor_proc_call(actor_proc, *args)
|
103
|
-
passable_args = actor_proc.arity.negative? ? args : args.take(actor_proc.arity)
|
104
|
-
instance_exec(*passable_args, &actor_proc)
|
89
|
+
rescue MimeActor::ActorNotFound => e
|
90
|
+
logger.error { "actor error, cause: #{e.inspect}" } unless raise_on_actor_error
|
91
|
+
raise e if raise_on_actor_error
|
92
|
+
rescue StandardError => e
|
93
|
+
rescued = rescue_actor(e, action:, format:)
|
94
|
+
rescued || raise
|
105
95
|
end
|
106
96
|
end
|
107
97
|
end
|
data/lib/mime_actor/validator.rb
CHANGED
@@ -32,7 +32,7 @@ module MimeActor
|
|
32
32
|
# @param rule the name of validator
|
33
33
|
def validate!(rule, *args)
|
34
34
|
validator = "validate_#{rule}"
|
35
|
-
raise NameError, "Validator not found, got: #{validator.inspect}" unless respond_to?(validator
|
35
|
+
raise NameError, "Validator not found, got: #{validator.inspect}" unless respond_to?(validator)
|
36
36
|
|
37
37
|
error = send(validator, *args)
|
38
38
|
raise error if error
|
@@ -53,6 +53,13 @@ module MimeActor
|
|
53
53
|
NameError.new("invalid actions, got: #{rejected.map(&:inspect).join(", ")}") if rejected.size.positive?
|
54
54
|
end
|
55
55
|
|
56
|
+
# Validate against `actions` rule if argument is a Enumerable. otherwise, validate against `action` rule.
|
57
|
+
#
|
58
|
+
# @param unchecked the `actions` or `action` to be validated
|
59
|
+
def validate_action_or_actions(unchecked)
|
60
|
+
unchecked.is_a?(Enumerable) ? validate_actions(unchecked) : validate_action(unchecked)
|
61
|
+
end
|
62
|
+
|
56
63
|
# Validate `format` must be a Symbol and a valid MIME type
|
57
64
|
#
|
58
65
|
# @param unchecked the `format` to be validated
|
@@ -73,6 +80,13 @@ module MimeActor
|
|
73
80
|
NameError.new("invalid formats, got: #{rejected.map(&:inspect).join(", ")}") if rejected.size.positive?
|
74
81
|
end
|
75
82
|
|
83
|
+
# Validate against `formats` rule if argument is a Enumerable. otherwise, validate against `format` rule.
|
84
|
+
#
|
85
|
+
# @param unchecked the `formats` or `format` to be validated
|
86
|
+
def validate_format_or_formats(unchecked)
|
87
|
+
unchecked.is_a?(Enumerable) ? validate_formats(unchecked) : validate_format(unchecked)
|
88
|
+
end
|
89
|
+
|
76
90
|
# Validate `klazz` must be a Class/Module or a String referencing a Class/Module
|
77
91
|
#
|
78
92
|
# @param unchecked the `klazz` to be validated
|
data/lib/mime_actor/version.rb
CHANGED
@@ -1,19 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# :markup: markdown
|
4
|
+
|
3
5
|
module MimeActor
|
4
6
|
def self.version
|
5
|
-
|
7
|
+
VERSION::STRING
|
6
8
|
end
|
7
9
|
|
8
10
|
def self.gem_version
|
9
|
-
Gem::Version.new
|
11
|
+
Gem::Version.new(VERSION::STRING)
|
10
12
|
end
|
11
13
|
|
12
14
|
module VERSION
|
13
15
|
MAJOR = 0
|
14
16
|
MINOR = 6
|
15
|
-
BUILD =
|
16
|
-
PRE =
|
17
|
+
BUILD = 3
|
18
|
+
PRE = "alpha"
|
17
19
|
|
18
20
|
STRING = [MAJOR, MINOR, BUILD, PRE].compact.join(".")
|
19
21
|
end
|
data/lib/mime_actor.rb
CHANGED
@@ -7,6 +7,7 @@ require "mime_actor/version"
|
|
7
7
|
require "mime_actor/errors"
|
8
8
|
|
9
9
|
require "active_support/dependencies/autoload"
|
10
|
+
require "active_support/deprecation"
|
10
11
|
|
11
12
|
module MimeActor
|
12
13
|
extend ActiveSupport::Autoload
|
@@ -17,4 +18,18 @@ module MimeActor
|
|
17
18
|
autoload :Rescue
|
18
19
|
autoload :Validator
|
19
20
|
autoload :Logging
|
21
|
+
|
22
|
+
def self.deprecator
|
23
|
+
@deprecator ||= ActiveSupport::Deprecation.new("0.7.0", "MimeActor")
|
24
|
+
end
|
25
|
+
|
26
|
+
[
|
27
|
+
[MimeActor::Rescue::ClassMethods, { rescue_actor: "use #rescue_actor instance method" }],
|
28
|
+
[MimeActor::Scene::ClassMethods, { act_on_format: :respond_act_to }],
|
29
|
+
[MimeActor::Stage::ClassMethods, { actor?: "no longer supported, use Object#respond_to?" }],
|
30
|
+
[MimeActor::Stage::ClassMethods, { dispatch_cue: "no longer support anonymous proc with rescue" }],
|
31
|
+
[MimeActor::Stage::ClassMethods, { dispatch_act: "no longer support anonymous proc with rescue" }]
|
32
|
+
].each do |klazz, *args|
|
33
|
+
deprecator.deprecate_methods(klazz, *args)
|
34
|
+
end
|
20
35
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mime_actor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.3.alpha
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Chang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-07-
|
11
|
+
date: 2024-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -71,6 +71,8 @@ files:
|
|
71
71
|
- codecov.yml
|
72
72
|
- lib/mime_actor.rb
|
73
73
|
- lib/mime_actor/action.rb
|
74
|
+
- lib/mime_actor/callbacks.rb
|
75
|
+
- lib/mime_actor/dispatcher.rb
|
74
76
|
- lib/mime_actor/errors.rb
|
75
77
|
- lib/mime_actor/logging.rb
|
76
78
|
- lib/mime_actor/rescue.rb
|