mime_actor 0.6.3.alpha → 0.6.3
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 +72 -71
- data/README.md +39 -43
- data/lib/mime_actor/action.rb +7 -13
- data/lib/mime_actor/dispatcher.rb +1 -1
- data/lib/mime_actor/scene.rb +2 -2
- data/lib/mime_actor/stage.rb +9 -7
- data/lib/mime_actor/version.rb +1 -1
- metadata +4 -10
- data/.hound.yml +0 -2
- data/.rspec +0 -1
- data/.rubocop.yml +0 -66
- data/.ruby-version +0 -1
- data/.yardopts +0 -2
- data/Rakefile +0 -9
- data/codecov.yml +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c54b7a74eed15ac67b37d8df3879584634955ff4db0275cecc1dd4b7d531ec55
|
4
|
+
data.tar.gz: da797d40d7f90f5c6f8e23b61f2fe1441aef08860c6af5ff1763f49cee15ccaa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79eea272d7e8c3b625584eb8ae54790076035b511210302bb948076f54d847f34bb5c1f354d03b5fa319fbcc7c370f4c72a97b806593a732dd6207dfb8b6bbbe
|
7
|
+
data.tar.gz: 387780b44aafbcd60e79b7f9ebedd3c00edc40faa1acdce0c6946aad5787f053ae17ae62cab3ba84f8dd695874074e66ef867f6cfbb79e9ba947ddff837c7baa
|
data/COMPARE.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
## Comparison in Rails
|
2
2
|
|
3
|
-
### MIME
|
3
|
+
### MIME Action
|
4
4
|
|
5
5
|
#### before
|
6
6
|
```rb
|
@@ -22,103 +22,104 @@ end
|
|
22
22
|
#### after
|
23
23
|
```rb
|
24
24
|
class EventsController < ActionController::Base
|
25
|
-
|
25
|
+
include MimeActor::Action
|
26
26
|
|
27
|
-
|
27
|
+
before_act -> { @events = Event.all }, action: :index
|
28
28
|
|
29
|
-
|
30
|
-
|
29
|
+
# dynamically defines the action method according to on: argument
|
30
|
+
respond_act_to :html, :json, on: :index
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
def index_html
|
33
|
+
@event_categories = EventCategory.all
|
34
|
+
|
35
|
+
# render html using @events and @event_categories
|
36
|
+
render :index
|
37
|
+
end
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
def index_json
|
40
|
+
# render json using #as_json
|
41
|
+
render json: @events
|
42
|
+
end
|
43
43
|
end
|
44
44
|
```
|
45
45
|
|
46
|
-
### MIME
|
46
|
+
### MIME Rescue
|
47
47
|
|
48
48
|
#### before
|
49
49
|
```rb
|
50
50
|
class EventsController < ActionController::Base
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
raise ex # re-raise since we are not handling it
|
68
|
-
end
|
51
|
+
before_action only: %i[show update], with: :load_event
|
52
|
+
|
53
|
+
rescue_from ActiveRecord::RecordNotFound do |ex|
|
54
|
+
case action_name.to_s
|
55
|
+
when "show"
|
56
|
+
respond_to do |format|
|
57
|
+
format.html { redirect_to events_path } # redirect to index
|
58
|
+
format.json { render status: :bad_request, json: { error: ex.message } }
|
59
|
+
end
|
60
|
+
when "update"
|
61
|
+
respond_to do |format|
|
62
|
+
format.html { render :edit }
|
63
|
+
format.json { render status: :bad_request, json: { error: ex.message } }
|
64
|
+
end
|
65
|
+
else
|
66
|
+
raise ex # re-raise since we are not handling it
|
69
67
|
end
|
68
|
+
end
|
70
69
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
end
|
70
|
+
def show
|
71
|
+
respond_to do |format|
|
72
|
+
format.html { render :show } # render html using @event
|
73
|
+
format.json { render json: @event } # render json using #as_json
|
76
74
|
end
|
75
|
+
end
|
77
76
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
end
|
77
|
+
def update
|
78
|
+
# ...
|
79
|
+
respond_to do |format|
|
80
|
+
format.html { redirect_to event_path(@event.id) } # redirect to show upon sucessful update
|
81
|
+
format.json { render json: @event } # render json using #as_json
|
84
82
|
end
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def load_event
|
88
|
+
@event = Event.find(params.require(:event_id))
|
89
|
+
end
|
85
90
|
end
|
86
91
|
```
|
87
92
|
#### after
|
88
93
|
```rb
|
89
94
|
class EventsController < ActionController::Base
|
90
|
-
|
95
|
+
include MimeActor::Action
|
91
96
|
|
92
|
-
|
93
|
-
before_action only: [:show, :update] { @event = Event.find(params.require(:event_id)) }
|
97
|
+
before_action only: %i[show update], with: :load_event
|
94
98
|
|
95
|
-
|
99
|
+
respond_act_to :html, on: %i[show update]
|
100
|
+
respond_act_to :json, on: %i[show update], with: -> { render json: @event } # render json using #as_json
|
96
101
|
|
97
|
-
|
98
|
-
render status: :bad_request, json: { error: ex.message }
|
99
|
-
end
|
102
|
+
rescue_act_from ActiveRecord::RecordNotFound, format: :json, with: :handle_json_error
|
100
103
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
+
rescue_act_from ActiveRecord::RecordNotFound, format: :html, action: :show do
|
105
|
+
redirect_to events_path
|
106
|
+
end
|
104
107
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
+
private
|
109
|
+
|
110
|
+
def show_html
|
111
|
+
render :show # render html using @event
|
112
|
+
end
|
108
113
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
def show_json
|
116
|
-
render json: @event # render json using #as_json
|
117
|
-
end
|
114
|
+
def update_html
|
115
|
+
# ...
|
116
|
+
redirect_to event_path(@event.id) # redirect to show upon sucessful update
|
117
|
+
rescue ActiveRecord::RecordNotFound
|
118
|
+
render :edit
|
119
|
+
end
|
118
120
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
end
|
121
|
+
def handle_json_error(error)
|
122
|
+
render status: :bad_request, json: { error: error.message }
|
123
|
+
end
|
123
124
|
end
|
124
125
|
```
|
data/README.md
CHANGED
@@ -5,55 +5,51 @@
|
|
5
5
|
[![Coverage][coverage_badge]][coverage]
|
6
6
|
[![Maintainability][maintainability_badge]][maintainability]
|
7
7
|
|
8
|
-
Action
|
8
|
+
Action processing with Callback + Rescue handlers for different MIME types in Rails.
|
9
9
|
|
10
10
|
## Usage
|
11
11
|
|
12
12
|
MimeActor allows you to do something like below:
|
13
13
|
```rb
|
14
14
|
class EventsController < ActionController::Base
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
def update_json
|
54
|
-
# ...
|
55
|
-
render json: @event # render json using #as_json
|
56
|
-
end
|
15
|
+
include MimeActor::Action
|
16
|
+
|
17
|
+
before_act -> { @events = Event.all }, action: :index
|
18
|
+
before_act :load_event, action: %i[show update]
|
19
|
+
|
20
|
+
respond_act_to :html, :json, on: :update
|
21
|
+
respond_act_to :html, on: %i[index show], with: :render_html
|
22
|
+
respond_act_to :json, on: %i[index show], with: -> { render json: { action: action_name } }
|
23
|
+
|
24
|
+
rescue_act_from ActiveRecord::RecordNotFound, format: :json, with: :handle_json_error
|
25
|
+
rescue_act_from ActiveRecord::RecordNotFound, format: :html, action: :show, with: -> { redirect_to "/events" }
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def update_html
|
30
|
+
# ...
|
31
|
+
redirect_to "/events/#{@event.id}" # redirect to show upon sucessful update
|
32
|
+
rescue ActiveRecord::RecordNotFound
|
33
|
+
render html: :edit
|
34
|
+
end
|
35
|
+
|
36
|
+
def update_json
|
37
|
+
# ...
|
38
|
+
render json: @event # render json using #as_json
|
39
|
+
end
|
40
|
+
|
41
|
+
def render_html
|
42
|
+
@event_categories = EventCategory.all if action_name == :index
|
43
|
+
render html: action_name
|
44
|
+
end
|
45
|
+
|
46
|
+
def load_event
|
47
|
+
@event = Event.find(params.require(:event_id))
|
48
|
+
end
|
49
|
+
|
50
|
+
def handle_json_error(error)
|
51
|
+
render status: :bad_request, json: { error: error.message }
|
52
|
+
end
|
57
53
|
end
|
58
54
|
```
|
59
55
|
|
data/lib/mime_actor/action.rb
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
|
3
3
|
# :markup: markdown
|
4
4
|
|
5
|
-
require "mime_actor/callbacks"
|
6
5
|
require "mime_actor/logging"
|
7
6
|
require "mime_actor/scene"
|
8
7
|
require "mime_actor/stage"
|
@@ -18,7 +17,7 @@ module MimeActor
|
|
18
17
|
#
|
19
18
|
# `Action` is the recommended `Module` to be included in the `ActionController`.
|
20
19
|
#
|
21
|
-
# Provides intuitive way of `action`
|
20
|
+
# Provides intuitive way of `action` processing for a specific MIME type with callback + rescue handlers.
|
22
21
|
#
|
23
22
|
module Action
|
24
23
|
extend ActiveSupport::Concern
|
@@ -26,17 +25,15 @@ module MimeActor
|
|
26
25
|
include AbstractController::Rendering # required by MimeResponds
|
27
26
|
include ActionController::MimeResponds
|
28
27
|
|
29
|
-
include Callbacks
|
30
28
|
include Scene
|
31
29
|
include Stage
|
32
30
|
include Logging
|
33
31
|
|
34
32
|
# The core logic where rendering logics are collected as `Proc` and passed over to `ActionController::MimeResponds`
|
35
33
|
#
|
36
|
-
# @param action the `action` of the controller
|
37
|
-
#
|
38
34
|
# @example process `create` action
|
39
|
-
#
|
35
|
+
# # it uses AbstractController#action_name to process
|
36
|
+
# start_scene
|
40
37
|
#
|
41
38
|
# # it is equivalent to the following if `create` action is configured with `html` and `json` formats
|
42
39
|
# def create
|
@@ -46,21 +43,18 @@ module MimeActor
|
|
46
43
|
# end
|
47
44
|
# end
|
48
45
|
#
|
49
|
-
def start_scene
|
46
|
+
def start_scene
|
47
|
+
action = action_name.to_sym
|
50
48
|
formats = acting_scenes.fetch(action, {})
|
51
49
|
|
52
50
|
if formats.empty?
|
53
|
-
logger.warn { "format is empty for action: #{
|
51
|
+
logger.warn { "format is empty for action: #{action_name.inspect}" }
|
54
52
|
return
|
55
53
|
end
|
56
54
|
|
57
55
|
respond_to do |collector|
|
58
56
|
formats.each do |format, actor|
|
59
|
-
dispatch =
|
60
|
-
run_act_callbacks(format) do
|
61
|
-
cue_actor(actor.presence || "#{action}_#{format}", action:, format:)
|
62
|
-
end
|
63
|
-
end
|
57
|
+
dispatch = -> { cue_actor(actor.presence || "#{action}_#{format}", format:) }
|
64
58
|
collector.public_send(format, &dispatch)
|
65
59
|
end
|
66
60
|
end
|
@@ -16,7 +16,7 @@ module MimeActor
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def call(target)
|
19
|
-
raise MimeActor::ActorNotFound, method_name unless target.respond_to?(method_name)
|
19
|
+
raise MimeActor::ActorNotFound, method_name unless target.respond_to?(method_name, true)
|
20
20
|
|
21
21
|
method_call = target.method(method_name)
|
22
22
|
filtered_args = method_call.arity.negative? ? args : args.take(method_call.arity)
|
data/lib/mime_actor/scene.rb
CHANGED
@@ -116,11 +116,11 @@ module MimeActor
|
|
116
116
|
def define_scene(action)
|
117
117
|
module_eval(
|
118
118
|
# def index
|
119
|
-
# self.respond_to?(:start_scene) && self.start_scene
|
119
|
+
# self.respond_to?(:start_scene) && self.start_scene
|
120
120
|
# end
|
121
121
|
<<-RUBY, __FILE__, __LINE__ + 1
|
122
122
|
def #{action}
|
123
|
-
self.respond_to?(:start_scene) && self.start_scene
|
123
|
+
self.respond_to?(:start_scene) && self.start_scene
|
124
124
|
end
|
125
125
|
RUBY
|
126
126
|
)
|
data/lib/mime_actor/stage.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
# :markup: markdown
|
4
4
|
|
5
|
+
require "mime_actor/callbacks"
|
5
6
|
require "mime_actor/dispatcher"
|
6
7
|
require "mime_actor/logging"
|
7
8
|
require "mime_actor/rescue"
|
@@ -17,6 +18,7 @@ module MimeActor
|
|
17
18
|
module Stage
|
18
19
|
extend ActiveSupport::Concern
|
19
20
|
|
21
|
+
include Callbacks
|
20
22
|
include Rescue
|
21
23
|
include Logging
|
22
24
|
|
@@ -76,21 +78,21 @@ module MimeActor
|
|
76
78
|
# @param actor either a method name or a Proc to evaluate
|
77
79
|
# @param args arguments to be passed when calling the actor
|
78
80
|
#
|
79
|
-
def cue_actor(actor, *args,
|
81
|
+
def cue_actor(actor, *args, format:)
|
80
82
|
dispatcher = MimeActor::Dispatcher.build(actor, *args)
|
81
83
|
raise TypeError, "invalid actor, got: #{actor.inspect}" unless dispatcher
|
82
84
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
result
|
85
|
+
self.class.validate!(:format, format)
|
86
|
+
|
87
|
+
run_act_callbacks(format) do
|
88
|
+
result = dispatcher.call(self)
|
89
|
+
block_given? ? yield(result) : result
|
88
90
|
end
|
89
91
|
rescue MimeActor::ActorNotFound => e
|
90
92
|
logger.error { "actor error, cause: #{e.inspect}" } unless raise_on_actor_error
|
91
93
|
raise e if raise_on_actor_error
|
92
94
|
rescue StandardError => e
|
93
|
-
rescued = rescue_actor(e, action
|
95
|
+
rescued = rescue_actor(e, action: action_name.to_sym, format: format)
|
94
96
|
rescued || raise
|
95
97
|
end
|
96
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.6.3
|
4
|
+
version: 0.6.3
|
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-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -59,16 +59,9 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
- ".hound.yml"
|
63
|
-
- ".rspec"
|
64
|
-
- ".rubocop.yml"
|
65
|
-
- ".ruby-version"
|
66
|
-
- ".yardopts"
|
67
62
|
- COMPARE.md
|
68
63
|
- LICENSE
|
69
64
|
- README.md
|
70
|
-
- Rakefile
|
71
|
-
- codecov.yml
|
72
65
|
- lib/mime_actor.rb
|
73
66
|
- lib/mime_actor/action.rb
|
74
67
|
- lib/mime_actor/callbacks.rb
|
@@ -108,5 +101,6 @@ requirements: []
|
|
108
101
|
rubygems_version: 3.5.9
|
109
102
|
signing_key:
|
110
103
|
specification_version: 4
|
111
|
-
summary: Action
|
104
|
+
summary: Action processing with Callback + Rescue handlers for different MIME types
|
105
|
+
in Rails
|
112
106
|
test_files: []
|
data/.hound.yml
DELETED
data/.rspec
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--require spec_helper
|
data/.rubocop.yml
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
require:
|
2
|
-
- rubocop-rake
|
3
|
-
- rubocop-rspec
|
4
|
-
|
5
|
-
AllCops:
|
6
|
-
TargetRubyVersion: 3.1
|
7
|
-
NewCops: enable
|
8
|
-
Exclude:
|
9
|
-
- bin/*
|
10
|
-
- vendor/**/*
|
11
|
-
|
12
|
-
Style/Documentation:
|
13
|
-
Enabled: false
|
14
|
-
|
15
|
-
Style/BlockComments:
|
16
|
-
Exclude:
|
17
|
-
- spec/*_helper.rb
|
18
|
-
|
19
|
-
Style/RegexpLiteral:
|
20
|
-
EnforcedStyle: percent_r
|
21
|
-
|
22
|
-
Metrics/AbcSize:
|
23
|
-
Enabled: false
|
24
|
-
|
25
|
-
Metrics/BlockLength:
|
26
|
-
AllowedMethods:
|
27
|
-
- class_methods
|
28
|
-
|
29
|
-
Metrics/CyclomaticComplexity:
|
30
|
-
Enabled: false
|
31
|
-
|
32
|
-
Metrics/PerceivedComplexity:
|
33
|
-
Enabled: false
|
34
|
-
|
35
|
-
Metrics/MethodLength:
|
36
|
-
CountAsOne: ['array', 'heredoc', 'method_call']
|
37
|
-
Max: 15
|
38
|
-
|
39
|
-
Style/StringLiterals:
|
40
|
-
EnforcedStyle: double_quotes
|
41
|
-
|
42
|
-
Style/StringLiteralsInInterpolation:
|
43
|
-
EnforcedStyle: double_quotes
|
44
|
-
|
45
|
-
Style/HashSyntax:
|
46
|
-
EnforcedShorthandSyntax: consistent
|
47
|
-
|
48
|
-
Layout/FirstHashElementIndentation:
|
49
|
-
EnforcedStyle: consistent
|
50
|
-
|
51
|
-
Layout/HashAlignment:
|
52
|
-
EnforcedHashRocketStyle: table
|
53
|
-
EnforcedColonStyle: table
|
54
|
-
|
55
|
-
RSpec/MultipleExpectations:
|
56
|
-
Max: 6
|
57
|
-
|
58
|
-
RSpec/ExampleLength:
|
59
|
-
CountAsOne: ['array', 'heredoc', 'method_call']
|
60
|
-
Max: 10
|
61
|
-
|
62
|
-
RSpec/MultipleMemoizedHelpers:
|
63
|
-
Max: 10
|
64
|
-
|
65
|
-
RSpec/NestedGroups:
|
66
|
-
Max: 5
|
data/.ruby-version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
3.3.1
|
data/.yardopts
DELETED
data/Rakefile
DELETED
data/codecov.yml
DELETED