mime_actor 1.0.1 → 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 +88 -10
- 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 +2 -2
- data/lib/mime_actor.rb +5 -18
- metadata +3 -8
- data/lib/mime_actor/deprecator.rb +0 -11
- data/lib/mime_actor/dispatcher.rb +0 -65
- data/lib/mime_actor/logging.rb +0 -46
- data/lib/mime_actor/railtie.rb +0 -11
- data/lib/mime_actor/stage.rb +0 -55
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.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
|
data/lib/mime_actor/validator.rb
CHANGED
|
@@ -2,112 +2,119 @@
|
|
|
2
2
|
|
|
3
3
|
# :markup: markdown
|
|
4
4
|
|
|
5
|
-
require "active_support/concern"
|
|
6
5
|
require "active_support/core_ext/object/blank"
|
|
7
6
|
require "set" # required by mime_type with ruby <= 3.1
|
|
8
7
|
require "action_dispatch/http/mime_type"
|
|
8
|
+
require "action_pack/version"
|
|
9
9
|
|
|
10
10
|
module MimeActor
|
|
11
11
|
# # MimeActor Validator
|
|
12
12
|
#
|
|
13
|
-
# Validator provides validation rules for `action`, `format`, and `with` handlers
|
|
13
|
+
# Validator provides validation rules for `action`, `format`, and `with` handlers.
|
|
14
14
|
#
|
|
15
15
|
# @example Raise error when rule is violated
|
|
16
|
-
# validate!(:action, action_param)
|
|
16
|
+
# MimeActor::Validator.validate!(:action, action_param)
|
|
17
17
|
#
|
|
18
18
|
# @example Return the error when rule is violated
|
|
19
|
-
#
|
|
20
|
-
# raise
|
|
19
|
+
# error = MimeActor::Validator.validate_action(action_param)
|
|
20
|
+
# raise error if error
|
|
21
21
|
#
|
|
22
22
|
module Validator
|
|
23
|
-
|
|
23
|
+
module_function
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
# Raise the error returned by validator if any.
|
|
26
|
+
#
|
|
27
|
+
# @param rule the name of validator
|
|
28
|
+
def validate!(rule, *args)
|
|
29
|
+
validator = "validate_#{rule}"
|
|
30
|
+
raise NameError, "Validator not found, got: #{validator.inspect}" unless respond_to?(validator)
|
|
31
|
+
|
|
32
|
+
error = public_send(validator, *args)
|
|
33
|
+
raise error if error
|
|
27
34
|
end
|
|
28
35
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
raise NameError, "Validator not found, got: #{validator.inspect}" unless respond_to?(validator)
|
|
36
|
+
# Validate `action` must be a Symbol
|
|
37
|
+
#
|
|
38
|
+
# @param unchecked the `action` to be validated
|
|
39
|
+
def validate_action(unchecked)
|
|
40
|
+
TypeError.new("action must be a Symbol") unless unchecked.is_a?(Symbol)
|
|
41
|
+
end
|
|
36
42
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
43
|
+
# Validate `actions` must be a collection of Symbol
|
|
44
|
+
#
|
|
45
|
+
# @param unchecked the `actions` to be validated
|
|
46
|
+
def validate_actions(unchecked)
|
|
47
|
+
return TypeError.new("actions must not be empty") if unchecked.empty?
|
|
40
48
|
|
|
41
|
-
|
|
42
|
-
#
|
|
43
|
-
|
|
44
|
-
def validate_action(unchecked)
|
|
45
|
-
TypeError.new("action must be a Symbol") unless unchecked.is_a?(Symbol)
|
|
46
|
-
end
|
|
49
|
+
rejected = unchecked.grep_v(Symbol)
|
|
50
|
+
NameError.new("invalid actions, got: #{rejected.map(&:inspect).join(", ")}") if rejected.size.positive?
|
|
51
|
+
end
|
|
47
52
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
+
# Validate against `actions` rule if argument is a Enumerable. otherwise, validate against `action` rule.
|
|
54
|
+
#
|
|
55
|
+
# @param unchecked the `actions` or `action` to be validated
|
|
56
|
+
def validate_action_or_actions(unchecked)
|
|
57
|
+
unchecked.is_a?(Enumerable) ? validate_actions(unchecked) : validate_action(unchecked)
|
|
58
|
+
end
|
|
53
59
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
60
|
+
# Validate `format` must be a Symbol and a valid MIME type
|
|
61
|
+
#
|
|
62
|
+
# @param unchecked the `format` to be validated
|
|
63
|
+
def validate_format(unchecked)
|
|
64
|
+
return TypeError.new("format must be a Symbol") unless unchecked.is_a?(Symbol)
|
|
57
65
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
# @param unchecked the `actions` or `action` to be validated
|
|
61
|
-
def validate_action_or_actions(unchecked)
|
|
62
|
-
unchecked.is_a?(Enumerable) ? validate_actions(unchecked) : validate_action(unchecked)
|
|
63
|
-
end
|
|
66
|
+
NameError.new("invalid format, got: #{unchecked.inspect}") unless supported_format?(unchecked)
|
|
67
|
+
end
|
|
64
68
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
# Validate `formats` must be an collection of Symbol which each of them is a valid MIME type
|
|
70
|
+
#
|
|
71
|
+
# @param unchecked the `formats` to be validated
|
|
72
|
+
def validate_formats(unchecked)
|
|
73
|
+
return TypeError.new("formats must not be empty") if unchecked.empty?
|
|
70
74
|
|
|
71
|
-
|
|
72
|
-
end
|
|
75
|
+
rejected = unchecked.to_set.reject { |format| supported_format?(format) }
|
|
73
76
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
# @param unchecked the `formats` to be validated
|
|
77
|
-
def validate_formats(unchecked)
|
|
78
|
-
return TypeError.new("formats must not be empty") if unchecked.empty?
|
|
77
|
+
NameError.new("invalid formats, got: #{rejected.map(&:inspect).join(", ")}") if rejected.size.positive?
|
|
78
|
+
end
|
|
79
79
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
# Validate against `formats` rule if argument is a Enumerable. otherwise, validate against `format` rule.
|
|
81
|
+
#
|
|
82
|
+
# @param unchecked the `formats` or `format` to be validated
|
|
83
|
+
def validate_format_or_formats(unchecked)
|
|
84
|
+
unchecked.is_a?(Enumerable) ? validate_formats(unchecked) : validate_format(unchecked)
|
|
85
|
+
end
|
|
83
86
|
|
|
84
|
-
|
|
85
|
-
|
|
87
|
+
# Validate `klazz` must be a Class/Module or a String referencing a Class/Module
|
|
88
|
+
#
|
|
89
|
+
# @param unchecked the `klazz` to be validated
|
|
90
|
+
def validate_klazz(unchecked)
|
|
91
|
+
return if unchecked.is_a?(Module) || unchecked.is_a?(String)
|
|
86
92
|
|
|
87
|
-
#
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
93
|
+
TypeError.new("#{unchecked.inspect} must be a Class/Module or a String referencing a Class/Module")
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Validate `callable` must be a Symbol or Proc, a lambda must not require more than `max_arguments`
|
|
97
|
+
#
|
|
98
|
+
# @param unchecked the `callable` to be validated
|
|
99
|
+
# @param max_arguments the arguments the `callable` is called with
|
|
100
|
+
def validate_callable(unchecked, max_arguments = 0)
|
|
101
|
+
unless unchecked.is_a?(Proc) || unchecked.is_a?(Symbol)
|
|
102
|
+
return TypeError.new("#{unchecked.inspect} must be a Symbol or Proc")
|
|
92
103
|
end
|
|
104
|
+
return unless unchecked.is_a?(Proc) && unchecked.lambda?
|
|
93
105
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
# @param unchecked the `klazz` to be validated
|
|
97
|
-
def validate_klazz(unchecked)
|
|
98
|
-
return if unchecked.is_a?(Module) || unchecked.is_a?(String)
|
|
106
|
+
required = unchecked.parameters.count { |type, _| type == :req }
|
|
107
|
+
return if required <= max_arguments
|
|
99
108
|
|
|
100
|
-
|
|
101
|
-
|
|
109
|
+
requirement = max_arguments.zero? ? "arguments" : "more than #{max_arguments} argument"
|
|
110
|
+
ArgumentError.new("#{unchecked.inspect} must not require #{requirement}")
|
|
111
|
+
end
|
|
102
112
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
# @param unchecked the `callable` to be validated
|
|
106
|
-
def validate_callable(unchecked)
|
|
107
|
-
return if unchecked.is_a?(Proc) || unchecked.is_a?(Symbol)
|
|
113
|
+
def supported_format?(format)
|
|
114
|
+
return Mime::SET.symbols.include?(format) if ActionPack.version < Gem::Version.new("7.1")
|
|
108
115
|
|
|
109
|
-
|
|
110
|
-
end
|
|
116
|
+
Mime.symbols.include?(format)
|
|
111
117
|
end
|
|
118
|
+
private_class_method :supported_format?
|
|
112
119
|
end
|
|
113
120
|
end
|
data/lib/mime_actor/version.rb
CHANGED
data/lib/mime_actor.rb
CHANGED
|
@@ -3,24 +3,11 @@
|
|
|
3
3
|
# :markup: markdown
|
|
4
4
|
# :include: ../README.md
|
|
5
5
|
|
|
6
|
+
require "active_support/version"
|
|
7
|
+
# active_support < 7.1 uses Logger without requiring it
|
|
8
|
+
require "logger" if ActiveSupport.version < Gem::Version.new("7.1")
|
|
9
|
+
require "active_support" # load the core before its components, required by rails >= 8.2
|
|
10
|
+
|
|
6
11
|
require "mime_actor/version"
|
|
7
12
|
require "mime_actor/errors"
|
|
8
|
-
|
|
9
|
-
# load mime_actor components
|
|
10
13
|
require "mime_actor/action"
|
|
11
|
-
require "mime_actor/callbacks"
|
|
12
|
-
require "mime_actor/logging"
|
|
13
|
-
require "mime_actor/rescue"
|
|
14
|
-
require "mime_actor/scene"
|
|
15
|
-
require "mime_actor/stage"
|
|
16
|
-
require "mime_actor/validator"
|
|
17
|
-
|
|
18
|
-
require "mime_actor/deprecator"
|
|
19
|
-
|
|
20
|
-
begin
|
|
21
|
-
require "rails"
|
|
22
|
-
rescue LoadError
|
|
23
|
-
# noop
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
require "mime_actor/railtie" if defined?(Rails::Railtie)
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mime_actor
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ryan Chang
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: actionpack
|
|
@@ -50,14 +50,9 @@ files:
|
|
|
50
50
|
- lib/mime_actor.rb
|
|
51
51
|
- lib/mime_actor/action.rb
|
|
52
52
|
- lib/mime_actor/callbacks.rb
|
|
53
|
-
- lib/mime_actor/deprecator.rb
|
|
54
|
-
- lib/mime_actor/dispatcher.rb
|
|
55
53
|
- lib/mime_actor/errors.rb
|
|
56
|
-
- lib/mime_actor/logging.rb
|
|
57
|
-
- lib/mime_actor/railtie.rb
|
|
58
54
|
- lib/mime_actor/rescue.rb
|
|
59
55
|
- lib/mime_actor/scene.rb
|
|
60
|
-
- lib/mime_actor/stage.rb
|
|
61
56
|
- lib/mime_actor/validator.rb
|
|
62
57
|
- lib/mime_actor/version.rb
|
|
63
58
|
- sig/mime_actor.rbs
|
|
@@ -84,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
84
79
|
- !ruby/object:Gem::Version
|
|
85
80
|
version: '0'
|
|
86
81
|
requirements: []
|
|
87
|
-
rubygems_version: 3.6.
|
|
82
|
+
rubygems_version: 3.6.9
|
|
88
83
|
specification_version: 4
|
|
89
84
|
summary: Action processing with Callback + Rescue handlers for different MIME types
|
|
90
85
|
in Rails
|