mime_actor 0.0.4.alpha → 0.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/lib/mime_actor/formatter.rb +16 -6
- data/lib/mime_actor/rescuer.rb +140 -0
- data/lib/mime_actor/version.rb +3 -3
- data/lib/mime_actor.rb +6 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e71c1193071831b1f09601cb8ea8909e1f7b2b6642dfdab1253f288df4717c2
|
4
|
+
data.tar.gz: 22c67a0d503292dffa976efb5fc467ab1e280abd59dd7b2058ea0b75759ca5bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 04d272ad8c24f21fa92576969bf6e434a3c65c46dfb9e39ddfb7e9b80b7847055a21a9c5dc5e38274916af61be5fa9fcc08bd7e9612625a9a5992915ce4e14f5
|
7
|
+
data.tar.gz: 3d2f97f2b3ae25c7e64047629967932da4c0bceca4794f8b93fa87f98250e7a18256331ebdd24fe5ba5ca2d60eb9cb4a6349512da1040dbef0117a045277b76c
|
data/lib/mime_actor/formatter.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'active_support'
|
3
|
+
require 'active_support/concern'
|
4
|
+
require 'active_support/configurable'
|
4
5
|
require "active_support/core_ext/array/extract_options"
|
5
6
|
require "active_support/core_ext/array/wrap"
|
6
7
|
require "active_support/core_ext/hash/indifferent_access"
|
7
8
|
require "active_support/core_ext/module/attribute_accessors"
|
8
|
-
require '
|
9
|
+
require 'abstract_controller/logger'
|
10
|
+
require 'abstract_controller/rendering'
|
11
|
+
require 'action_controller/metal/mime_responds'
|
9
12
|
|
10
13
|
module MimeActor
|
11
14
|
module Formatter
|
@@ -15,9 +18,6 @@ module MimeActor
|
|
15
18
|
include AbstractController::Rendering # required by MimeResponds
|
16
19
|
include ActionController::MimeResponds
|
17
20
|
|
18
|
-
SUPPORTED_MIME_TYPES = Mime::SET.symbols.to_set
|
19
|
-
private_constant :SUPPORTED_MIME_TYPES
|
20
|
-
|
21
21
|
included do
|
22
22
|
mattr_accessor :action_formatters, instance_writer: false, default: ActiveSupport::HashWithIndifferentAccess.new
|
23
23
|
mattr_accessor :raise_on_missing_action_formatter, instance_writer: false, default: false
|
@@ -62,7 +62,17 @@ module MimeActor
|
|
62
62
|
formatters.each do |formatter|
|
63
63
|
next unless actor = find_actor(action_name, formatter)
|
64
64
|
|
65
|
-
|
65
|
+
dispatch = unless self.class.respond_to?(:dispatch_act)
|
66
|
+
actor
|
67
|
+
else
|
68
|
+
self.class.public_send(:dispatch_act,
|
69
|
+
action: action_name.to_sym,
|
70
|
+
format: formatter.to_sym,
|
71
|
+
context: self,
|
72
|
+
&actor
|
73
|
+
)
|
74
|
+
end
|
75
|
+
collector.public_send(formatter, &dispatch)
|
66
76
|
end
|
67
77
|
end
|
68
78
|
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
require 'active_support/configurable'
|
5
|
+
require "active_support/core_ext/module/attribute_accessors"
|
6
|
+
require 'abstract_controller/logger'
|
7
|
+
|
8
|
+
module MimeActor
|
9
|
+
module Rescuer
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
include ActiveSupport::Configurable
|
12
|
+
include AbstractController::Logger
|
13
|
+
|
14
|
+
included do
|
15
|
+
mattr_accessor :actor_rescuers, instance_writer: false, default: []
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def rescue_act_from(*klazzes, format: nil, action: nil, with: nil, &block)
|
20
|
+
unless with
|
21
|
+
raise ArgumentError, "Provide the with: keyword argument or a block" unless block_given?
|
22
|
+
with = block
|
23
|
+
end
|
24
|
+
raise ArgumentError, "Rescue handler can only be Symbol/Proc" unless with.is_a?(Proc) || with.is_a?(Symbol)
|
25
|
+
|
26
|
+
if format.present?
|
27
|
+
case format
|
28
|
+
when Symbol
|
29
|
+
raise ArgumentError, "Unsupported format: #{format}" unless SUPPORTED_MIME_TYPES.include?(format.to_sym)
|
30
|
+
when Enumerable
|
31
|
+
unfiltered = format.to_set
|
32
|
+
filtered = unfiltered & SUPPORTED_MIME_TYPES
|
33
|
+
rejected = unfiltered - filtered
|
34
|
+
raise ArgumentError, "Unsupported formats: #{rejected.join(', ')}" if rejected.size.positive?
|
35
|
+
else
|
36
|
+
raise ArgumentError, "Format filter can only be Symbol/Enumerable"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
if action.present?
|
41
|
+
message = "Action filter can only be Symbol/Enumerable"
|
42
|
+
raise ArgumentError, message unless action.is_a?(Symbol) || action.is_a?(Enumerable)
|
43
|
+
end
|
44
|
+
|
45
|
+
klazzes.each do |klazz|
|
46
|
+
error = if klazz.is_a?(Module)
|
47
|
+
klazz.name
|
48
|
+
elsif klazz.is_a?(String)
|
49
|
+
klazz
|
50
|
+
else
|
51
|
+
raise ArgumentError, "#{klazz.inspect} must be a class/module or a String referencing a class/module"
|
52
|
+
end
|
53
|
+
|
54
|
+
# append at the end because strategies are read in reverse.
|
55
|
+
self.actor_rescuers << [error, format, action, with]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def dispatch_act(action: nil, format: nil, context: self, &block)
|
60
|
+
lambda do
|
61
|
+
case block
|
62
|
+
when Proc
|
63
|
+
context.instance_exec(&block)
|
64
|
+
else
|
65
|
+
block.call
|
66
|
+
end
|
67
|
+
rescue Exception => ex
|
68
|
+
rescue_actor(ex, format:, action:, context:) || raise
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def rescue_actor(error, action: nil, format: nil, context: self, visited: [])
|
73
|
+
visited << error
|
74
|
+
|
75
|
+
if rescuer = dispatch_rescuer(error, format:, action:, context:)
|
76
|
+
rescuer.call(error, format, action)
|
77
|
+
error
|
78
|
+
elsif error && error.cause && !visited.include?(error.cause)
|
79
|
+
rescue_actor(error.cause, format:, action:, context:, visited:)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def dispatch_rescuer(error, format:, action:, context:)
|
86
|
+
case rescuer = find_rescuer(error, format:, action:)
|
87
|
+
when Symbol
|
88
|
+
rescuer_method = context.method(rescuer)
|
89
|
+
case rescuer_method.arity
|
90
|
+
when 0
|
91
|
+
-> e,f,a { rescuer_method.call }
|
92
|
+
when 1
|
93
|
+
-> e,f,a { rescuer_method.call(e) }
|
94
|
+
when 2
|
95
|
+
-> e,f,a { rescuer_method.call(e,f) }
|
96
|
+
else
|
97
|
+
-> e,f,a { rescuer_method.call(e,f,a) }
|
98
|
+
end
|
99
|
+
when Proc
|
100
|
+
case rescuer.arity
|
101
|
+
when 0
|
102
|
+
-> e,f,a { context.instance_exec(&rescuer) }
|
103
|
+
when 1
|
104
|
+
-> e,f,a { context.instance_exec(e, &rescuer) }
|
105
|
+
when 2
|
106
|
+
-> e,f,a { context.instance_exec(e, f, &rescuer) }
|
107
|
+
else
|
108
|
+
-> e,f,a { context.instance_exec(e, f, a, &rescuer) }
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def find_rescuer(error, format:, action:)
|
114
|
+
return unless error
|
115
|
+
|
116
|
+
*_, rescuer = actor_rescuers.reverse_each.detect do |rescuee, format_filter, action_filter|
|
117
|
+
next if action_filter.present? && !Array.wrap(action_filter).include?(action)
|
118
|
+
next if format_filter.present? && !Array.wrap(format_filter).include?(format)
|
119
|
+
next unless klazz = constantize_rescuee(rescuee)
|
120
|
+
|
121
|
+
klazz === error # klazz is a member of error
|
122
|
+
end
|
123
|
+
rescuer
|
124
|
+
end
|
125
|
+
|
126
|
+
def constantize_rescuee(class_or_name)
|
127
|
+
case class_or_name
|
128
|
+
when String, Symbol
|
129
|
+
begin
|
130
|
+
const_get(class_or_name)
|
131
|
+
rescue NameError
|
132
|
+
class_or_name.safe_constantize
|
133
|
+
end
|
134
|
+
else
|
135
|
+
class_or_name
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
data/lib/mime_actor/version.rb
CHANGED
data/lib/mime_actor.rb
CHANGED
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.0
|
4
|
+
version: 0.1.0
|
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-06-
|
11
|
+
date: 2024-06-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -95,6 +95,7 @@ files:
|
|
95
95
|
- Rakefile
|
96
96
|
- lib/mime_actor.rb
|
97
97
|
- lib/mime_actor/formatter.rb
|
98
|
+
- lib/mime_actor/rescuer.rb
|
98
99
|
- lib/mime_actor/version.rb
|
99
100
|
- sig/mime_actor.rbs
|
100
101
|
homepage: https://github.com/ryancyq/mime_actor
|