mime_actor 1.0.0 → 1.1.0
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/COMPARE.md +14 -11
- data/README.md +92 -14
- data/lib/mime_actor/action.rb +76 -43
- data/lib/mime_actor/callbacks.rb +89 -126
- data/lib/mime_actor/errors.rb +0 -15
- data/lib/mime_actor/rescue.rb +41 -40
- data/lib/mime_actor/scene.rb +68 -72
- data/lib/mime_actor/validator.rb +81 -74
- data/lib/mime_actor/version.rb +1 -1
- data/lib/mime_actor.rb +5 -18
- metadata +3 -11
- data/lib/mime_actor/deprecator.rb +0 -11
- data/lib/mime_actor/dispatcher.rb +0 -65
- data/lib/mime_actor/logging.rb +0 -44
- data/lib/mime_actor/railtie.rb +0 -11
- data/lib/mime_actor/stage.rb +0 -55
data/lib/mime_actor/callbacks.rb
CHANGED
|
@@ -6,8 +6,6 @@ require "mime_actor/validator"
|
|
|
6
6
|
|
|
7
7
|
require "active_support/callbacks"
|
|
8
8
|
require "active_support/concern"
|
|
9
|
-
require "active_support/core_ext/module/attr_internal"
|
|
10
|
-
require "active_support/version"
|
|
11
9
|
|
|
12
10
|
module MimeActor
|
|
13
11
|
# # MimeActor Callbacks
|
|
@@ -24,21 +22,87 @@ module MimeActor
|
|
|
24
22
|
# - prepend_act_around
|
|
25
23
|
# - prepend_act_after
|
|
26
24
|
#
|
|
25
|
+
# An `act_before` halts the act once it responds (render, redirect or head) or throws `:abort`,
|
|
26
|
+
# the remaining callbacks and the act are skipped.
|
|
27
|
+
#
|
|
27
28
|
# NOTE: Calling the same callback multiple times will overwrite previous callback definitions.
|
|
28
29
|
#
|
|
30
|
+
# Callbacks invocation sequence depends on the order of callback definition.
|
|
31
|
+
#
|
|
32
|
+
# @example callbacks with/without action filter
|
|
33
|
+
# act_before :my_act_before_one
|
|
34
|
+
# act_before :my_act_before_two, action: :create
|
|
35
|
+
# act_before :my_act_before_three
|
|
36
|
+
#
|
|
37
|
+
# act_around :my_act_around_one
|
|
38
|
+
# act_around :my_act_around_two, action: :create
|
|
39
|
+
# act_around :my_act_around_three
|
|
40
|
+
#
|
|
41
|
+
# act_after :my_act_after_one
|
|
42
|
+
# act_after :my_act_after_two, action: :create
|
|
43
|
+
# act_after :my_act_after_three
|
|
44
|
+
#
|
|
45
|
+
# # actual sequence:
|
|
46
|
+
# # - my_act_before_one
|
|
47
|
+
# # - my_act_before_two
|
|
48
|
+
# # - my_act_before_three
|
|
49
|
+
# # - my_act_around_one
|
|
50
|
+
# # - my_act_around_two
|
|
51
|
+
# # - my_act_around_three
|
|
52
|
+
# # - my_act_after_three
|
|
53
|
+
# # - my_act_after_two
|
|
54
|
+
# # - my_act_after_one
|
|
55
|
+
#
|
|
56
|
+
# @example callbacks with format filter
|
|
57
|
+
# act_before :my_act_before_one
|
|
58
|
+
# act_before :my_act_before_two, action: :create
|
|
59
|
+
# act_before :my_act_before_three, action: :create, format: :html
|
|
60
|
+
# act_before :my_act_before_four
|
|
61
|
+
#
|
|
62
|
+
# act_around :my_act_around_one
|
|
63
|
+
# act_around :my_act_around_two, action: :create, format: :html
|
|
64
|
+
# act_around :my_act_around_three, action: :create
|
|
65
|
+
# act_around :my_act_around_four
|
|
66
|
+
#
|
|
67
|
+
# act_after :my_act_after_one, format: :html
|
|
68
|
+
# act_after :my_act_after_two
|
|
69
|
+
# act_after :my_act_after_three, action: :create
|
|
70
|
+
# act_after :my_act_after_four
|
|
71
|
+
#
|
|
72
|
+
# # actual sequence:
|
|
73
|
+
# # - my_act_before_one
|
|
74
|
+
# # - my_act_before_two
|
|
75
|
+
# # - my_act_before_four
|
|
76
|
+
# # - my_act_before_three
|
|
77
|
+
# # - my_act_around_one
|
|
78
|
+
# # - my_act_around_two
|
|
79
|
+
# # - my_act_around_three
|
|
80
|
+
# # - my_act_around_four
|
|
81
|
+
# # - my_act_after_four
|
|
82
|
+
# # - my_act_after_three
|
|
83
|
+
# # - my_act_after_two
|
|
84
|
+
# # - my_act_after_one
|
|
85
|
+
#
|
|
29
86
|
module Callbacks
|
|
30
87
|
extend ActiveSupport::Concern
|
|
31
88
|
|
|
32
89
|
include ActiveSupport::Callbacks
|
|
33
|
-
include MimeActor::Validator
|
|
34
90
|
|
|
35
91
|
LIFECYCLES = %i[before after around].freeze
|
|
36
92
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
93
|
+
TERMINATOR = lambda do |controller, result_lambda|
|
|
94
|
+
halted = true
|
|
95
|
+
catch(:abort) do
|
|
96
|
+
result_lambda.call
|
|
97
|
+
halted = false
|
|
98
|
+
end
|
|
99
|
+
halted || controller.performed?
|
|
100
|
+
end
|
|
40
101
|
|
|
41
|
-
|
|
102
|
+
included do
|
|
103
|
+
define_callbacks :act, terminator: TERMINATOR, skip_after_callbacks_if_terminated: true
|
|
104
|
+
# keep the callback runners out of `action_methods`
|
|
105
|
+
%i[_run_act_callbacks _run_act_callbacks! _act_callbacks].each { |name| private name if method_defined?(name) }
|
|
42
106
|
end
|
|
43
107
|
|
|
44
108
|
module ClassMethods
|
|
@@ -46,19 +110,14 @@ module MimeActor
|
|
|
46
110
|
attr_reader :actions, :formats
|
|
47
111
|
|
|
48
112
|
def initialize(actions, formats)
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
@formats = formats ? Array(formats).to_set(&:to_sym) : nil
|
|
52
|
-
else
|
|
53
|
-
@actions = actions&.then { |a| Array(a).to_set(&:to_sym) }
|
|
54
|
-
@formats = formats&.then { |f| Array(f).to_set(&:to_sym) }
|
|
55
|
-
end
|
|
113
|
+
@actions = actions && Array(actions).to_set(&:to_sym)
|
|
114
|
+
@formats = formats && Array(formats).to_set(&:to_sym)
|
|
56
115
|
end
|
|
57
116
|
|
|
58
117
|
def match?(controller)
|
|
59
118
|
matched = true
|
|
60
|
-
matched &= actions.include?(controller.act_action)
|
|
61
|
-
matched &= formats.include?(controller.act_format)
|
|
119
|
+
matched &= actions.include?(controller.send(:act_action)) unless actions.nil?
|
|
120
|
+
matched &= formats.include?(controller.send(:act_format)) unless formats.nil?
|
|
62
121
|
matched
|
|
63
122
|
end
|
|
64
123
|
|
|
@@ -67,6 +126,18 @@ module MimeActor
|
|
|
67
126
|
alias around match?
|
|
68
127
|
end
|
|
69
128
|
|
|
129
|
+
LIFECYCLES.each do |kind|
|
|
130
|
+
{ append: false, prepend: true }.each do |type, prepend|
|
|
131
|
+
define_method(:"#{type}_act_#{kind}") do |*callbacks, action: nil, format: nil, &block|
|
|
132
|
+
validate_callback_options!(action, format)
|
|
133
|
+
configure_callbacks(callbacks, action, format, block) do |callback, options|
|
|
134
|
+
set_callback(:act, kind, callback, options.merge!(prepend: prepend))
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
alias_method :"act_#{kind}", :"append_act_#{kind}"
|
|
139
|
+
end
|
|
140
|
+
|
|
70
141
|
private
|
|
71
142
|
|
|
72
143
|
def configure_callbacks(callbacks, actions, formats, block)
|
|
@@ -80,116 +151,8 @@ module MimeActor
|
|
|
80
151
|
end
|
|
81
152
|
|
|
82
153
|
def validate_callback_options!(action, format)
|
|
83
|
-
validate!(:action_or_actions, action) unless action.nil?
|
|
84
|
-
validate!(:format_or_formats, format) unless format.nil?
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
def eval_act_callback_methods
|
|
88
|
-
singleton_class.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
|
89
|
-
# def append_act_before(*callbacks, action: nil, format: nil, &block)
|
|
90
|
-
# validate_callback_options!(action, format)
|
|
91
|
-
# configure_callbacks(callbacks, action, format, block) do |callback, options|
|
|
92
|
-
# set_callback(:act, :before, callback, options.merge!(prepend: false))
|
|
93
|
-
# end
|
|
94
|
-
# end
|
|
95
|
-
#
|
|
96
|
-
# alias_method :act_before, :append_act_before
|
|
97
|
-
#
|
|
98
|
-
#{LIFECYCLES.map { |kind| act_callback_kind_template(kind, append: true) }.join(";")}
|
|
99
|
-
#{LIFECYCLES.map { |kind| act_callback_kind_template(kind, append: false) }.join(";")}
|
|
100
|
-
#{LIFECYCLES.map { |kind| "alias_method :act_#{kind}, :append_act_#{kind}" }.join(";")}
|
|
101
|
-
RUBY
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
def generate_act_callback_methods
|
|
105
|
-
require "active_support/code_generator"
|
|
106
|
-
ActiveSupport::CodeGenerator.batch(singleton_class, __FILE__, __LINE__) do |owner|
|
|
107
|
-
LIFECYCLES.each do |kind|
|
|
108
|
-
owner.define_cached_method(:"append_act_#{kind}", as: :"act_#{kind}", namespace: :mime_callbacks) do |batch|
|
|
109
|
-
batch << act_callback_kind_template(kind, append: true)
|
|
110
|
-
end
|
|
111
|
-
owner.define_cached_method(:"prepend_act_#{kind}", namespace: :mime_callbacks) do |batch|
|
|
112
|
-
batch << act_callback_kind_template(kind, append: false)
|
|
113
|
-
end
|
|
114
|
-
end
|
|
115
|
-
end
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
def act_callback_kind_template(kind, append:)
|
|
119
|
-
type = append ? "append" : "prepend"
|
|
120
|
-
<<-RUBY
|
|
121
|
-
def #{type}_act_#{kind}(*callbacks, action: nil, format: nil, &block)
|
|
122
|
-
validate_callback_options!(action, format)
|
|
123
|
-
configure_callbacks(callbacks, action, format, block) do |callback, options|
|
|
124
|
-
set_callback(:act, :#{kind}, callback, options.merge!(prepend: #{!append}))
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
RUBY
|
|
128
|
-
end
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
# Callbacks invocation sequence depends on the order of callback definition.
|
|
132
|
-
#
|
|
133
|
-
# @example callbacks with/without action filter
|
|
134
|
-
# act_before :my_act_before_one
|
|
135
|
-
# act_before :my_act_before_two, action: :create
|
|
136
|
-
# act_before :my_act_before_three
|
|
137
|
-
#
|
|
138
|
-
# act_around :my_act_around_one
|
|
139
|
-
# act_around :my_act_around_two, action: :create
|
|
140
|
-
# act_around :my_act_around_three
|
|
141
|
-
#
|
|
142
|
-
# act_after :my_act_after_one
|
|
143
|
-
# act_after :my_act_after_two, action: :create
|
|
144
|
-
# act_after :my_act_after_three
|
|
145
|
-
#
|
|
146
|
-
# # actual sequence:
|
|
147
|
-
# # - my_act_before_one
|
|
148
|
-
# # - my_act_before_two
|
|
149
|
-
# # - my_act_before_three
|
|
150
|
-
# # - my_act_around_one
|
|
151
|
-
# # - my_act_around_two
|
|
152
|
-
# # - my_act_around_three
|
|
153
|
-
# # - my_act_after_three
|
|
154
|
-
# # - my_act_after_two
|
|
155
|
-
# # - my_act_after_one
|
|
156
|
-
#
|
|
157
|
-
# @example callbacks with format filter
|
|
158
|
-
# act_before :my_act_before_one
|
|
159
|
-
# act_before :my_act_before_two, action: :create
|
|
160
|
-
# act_before :my_act_before_three, action: :create, format: :html
|
|
161
|
-
# act_before :my_act_before_four
|
|
162
|
-
#
|
|
163
|
-
# act_around :my_act_around_one
|
|
164
|
-
# act_around :my_act_around_two, action: :create, format: :html
|
|
165
|
-
# act_around :my_act_around_three, action: :create
|
|
166
|
-
# act_around :my_act_around_four
|
|
167
|
-
#
|
|
168
|
-
# act_after :my_act_after_one, format: :html
|
|
169
|
-
# act_after :my_act_after_two
|
|
170
|
-
# act_after :my_act_after_three, action: :create
|
|
171
|
-
# act_after :my_act_after_four
|
|
172
|
-
#
|
|
173
|
-
# # actual sequence:
|
|
174
|
-
# # - my_act_before_one
|
|
175
|
-
# # - my_act_before_two
|
|
176
|
-
# # - my_act_before_four
|
|
177
|
-
# # - my_act_before_three
|
|
178
|
-
# # - my_act_around_one
|
|
179
|
-
# # - my_act_around_two
|
|
180
|
-
# # - my_act_around_three
|
|
181
|
-
# # - my_act_around_four
|
|
182
|
-
# # - my_act_after_four
|
|
183
|
-
# # - my_act_after_three
|
|
184
|
-
# # - my_act_after_two
|
|
185
|
-
# # - my_act_after_one
|
|
186
|
-
#
|
|
187
|
-
def run_act_callbacks(action:, format:)
|
|
188
|
-
@_act_action = action.to_sym
|
|
189
|
-
@_act_format = format.to_sym
|
|
190
|
-
|
|
191
|
-
run_callbacks :act do
|
|
192
|
-
yield if block_given?
|
|
154
|
+
Validator.validate!(:action_or_actions, action) unless action.nil?
|
|
155
|
+
Validator.validate!(:format_or_formats, format) unless format.nil?
|
|
193
156
|
end
|
|
194
157
|
end
|
|
195
158
|
end
|
data/lib/mime_actor/errors.rb
CHANGED
|
@@ -29,19 +29,4 @@ module MimeActor
|
|
|
29
29
|
"#{actor.inspect} not found"
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
|
-
|
|
33
|
-
class ActionError < Error
|
|
34
|
-
attr_reader :action
|
|
35
|
-
|
|
36
|
-
def initialize(action = nil)
|
|
37
|
-
@action = action
|
|
38
|
-
super(generate_message)
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
class ActionNotImplemented < ActionError
|
|
43
|
-
def generate_message
|
|
44
|
-
"action #{action.inspect} not implemented"
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
32
|
end
|
data/lib/mime_actor/rescue.rb
CHANGED
|
@@ -2,17 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
# :markup: markdown
|
|
4
4
|
|
|
5
|
-
require "mime_actor/
|
|
5
|
+
require "mime_actor/errors"
|
|
6
6
|
require "mime_actor/validator"
|
|
7
7
|
|
|
8
8
|
require "active_support/concern"
|
|
9
|
-
require "active_support/core_ext/
|
|
9
|
+
require "active_support/core_ext/class/attribute"
|
|
10
10
|
require "active_support/core_ext/string/inflections"
|
|
11
11
|
|
|
12
12
|
module MimeActor
|
|
13
13
|
# # MimeActor Rescue
|
|
14
14
|
#
|
|
15
|
-
#
|
|
15
|
+
# Similar to `ActionController::Rescue` but with additional filtering logic on `action`/`format`.
|
|
16
16
|
#
|
|
17
17
|
# @example Rescue RuntimeError when raised for any action with `json` format
|
|
18
18
|
# rescue_act_from RuntimeError, format: :json, with: :handle_json_error
|
|
@@ -20,10 +20,8 @@ module MimeActor
|
|
|
20
20
|
module Rescue
|
|
21
21
|
extend ActiveSupport::Concern
|
|
22
22
|
|
|
23
|
-
include Validator
|
|
24
|
-
|
|
25
23
|
included do
|
|
26
|
-
|
|
24
|
+
class_attribute :actor_rescuers, instance_accessor: false, instance_predicate: false, default: [].freeze
|
|
27
25
|
end
|
|
28
26
|
|
|
29
27
|
module ClassMethods
|
|
@@ -47,52 +45,55 @@ module MimeActor
|
|
|
47
45
|
raise ArgumentError, "error filter is required" if klazzes.empty?
|
|
48
46
|
raise ArgumentError, "provide either with: or a block" unless !with.nil? ^ block_given?
|
|
49
47
|
|
|
50
|
-
validate!(:callable, with) unless with.nil?
|
|
48
|
+
Validator.validate!(:callable, with, 1) unless with.nil?
|
|
51
49
|
with = block if block_given?
|
|
52
50
|
|
|
53
|
-
validate!(:action_or_actions, action) unless action.nil?
|
|
54
|
-
validate!(:format_or_formats, format) unless format.nil?
|
|
51
|
+
Validator.validate!(:action_or_actions, action) unless action.nil?
|
|
52
|
+
Validator.validate!(:format_or_formats, format) unless format.nil?
|
|
55
53
|
|
|
56
|
-
klazzes.
|
|
57
|
-
validate!(:klazz, klazz)
|
|
54
|
+
rescuers = klazzes.map do |klazz|
|
|
55
|
+
Validator.validate!(:klazz, klazz)
|
|
58
56
|
error = klazz.is_a?(Module) ? klazz.name : klazz
|
|
59
|
-
|
|
60
|
-
# append at the end because strategies are read in reverse.
|
|
61
|
-
actor_rescuers << [error, format, action, with]
|
|
57
|
+
[error, format, action, with]
|
|
62
58
|
end
|
|
59
|
+
|
|
60
|
+
# append at the end because strategies are read in reverse.
|
|
61
|
+
# copy-on-write, registrations must not leak into the parent class or sibling classes
|
|
62
|
+
self.actor_rescuers = (actor_rescuers + rescuers).freeze
|
|
63
63
|
end
|
|
64
64
|
end
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
#
|
|
69
|
-
#
|
|
70
|
-
# @param error the error instance to rescue
|
|
71
|
-
# @param action the `action` filter
|
|
72
|
-
# @param format the `format` filter
|
|
73
|
-
# @param visited the errors to skip after no rescue handler matched the filter
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
# Rescues the error, or else its causes, with the most recently registered matching handler.
|
|
74
69
|
#
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
rescuer = find_rescuer(error
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
rescue_actor(error.cause, format: format, action: action, visited: visited)
|
|
85
|
-
end
|
|
70
|
+
# Returns the rescued error, `nil` when no handler matched.
|
|
71
|
+
def rescue_act(error, visited = Set.new)
|
|
72
|
+
return if error.nil? || !visited.add?(error)
|
|
73
|
+
|
|
74
|
+
rescuer = find_rescuer(error)
|
|
75
|
+
return rescue_act(error.cause, visited) unless rescuer
|
|
76
|
+
|
|
77
|
+
call_rescuer(rescuer, error)
|
|
78
|
+
error
|
|
86
79
|
end
|
|
87
80
|
|
|
88
|
-
|
|
81
|
+
# Calls the handler with the error unless it takes no arguments.
|
|
82
|
+
def call_rescuer(rescuer, error)
|
|
83
|
+
if rescuer.is_a?(Proc)
|
|
84
|
+
rescuer.arity.zero? ? instance_exec(&rescuer) : instance_exec(error, &rescuer)
|
|
85
|
+
else
|
|
86
|
+
raise MimeActor::ActorNotFound, rescuer unless respond_to?(rescuer, true)
|
|
89
87
|
|
|
90
|
-
|
|
91
|
-
|
|
88
|
+
handler = method(rescuer)
|
|
89
|
+
handler.arity.zero? ? handler.call : handler.call(error)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
next unless
|
|
93
|
+
def find_rescuer(error)
|
|
94
|
+
*_, rescuer = self.class.actor_rescuers.reverse_each.detect do |rescuee, format_filter, action_filter|
|
|
95
|
+
next unless action_filter.nil? || Array(action_filter).include?(act_action)
|
|
96
|
+
next unless format_filter.nil? || Array(format_filter).include?(act_format)
|
|
96
97
|
next unless (klazz = constantize_rescuee(rescuee))
|
|
97
98
|
|
|
98
99
|
error.is_a?(klazz)
|
|
@@ -104,7 +105,7 @@ module MimeActor
|
|
|
104
105
|
case class_or_name
|
|
105
106
|
when String, Symbol
|
|
106
107
|
begin
|
|
107
|
-
const_get(class_or_name)
|
|
108
|
+
self.class.const_get(class_or_name)
|
|
108
109
|
rescue NameError
|
|
109
110
|
class_or_name.safe_constantize
|
|
110
111
|
end
|
data/lib/mime_actor/scene.rb
CHANGED
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
# :markup: markdown
|
|
4
4
|
|
|
5
|
-
require "mime_actor/errors"
|
|
6
5
|
require "mime_actor/validator"
|
|
7
6
|
|
|
7
|
+
require "abstract_controller"
|
|
8
8
|
require "active_support/concern"
|
|
9
|
-
require "active_support/core_ext/
|
|
10
|
-
require "active_support/version"
|
|
9
|
+
require "active_support/core_ext/class/attribute"
|
|
11
10
|
|
|
12
11
|
module MimeActor
|
|
13
12
|
# # MimeActor Scene
|
|
@@ -27,7 +26,7 @@ module MimeActor
|
|
|
27
26
|
# act_on_action :index, format: :html, with: -> { render html: "<h1>my header</h1>" }
|
|
28
27
|
#
|
|
29
28
|
# @example register a `html` format on action `index` with respond handler block
|
|
30
|
-
# act_on_action :
|
|
29
|
+
# act_on_action :index, format: :html do
|
|
31
30
|
# render :index
|
|
32
31
|
# end
|
|
33
32
|
#
|
|
@@ -36,10 +35,10 @@ module MimeActor
|
|
|
36
35
|
module Scene
|
|
37
36
|
extend ActiveSupport::Concern
|
|
38
37
|
|
|
39
|
-
|
|
38
|
+
class ActionMethods < Module; end
|
|
40
39
|
|
|
41
40
|
included do
|
|
42
|
-
|
|
41
|
+
class_attribute :acting_scenes, instance_accessor: false, instance_predicate: false, default: {}.freeze
|
|
43
42
|
end
|
|
44
43
|
|
|
45
44
|
module ClassMethods
|
|
@@ -48,111 +47,108 @@ module MimeActor
|
|
|
48
47
|
# @param action a collection of `action`
|
|
49
48
|
# @param format a single `format` or a collection of `format`
|
|
50
49
|
# @param with the respond handler for the given `action` + `format`
|
|
51
|
-
# @param block the `block` to be
|
|
50
|
+
# @param block the `block` to be yielded for the given `action` + `format`
|
|
52
51
|
#
|
|
53
52
|
# @example register a `html` format on action `create`
|
|
54
53
|
# act_on_action :create, format: :html
|
|
55
54
|
#
|
|
56
|
-
# # an action method will be defined in the class
|
|
57
|
-
# def indexl; end
|
|
58
55
|
# @example register `html`, `json` formats on actions `index`, `show`
|
|
59
56
|
# act_on_action :index, :show, format: [:html, :json]
|
|
60
57
|
#
|
|
61
|
-
# # these action methods will be defined in the class
|
|
62
|
-
# def index; end
|
|
63
|
-
# def show; end
|
|
64
58
|
# @example register a `html` format on action `index` with respond handler method
|
|
65
59
|
# act_on_action :index, format: :html, with: :render_html
|
|
66
60
|
#
|
|
67
|
-
# # an action method will be defined in the class
|
|
68
|
-
# # the handler method will be called by the action method.
|
|
69
|
-
# def index; end
|
|
70
61
|
# # the given method should be defined in the class
|
|
71
62
|
# def render_html; end
|
|
63
|
+
#
|
|
72
64
|
# @example register a `html` format on action `index` with respond handler Proc
|
|
73
65
|
# act_on_action :index, format: :html, with: -> { render html: "<h1>my header</h1>" }
|
|
74
66
|
#
|
|
75
|
-
# # an action method will be defined in the class,
|
|
76
|
-
# # the handler Proc will be called by the action method.
|
|
77
|
-
# def index; end
|
|
78
67
|
# @example register a `html` format on action `index` with respond handler block
|
|
79
|
-
# act_on_action :
|
|
68
|
+
# act_on_action :index, format: :html do
|
|
80
69
|
# render :index
|
|
81
70
|
# end
|
|
82
71
|
#
|
|
83
|
-
#
|
|
84
|
-
# def index; end
|
|
85
|
-
#
|
|
86
|
-
# For each unique `action` being registered, a corresponding `action` method will be defined.
|
|
72
|
+
# Each `action` gets a base method, a method defined in the class or its ancestors takes precedence.
|
|
87
73
|
def act_on_action(*actions, format:, with: nil, &block)
|
|
88
74
|
raise ArgumentError, "format is required" if format.nil?
|
|
89
75
|
raise ArgumentError, "provide either with: or a block" if !with.nil? && block_given?
|
|
90
76
|
|
|
91
|
-
validate!(:actions, actions)
|
|
92
|
-
validate!(:format_or_formats, format)
|
|
93
|
-
validate!(:callable, with) unless with.nil?
|
|
77
|
+
Validator.validate!(:actions, actions)
|
|
78
|
+
Validator.validate!(:format_or_formats, format)
|
|
79
|
+
Validator.validate!(:callable, with) unless with.nil?
|
|
94
80
|
with = block if block_given?
|
|
95
81
|
|
|
82
|
+
register_acting_scenes(actions, format, with)
|
|
83
|
+
define_action_methods(actions)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
# copy-on-write, registrations must not leak into the parent class or sibling classes
|
|
89
|
+
def register_acting_scenes(actions, format, with)
|
|
90
|
+
scenes = acting_scenes.dup
|
|
96
91
|
actions.each do |action|
|
|
97
|
-
|
|
92
|
+
action_formats = scenes.fetch(action, {}).dup
|
|
98
93
|
Array(format).each do |action_format|
|
|
99
|
-
|
|
94
|
+
action_formats[action_format] = with
|
|
100
95
|
end
|
|
96
|
+
scenes[action] = action_formats.freeze
|
|
101
97
|
end
|
|
102
|
-
|
|
103
|
-
ActiveSupport.version >= "7.2" ? generate_action_methods(actions) : eval_action_methods(actions)
|
|
98
|
+
self.acting_scenes = scenes.freeze
|
|
104
99
|
end
|
|
105
100
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
@generated_action_methods ||= Module.new.tap { |mod| prepend mod }
|
|
101
|
+
# included rather than prepended, a method defined in the class wins
|
|
102
|
+
def mime_actor_action_methods
|
|
103
|
+
@mime_actor_action_methods ||= ActionMethods.new.tap { |mod| include mod }
|
|
110
104
|
end
|
|
111
105
|
|
|
112
|
-
def
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
# if respond_to?(:start_scene)
|
|
116
|
-
# start_scene do
|
|
117
|
-
# raise MimeActor::ActionNotImplemented, :index unless defined?(super)
|
|
118
|
-
# super
|
|
119
|
-
# end
|
|
120
|
-
# else
|
|
121
|
-
# raise MimeActor::ActionNotImplemented}, :index unless defined?(super)
|
|
122
|
-
# super
|
|
123
|
-
# end
|
|
124
|
-
# end
|
|
125
|
-
#
|
|
126
|
-
#{actions.each.map { |action| action_method_template(action) }.join(";")}
|
|
127
|
-
RUBY
|
|
128
|
-
end
|
|
106
|
+
def define_action_methods(actions)
|
|
107
|
+
actions.each do |action|
|
|
108
|
+
next if mime_actor_action_methods.method_defined?(action)
|
|
129
109
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
110
|
+
mime_actor_action_methods.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
|
111
|
+
# def create
|
|
112
|
+
# return super if defined?(super)
|
|
113
|
+
#
|
|
114
|
+
# raise action_not_found(:create)
|
|
115
|
+
# end
|
|
116
|
+
def #{action}
|
|
117
|
+
return super if defined?(super)
|
|
118
|
+
|
|
119
|
+
raise action_not_found(:#{action})
|
|
136
120
|
end
|
|
137
|
-
|
|
121
|
+
RUBY
|
|
138
122
|
end
|
|
123
|
+
# module methods skip `method_added`, which resets the cache in controllers
|
|
124
|
+
clear_action_methods! if respond_to?(:clear_action_methods!)
|
|
139
125
|
end
|
|
126
|
+
end
|
|
140
127
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
end
|
|
153
|
-
end
|
|
154
|
-
RUBY
|
|
128
|
+
private
|
|
129
|
+
|
|
130
|
+
# Raises when a handler of the `action` is missing, before the act callbacks can run or rescue it.
|
|
131
|
+
def verify_scene!(action)
|
|
132
|
+
self.class.acting_scenes.fetch(action).each_value do |actor|
|
|
133
|
+
case actor
|
|
134
|
+
when Symbol
|
|
135
|
+
raise MimeActor::ActorNotFound, actor unless respond_to?(actor, true)
|
|
136
|
+
when nil
|
|
137
|
+
raise action_not_found(action) unless action_method_defined?(action)
|
|
138
|
+
end
|
|
155
139
|
end
|
|
156
140
|
end
|
|
141
|
+
|
|
142
|
+
# whether a method other than the base `action` method is defined
|
|
143
|
+
def action_method_defined?(action)
|
|
144
|
+
method = self.class.instance_method(action)
|
|
145
|
+
method = method.super_method while method && method.owner.is_a?(ActionMethods)
|
|
146
|
+
!method.nil?
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def action_not_found(action)
|
|
150
|
+
message = "The action '#{action}' has no method or format handler for #{self.class.name}"
|
|
151
|
+
AbstractController::ActionNotFound.new(message, self, action.to_s)
|
|
152
|
+
end
|
|
157
153
|
end
|
|
158
154
|
end
|