mime_actor 0.5.4 → 0.6.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/.rubocop.yml +4 -0
- data/COMPARE.md +2 -2
- data/README.md +2 -2
- data/lib/mime_actor/action.rb +1 -1
- data/lib/mime_actor/rescue.rb +9 -24
- data/lib/mime_actor/scene.rb +33 -29
- data/lib/mime_actor/stage.rb +3 -2
- data/lib/mime_actor/validator.rb +11 -4
- data/lib/mime_actor/version.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 031455cd4203f02b1760129516e1a8722af2f84d7580cf1799f886d98262cdea
|
4
|
+
data.tar.gz: 78b2841148310600b408fd3d2932741ab61280f24afb28f0b251e62bcba91419
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2852a58bb4dc628d293e6d5974b452144496be454a2a594aec90baaffcf5fbbcdd096745778853f36cc5e8ef4eb577340175bd76533ba2be1852799ddf5cc2a6
|
7
|
+
data.tar.gz: f548c1d8fe2c32a2aba34c4b9189e3e02272fdcfdc850d8b88e6cb6eea54514a1c95cc8ede181656f5ba0e92111f2566db1cbc98b647dd77f0922cd2ff1357be
|
data/.rubocop.yml
CHANGED
data/COMPARE.md
CHANGED
@@ -94,11 +94,11 @@ class EventsController < ActionController::Base
|
|
94
94
|
|
95
95
|
act_on_format :html, :json, on: [:show, :update]
|
96
96
|
|
97
|
-
|
97
|
+
rescue_act_from ActiveRecord::RecordNotFound, format: :json do |ex|
|
98
98
|
render status: :bad_request, json: { error: ex.message }
|
99
99
|
end
|
100
100
|
|
101
|
-
|
101
|
+
rescue_act_from ActiveRecord::RecordNotFound, format: :html, action: :show do |ex|
|
102
102
|
redirect_to events_path
|
103
103
|
end
|
104
104
|
|
data/README.md
CHANGED
@@ -19,11 +19,11 @@ class EventsController < ActionController::Base
|
|
19
19
|
act_on_format :html, :json, on: :index
|
20
20
|
act_on_format :html, :json, on: [:show, :update]
|
21
21
|
|
22
|
-
|
22
|
+
rescue_act_from ActiveRecord::RecordNotFound, format: :json do |ex|
|
23
23
|
render status: :bad_request, json: { error: "Resouce not found" }
|
24
24
|
end
|
25
25
|
|
26
|
-
|
26
|
+
rescue_act_from ActiveRecord::RecordNotFound, format: :html, action: :show do |ex|
|
27
27
|
redirect_to events_path
|
28
28
|
end
|
29
29
|
|
data/lib/mime_actor/action.rb
CHANGED
@@ -27,7 +27,7 @@ module MimeActor
|
|
27
27
|
include Rescue
|
28
28
|
include Logging
|
29
29
|
|
30
|
-
# The core logic where rendering logics are collected as `Proc`
|
30
|
+
# The core logic where rendering logics are collected as `Proc` and passed over to `ActionController::MimeResponds`
|
31
31
|
#
|
32
32
|
# @param action the `action` of the controller
|
33
33
|
#
|
data/lib/mime_actor/rescue.rb
CHANGED
@@ -16,7 +16,7 @@ module MimeActor
|
|
16
16
|
# Simillar to `ActionController::Rescue` but with additional filtering logic on `action`/`format`.
|
17
17
|
#
|
18
18
|
# @example Rescue RuntimeError when raised for any action with `json` format
|
19
|
-
#
|
19
|
+
# rescue_act_from RuntimeError, format: :json, with: :handle_json_error
|
20
20
|
#
|
21
21
|
module Rescue
|
22
22
|
extend ActiveSupport::Concern
|
@@ -28,7 +28,7 @@ module MimeActor
|
|
28
28
|
mattr_accessor :actor_rescuers, instance_writer: false, default: []
|
29
29
|
end
|
30
30
|
|
31
|
-
|
31
|
+
class_methods do
|
32
32
|
# Registers a rescue handler for the given error classes with `action`/`format` filter
|
33
33
|
#
|
34
34
|
# @param klazzes the error classes to rescue
|
@@ -38,45 +38,30 @@ module MimeActor
|
|
38
38
|
# @param block the `block` to evaluate when `with` is not provided
|
39
39
|
#
|
40
40
|
# @example Rescue StandardError when raised for any action with `html` format
|
41
|
-
#
|
41
|
+
# rescue_act_from StandardError, format: :html, with: :handle_html_error
|
42
42
|
#
|
43
43
|
# @example Rescue StandardError when raised for `show` action with `json` format
|
44
|
-
#
|
44
|
+
# rescue_act_from StandardError, format: :json, action: :show do |ex|
|
45
45
|
# render status: :bad_request, json: { error: ex.message }
|
46
46
|
# end
|
47
47
|
#
|
48
|
-
def
|
48
|
+
def rescue_act_from(*klazzes, action: nil, format: nil, with: nil, &block)
|
49
49
|
raise ArgumentError, "error filter is required" if klazzes.empty?
|
50
50
|
|
51
51
|
validate!(:with, with, block)
|
52
52
|
with = block if block_given?
|
53
53
|
|
54
54
|
if action.present?
|
55
|
-
|
56
|
-
validate!(:actions, action)
|
57
|
-
else
|
58
|
-
validate!(:action, action)
|
59
|
-
end
|
55
|
+
action.is_a?(Enumerable) ? validate!(:actions, action) : validate!(:action, action)
|
60
56
|
end
|
61
57
|
|
62
58
|
if format.present?
|
63
|
-
|
64
|
-
validate!(:formats, format)
|
65
|
-
else
|
66
|
-
validate!(:format, format)
|
67
|
-
end
|
59
|
+
format.is_a?(Enumerable) ? validate!(:formats, format) : validate!(:format, format)
|
68
60
|
end
|
69
61
|
|
70
62
|
klazzes.each do |klazz|
|
71
|
-
|
72
|
-
|
73
|
-
klazz.name
|
74
|
-
when String
|
75
|
-
klazz
|
76
|
-
else
|
77
|
-
message = "#{klazz.inspect} must be a Class/Module or a String referencing a Class/Module"
|
78
|
-
raise ArgumentError, message
|
79
|
-
end
|
63
|
+
validate!(:klazz, klazz)
|
64
|
+
error = klazz.is_a?(Module) ? klazz.name : klazz
|
80
65
|
|
81
66
|
# append at the end because strategies are read in reverse.
|
82
67
|
actor_rescuers << [error, format, action, with]
|
data/lib/mime_actor/scene.rb
CHANGED
@@ -31,7 +31,7 @@ module MimeActor
|
|
31
31
|
mattr_accessor :acting_scenes, instance_writer: false, default: {}
|
32
32
|
end
|
33
33
|
|
34
|
-
|
34
|
+
class_methods do
|
35
35
|
# Register `action` + `format` definitions.
|
36
36
|
#
|
37
37
|
# @param options [Array]
|
@@ -39,49 +39,53 @@ module MimeActor
|
|
39
39
|
# @option options [Hash] on the collection of `action`
|
40
40
|
#
|
41
41
|
# @example register a `html` format on action `index`
|
42
|
-
#
|
42
|
+
# act_on_format :html, on: :index
|
43
43
|
# @example register `html`, `json` formats on actions `index`, `show`
|
44
|
-
#
|
44
|
+
# act_on_format :html, :json , on: [:index, :show]
|
45
45
|
#
|
46
46
|
# For each unique `action` being registered, it will have a corresponding `action` method being defined.
|
47
|
-
def
|
47
|
+
def act_on_format(*options)
|
48
48
|
config = options.extract_options!
|
49
49
|
validate!(:formats, options)
|
50
50
|
|
51
|
-
actions = config[:on]
|
52
|
-
|
53
|
-
raise ArgumentError, "action is required"
|
54
|
-
elsif actions.is_a?(Enumerable)
|
51
|
+
case actions = config[:on]
|
52
|
+
when Enumerable
|
55
53
|
validate!(:actions, actions)
|
56
|
-
|
54
|
+
when Symbol, String
|
57
55
|
validate!(:action, actions)
|
56
|
+
else
|
57
|
+
raise ArgumentError, "action is required"
|
58
58
|
end
|
59
59
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
60
|
+
Array.wrap(actions).each do |action|
|
61
|
+
options.each { |format| compose_scene(action, format) }
|
62
|
+
end
|
63
|
+
end
|
64
64
|
|
65
|
-
|
66
|
-
acting_scenes[action] |= [format]
|
65
|
+
private
|
67
66
|
|
68
|
-
|
67
|
+
def compose_scene(action, format)
|
68
|
+
action_defined = (instance_methods + private_instance_methods).include?(action.to_sym)
|
69
|
+
raise MimeActor::ActionExisted, action if !acting_scenes.key?(action) && action_defined
|
69
70
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
<<-RUBY, __FILE__, __LINE__ + 1
|
75
|
-
def #{action}
|
76
|
-
self.respond_to?(:start_scene) && self.start_scene(:#{action})
|
77
|
-
end
|
78
|
-
RUBY
|
79
|
-
)
|
80
|
-
end
|
81
|
-
end
|
71
|
+
acting_scenes[action] ||= Set.new
|
72
|
+
acting_scenes[action] |= [format]
|
73
|
+
|
74
|
+
define_scene(action) unless action_defined
|
82
75
|
end
|
83
76
|
|
84
|
-
|
77
|
+
def define_scene(action)
|
78
|
+
class_eval(
|
79
|
+
# def index
|
80
|
+
# self.respond_to?(:start_scene) && self.start_scene(:index)
|
81
|
+
# end
|
82
|
+
<<-RUBY, __FILE__, __LINE__ + 1
|
83
|
+
def #{action}
|
84
|
+
self.respond_to?(:start_scene) && self.start_scene(:#{action})
|
85
|
+
end
|
86
|
+
RUBY
|
87
|
+
)
|
88
|
+
end
|
85
89
|
end
|
86
90
|
end
|
87
91
|
end
|
data/lib/mime_actor/stage.rb
CHANGED
@@ -22,7 +22,7 @@ module MimeActor
|
|
22
22
|
mattr_accessor :raise_on_missing_actor, instance_writer: false, default: false
|
23
23
|
end
|
24
24
|
|
25
|
-
|
25
|
+
class_methods do
|
26
26
|
# Determine if the `actor_name` belongs to a public instance method excluding methods inherited from ancestors
|
27
27
|
#
|
28
28
|
# @param actor_name
|
@@ -37,7 +37,8 @@ module MimeActor
|
|
37
37
|
found
|
38
38
|
end
|
39
39
|
|
40
|
-
# Wraps the given `block` with a `lambda`, rescue any error raised from the `block
|
40
|
+
# Wraps the given `block` with a `lambda`, rescue any error raised from the `block`.
|
41
|
+
# Otherwise, error will be re-raised.
|
41
42
|
#
|
42
43
|
# @param action the `action` to be passed on to `rescue_actor`
|
43
44
|
# @param format the `format` to be passed on to `rescue_actor`
|
data/lib/mime_actor/validator.rb
CHANGED
@@ -25,7 +25,7 @@ module MimeActor
|
|
25
25
|
mattr_accessor :scene_formats, instance_writer: false, default: Mime::SET.symbols.to_set
|
26
26
|
end
|
27
27
|
|
28
|
-
|
28
|
+
class_methods do
|
29
29
|
# Raise the error returned by validator if any.
|
30
30
|
#
|
31
31
|
# @param rule the name of validator
|
@@ -72,6 +72,15 @@ module MimeActor
|
|
72
72
|
NameError.new("invalid formats, got: #{rejected.join(", ")}") if rejected.size.positive?
|
73
73
|
end
|
74
74
|
|
75
|
+
# Validate `klazz` must be a Class/Module or a String referencing a Class/Module
|
76
|
+
#
|
77
|
+
# @param unchecked the `klazz` to be validated
|
78
|
+
def validate_klazz(unchecked)
|
79
|
+
return if unchecked.is_a?(Module) || unchecked.is_a?(String)
|
80
|
+
|
81
|
+
ArgumentError.new("#{unchecked.inspect} must be a Class/Module or a String referencing a Class/Module")
|
82
|
+
end
|
83
|
+
|
75
84
|
# Validate `with` or `block` must be provided and if `with` is provided, it must be a Symbol or Proc
|
76
85
|
#
|
77
86
|
# @param unchecked the `with` to be validated
|
@@ -83,9 +92,7 @@ module MimeActor
|
|
83
92
|
unless unchecked.present? || block.present?
|
84
93
|
return ArgumentError.new("provide the with: keyword argument or a block")
|
85
94
|
end
|
86
|
-
|
87
|
-
return if block.present?
|
88
|
-
return if unchecked.is_a?(Proc) || unchecked.is_a?(Symbol)
|
95
|
+
return if block.present? || unchecked.is_a?(Proc) || unchecked.is_a?(Symbol)
|
89
96
|
|
90
97
|
ArgumentError.new("with handler must be a Symbol or Proc, got: #{unchecked.inspect}")
|
91
98
|
end
|
data/lib/mime_actor/version.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.
|
4
|
+
version: 0.6.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-
|
11
|
+
date: 2024-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|