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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5221de8b1d1149a88de730ab8e6c6c764a22e7067b9e57b6265ce801c671ce3f
4
- data.tar.gz: e98f9d6f3e5a8aa04f16cf77921edad0b4a61893ddb3c019157885079462fcde
3
+ metadata.gz: c54b7a74eed15ac67b37d8df3879584634955ff4db0275cecc1dd4b7d531ec55
4
+ data.tar.gz: da797d40d7f90f5c6f8e23b61f2fe1441aef08860c6af5ff1763f49cee15ccaa
5
5
  SHA512:
6
- metadata.gz: 9f6ce7e70ab30a4be726f346beb517c8036c95a1d516c3012d9ed62723a14c130d4af05cdf3695244f5fcb56d33f83cf719369ead9d05eabeba89e5335de1f3a
7
- data.tar.gz: 90e8ca69ac9ff99e9813c1dac19bc0826893f106968bb6c6684201a84523b13ab9df225fd597daf96379825080f9164ecfb19a6f7b48c8eabf217e5411654530
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 Rendering
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
- include MimeActor::Action
25
+ include MimeActor::Action
26
26
 
27
- before_action only: :index { @events = Event.all }
27
+ before_act -> { @events = Event.all }, action: :index
28
28
 
29
- # dynamically defines the action method according to on: argument
30
- respond_act_to :html, :json, on: :index
29
+ # dynamically defines the action method according to on: argument
30
+ respond_act_to :html, :json, on: :index
31
31
 
32
- def index_html
33
- @event_categories = EventCategory.all
34
-
35
- # render html using @events and @event_categories
36
- render :index
37
- end
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
- def index_json
40
- # render json using #as_json
41
- render json: @events
42
- end
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 Rescuing
46
+ ### MIME Rescue
47
47
 
48
48
  #### before
49
49
  ```rb
50
50
  class EventsController < ActionController::Base
51
- # AbstractController::Callbacks here to load model with params
52
- before_action only: [:show, :update] { @event = Event.find(params.require(:event_id)) }
53
-
54
- rescue_from ActiveRecord::RecordNotFound do |ex|
55
- case action_name.to_s
56
- when "show"
57
- respond_to do |format|
58
- format.html { redirect_to events_path } # redirect to index
59
- format.json { render status: :bad_request, json: { error: ex.message } }
60
- end
61
- when "update"
62
- respond_to do |format|
63
- format.html { render :edit }
64
- format.json { render status: :bad_request, json: { error: ex.message } }
65
- end
66
- else
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
- def show
72
- respond_to do |format|
73
- format.html { render :show } # render html using @event
74
- format.json { render json: @event } # render json using #as_json
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
- def update
79
- # ...
80
- respond_to do |format|
81
- format.html { redirect_to event_path(@event.id) } # redirect to show upon sucessful update
82
- format.json { render json: @event } # render json using #as_json
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
- include MimeActor::Action
95
+ include MimeActor::Action
91
96
 
92
- # AbstractController::Callbacks here to load model with params
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
- respond_act_to :html, :json, on: [:show, :update]
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
- rescue_act_from ActiveRecord::RecordNotFound, format: :json do |ex|
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
- rescue_act_from ActiveRecord::RecordNotFound, format: :html, action: :show do |ex|
102
- redirect_to events_path
103
- end
104
+ rescue_act_from ActiveRecord::RecordNotFound, format: :html, action: :show do
105
+ redirect_to events_path
106
+ end
104
107
 
105
- def show_html
106
- render :show # render html using @event
107
- end
108
+ private
109
+
110
+ def show_html
111
+ render :show # render html using @event
112
+ end
108
113
 
109
- def update_html
110
- redirect_to event_path(@event.id) # redirect to show upon sucessful update
111
- rescue ActiveRecord::RecordNotFound
112
- render :edit
113
- end
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
- def update_json
120
- # ...
121
- render json: @event # render json using #as_json
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 Render + Rescue handlers for different MIME types in Rails
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
- # AbstractController::Callbacks here to load model with params
16
- before_action only: :index { @events = Event.all }
17
- before_action only: [:show, :update] { @event = Event.find(params.require(:event_id)) }
18
-
19
- respond_act_to :html, :json, on: :index
20
- respond_act_to :html, :json, on: [:show, :update]
21
-
22
- rescue_act_from ActiveRecord::RecordNotFound, format: :json do |ex|
23
- render status: :bad_request, json: { error: "Resouce not found" }
24
- end
25
-
26
- rescue_act_from ActiveRecord::RecordNotFound, format: :html, action: :show do |ex|
27
- redirect_to events_path
28
- end
29
-
30
- def index_html
31
- @event_categories = EventCategory.all
32
- render :index # render html using @events and @event_categories
33
- end
34
-
35
- def index_json
36
- render json: @events # render json using #as_json
37
- end
38
-
39
- def show_html
40
- render :show # render html using @event
41
- end
42
-
43
- def update_html
44
- redirect_to event_path(@event.id) # redirect to show upon sucessful update
45
- rescue ActiveRecord::RecordNotFound
46
- render :edit
47
- end
48
-
49
- def show_json
50
- render json: @event # render json using #as_json
51
- end
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
 
@@ -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` rendering for a specific MIME type with rescue handlers.
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
- # start_scene(:create)
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(action)
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: #{action.inspect}" }
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 = lambda do
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)
@@ -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(:index)
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(:#{action})
123
+ self.respond_to?(:start_scene) && self.start_scene
124
124
  end
125
125
  RUBY
126
126
  )
@@ -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, action:, format:)
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
- result = dispatcher.call(self)
84
- if block_given?
85
- yield result
86
- else
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:, format:)
95
+ rescued = rescue_actor(e, action: action_name.to_sym, format: format)
94
96
  rescued || raise
95
97
  end
96
98
  end
@@ -15,7 +15,7 @@ module MimeActor
15
15
  MAJOR = 0
16
16
  MINOR = 6
17
17
  BUILD = 3
18
- PRE = "alpha"
18
+ PRE = nil
19
19
 
20
20
  STRING = [MAJOR, MINOR, BUILD, PRE].compact.join(".")
21
21
  end
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.alpha
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-23 00:00:00.000000000 Z
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 Render + Rescue handlers for different MIME types in Rails
104
+ summary: Action processing with Callback + Rescue handlers for different MIME types
105
+ in Rails
112
106
  test_files: []
data/.hound.yml DELETED
@@ -1,2 +0,0 @@
1
- rubocop:
2
- enabled: false
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
@@ -1,2 +0,0 @@
1
- --markup markdown
2
- lib/**/*.rb
data/Rakefile DELETED
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
-
5
- require "rubocop/rake_task"
6
- RuboCop::RakeTask.new
7
-
8
- require "rspec/core/rake_task"
9
- RSpec::Core::RakeTask.new(:spec)
data/codecov.yml DELETED
@@ -1,18 +0,0 @@
1
- codecov:
2
- notify:
3
- wait_for_ci: false
4
- require_ci_to_pass: false
5
-
6
- coverage:
7
- status:
8
- project:
9
- default:
10
- target: 99%
11
- threshold: 1%
12
- patch:
13
- default:
14
- target: 99%
15
- threshold: 1%
16
-
17
- comment:
18
- require_changes: true