mime_actor 0.5.4 → 0.6.1.beta

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: 5eb8de0fc146859794812ae3cb81cb99a1a775668af9681e22409fd8df0b245e
4
- data.tar.gz: b39aa38f9a21c2b8a794ba1edaf01e184fbe0277cc256721e61df9f2fa98a3c4
3
+ metadata.gz: b1a2cbc83644b0f085441dbcac4ec82cf9b9cc5336f3df9c6c795a104c952341
4
+ data.tar.gz: 73995fad83611aa2d77ca1b11ccc695c0f3b0984378797de1b30de73696d6089
5
5
  SHA512:
6
- metadata.gz: c4c8f432f1cd6d7a1678b9ed1da3cada46a28ed9406003f4fa9cac78375567dbc15f600c11cf3ef10acd76c8d4449b24385db6397be8f5dab6fc9f1ab6b9c3d9
7
- data.tar.gz: bbe236716e0a3969737388f9390349563d088581486b17b8601b150bf88ccaf8a68beadb913c1496cfad0652727a17689a0f2ad9db46c4917bac6449e36e8a21
6
+ metadata.gz: cb58712d60fa26fa4d5710b5abdc239a7d9e3e8d96808523572df64e32df411d2811d93bf6a1e2529c2f3bc7f56efeb806c8763dcc52beb516f06d341cdae93a
7
+ data.tar.gz: 554989721dfaf823d0fc5232b3855d47507f9812ceba7e6cff430bd5ac0984eafe9e586732c69bec04e61dd9e2ea6279f4036b90b0b91d4719d1d737e6785099
data/.rubocop.yml CHANGED
@@ -16,9 +16,16 @@ Style/BlockComments:
16
16
  Exclude:
17
17
  - spec/*_helper.rb
18
18
 
19
+ Style/RegexpLiteral:
20
+ EnforcedStyle: percent_r
21
+
19
22
  Metrics/AbcSize:
20
23
  Enabled: false
21
24
 
25
+ Metrics/BlockLength:
26
+ AllowedMethods:
27
+ - class_methods
28
+
22
29
  Metrics/CyclomaticComplexity:
23
30
  Enabled: false
24
31
 
data/COMPARE.md CHANGED
@@ -27,7 +27,7 @@ class EventsController < ActionController::Base
27
27
  before_action only: :index { @events = Event.all }
28
28
 
29
29
  # dynamically defines the action method according to on: argument
30
- act_on_format :html, :json, on: :index
30
+ respond_act_to :html, :json, on: :index
31
31
 
32
32
  def index_html
33
33
  @event_categories = EventCategory.all
@@ -92,13 +92,13 @@ class EventsController < ActionController::Base
92
92
  # AbstractController::Callbacks here to load model with params
93
93
  before_action only: [:show, :update] { @event = Event.find(params.require(:event_id)) }
94
94
 
95
- act_on_format :html, :json, on: [:show, :update]
95
+ respond_act_to :html, :json, on: [:show, :update]
96
96
 
97
- rescue_actor_from ActiveRecord::RecordNotFound, format: :json do |ex|
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
- rescue_actor_from ActiveRecord::RecordNotFound, format: :html, action: :show do |ex|
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
@@ -16,14 +16,14 @@ class EventsController < ActionController::Base
16
16
  before_action only: :index { @events = Event.all }
17
17
  before_action only: [:show, :update] { @event = Event.find(params.require(:event_id)) }
18
18
 
19
- act_on_format :html, :json, on: :index
20
- act_on_format :html, :json, on: [:show, :update]
19
+ respond_act_to :html, :json, on: :index
20
+ respond_act_to :html, :json, on: [:show, :update]
21
21
 
22
- rescue_actor_from ActiveRecord::RecordNotFound, format: :json do |ex|
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
- rescue_actor_from ActiveRecord::RecordNotFound, format: :html, action: :show do |ex|
26
+ rescue_act_from ActiveRecord::RecordNotFound, format: :html, action: :show do |ex|
27
27
  redirect_to events_path
28
28
  end
29
29
 
@@ -99,7 +99,7 @@ Please see [LICENSE](https://github.com/ryancyq/mime_actor/blob/main/LICENSE) fo
99
99
 
100
100
  ## Contributing
101
101
 
102
- Bug reports and pull requests are welcome on GitHub at https://github.com/ryancyq/mime_actor.
102
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/ryancyq/mime_actor](https://github.com/ryancyq/mime_actor).
103
103
 
104
104
  [rubygems_badge]: https://img.shields.io/gem/v/mime_actor.svg
105
105
  [rubygems]: https://rubygems.org/gems/mime_actor
data/codecov.yml ADDED
@@ -0,0 +1,18 @@
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
@@ -1,10 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # :markup: markdown
4
+
3
5
  require "mime_actor/scene"
4
6
  require "mime_actor/stage"
5
7
  require "mime_actor/rescue"
6
8
 
7
9
  require "active_support/concern"
10
+ require "active_support/core_ext/object/blank"
8
11
  require "active_support/lazy_load_hooks"
9
12
  require "abstract_controller/rendering"
10
13
  require "action_controller/metal/mime_responds"
@@ -27,7 +30,7 @@ module MimeActor
27
30
  include Rescue
28
31
  include Logging
29
32
 
30
- # The core logic where rendering logics are collected as `Proc` through configuration and passed over to `ActionController::MimeResponds`
33
+ # The core logic where rendering logics are collected as `Proc` and passed over to `ActionController::MimeResponds`
31
34
  #
32
35
  # @param action the `action` of the controller
33
36
  #
@@ -44,17 +47,17 @@ module MimeActor
44
47
  #
45
48
  def start_scene(action)
46
49
  action = action&.to_sym
47
- formats = acting_scenes.fetch(action, Set.new)
50
+ formats = acting_scenes.fetch(action, {})
48
51
 
49
52
  if formats.empty?
50
- logger.warn { "format is empty for action :#{action}" }
53
+ logger.warn { "format is empty for action: #{action.inspect}" }
51
54
  return
52
55
  end
53
56
 
54
57
  respond_to do |collector|
55
- formats.each do |format|
56
- dispatch = self.class.dispatch_cue(action: action, format: format, context: self) do
57
- cue_actor("#{action}_#{format}")
58
+ formats.each do |format, actor|
59
+ dispatch = self.class.dispatch_act(action: action, format: format, context: self) do
60
+ cue_actor(actor.presence || "#{action}_#{format}")
58
61
  end
59
62
  collector.public_send(format, &dispatch)
60
63
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # :markup: markdown
4
+
3
5
  require "set"
4
6
 
5
7
  module MimeActor
@@ -24,7 +26,7 @@ module MimeActor
24
26
 
25
27
  class ActorNotFound < ActorError
26
28
  def generate_message
27
- ":#{actor} not found"
29
+ "#{actor.inspect} not found"
28
30
  end
29
31
  end
30
32
 
@@ -39,7 +41,7 @@ module MimeActor
39
41
 
40
42
  class ActionExisted < ActionError
41
43
  def generate_message
42
- "action :#{action} already existed"
44
+ "action #{action.inspect} already existed"
43
45
  end
44
46
  end
45
47
  end
@@ -8,6 +8,7 @@ require "mime_actor/validator"
8
8
  require "active_support/concern"
9
9
  require "active_support/core_ext/array/wrap"
10
10
  require "active_support/core_ext/module/attribute_accessors"
11
+ require "active_support/core_ext/object/blank"
11
12
  require "active_support/core_ext/string/inflections"
12
13
 
13
14
  module MimeActor
@@ -16,7 +17,7 @@ module MimeActor
16
17
  # Simillar to `ActionController::Rescue` but with additional filtering logic on `action`/`format`.
17
18
  #
18
19
  # @example Rescue RuntimeError when raised for any action with `json` format
19
- # rescue_actor_from RuntimeError, format: :json, with: :handle_json_error
20
+ # rescue_act_from RuntimeError, format: :json, with: :handle_json_error
20
21
  #
21
22
  module Rescue
22
23
  extend ActiveSupport::Concern
@@ -34,49 +35,38 @@ module MimeActor
34
35
  # @param klazzes the error classes to rescue
35
36
  # @param action the `action` filter
36
37
  # @param format the `format` filter
37
- # @param with the rescue hanlder when `block` is not provided
38
+ # @param with the rescue handler when `block` is not provided
38
39
  # @param block the `block` to evaluate when `with` is not provided
39
40
  #
40
41
  # @example Rescue StandardError when raised for any action with `html` format
41
- # rescue_actor_from StandardError, format: :html, with: :handle_html_error
42
+ # rescue_act_from StandardError, format: :html, with: :handle_html_error
42
43
  #
43
44
  # @example Rescue StandardError when raised for `show` action with `json` format
44
- # rescue_actor_from StandardError, format: :json, action: :show do |ex|
45
+ # rescue_act_from StandardError, format: :json, action: :show do |ex|
45
46
  # render status: :bad_request, json: { error: ex.message }
46
47
  # end
47
48
  #
48
- def rescue_actor_from(*klazzes, action: nil, format: nil, with: nil, &block)
49
+ def rescue_act_from(*klazzes, action: nil, format: nil, with: nil, &block)
49
50
  raise ArgumentError, "error filter is required" if klazzes.empty?
51
+ raise ArgumentError, "provide either the with: argument or a block" unless with.present? ^ block_given?
50
52
 
51
- validate!(:with, with, block)
52
- with = block if block_given?
53
+ if block_given?
54
+ with = block
55
+ else
56
+ validate!(:with, with)
57
+ end
53
58
 
54
59
  if action.present?
55
- if action.is_a?(Enumerable)
56
- validate!(:actions, action)
57
- else
58
- validate!(:action, action)
59
- end
60
+ action.is_a?(Enumerable) ? validate!(:actions, action) : validate!(:action, action)
60
61
  end
61
62
 
62
63
  if format.present?
63
- if format.is_a?(Enumerable)
64
- validate!(:formats, format)
65
- else
66
- validate!(:format, format)
67
- end
64
+ format.is_a?(Enumerable) ? validate!(:formats, format) : validate!(:format, format)
68
65
  end
69
66
 
70
67
  klazzes.each do |klazz|
71
- error = case klazz
72
- when Module
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
68
+ validate!(:klazz, klazz)
69
+ error = klazz.is_a?(Module) ? klazz.name : klazz
80
70
 
81
71
  # append at the end because strategies are read in reverse.
82
72
  actor_rescuers << [error, format, action, with]
@@ -111,26 +101,14 @@ module MimeActor
111
101
  case rescuer = find_rescuer(error, format:, action:)
112
102
  when Symbol
113
103
  rescuer_method = context.method(rescuer)
114
- case rescuer_method.arity
115
- when 0
116
- ->(_e, _f, _a) { rescuer_method.call }
117
- when 1
118
- ->(e, _f, _a) { rescuer_method.call(e) }
119
- when 2
120
- ->(e, f, _a) { rescuer_method.call(e, f) }
121
- else
122
- ->(e, f, a) { rescuer_method.call(e, f, a) }
104
+ lambda do |*args|
105
+ passable_args = rescuer_method.arity.negative? ? args : args.take(rescuer_method.arity)
106
+ rescuer_method.call(*passable_args)
123
107
  end
124
108
  when Proc
125
- case rescuer.arity
126
- when 0
127
- ->(_e, _f, _a) { context.instance_exec(&rescuer) }
128
- when 1
129
- ->(e, _f, _a) { context.instance_exec(e, &rescuer) }
130
- when 2
131
- ->(e, f, _a) { context.instance_exec(e, f, &rescuer) }
132
- else
133
- ->(e, f, a) { context.instance_exec(e, f, a, &rescuer) }
109
+ lambda do |*args|
110
+ passable_args = rescuer.arity.negative? ? args : args.take(rescuer.arity)
111
+ context.instance_exec(*passable_args, &rescuer)
134
112
  end
135
113
  end
136
114
  end
@@ -6,9 +6,9 @@ require "mime_actor/errors"
6
6
  require "mime_actor/validator"
7
7
 
8
8
  require "active_support/concern"
9
- require "active_support/core_ext/array/extract_options"
10
9
  require "active_support/core_ext/array/wrap"
11
10
  require "active_support/core_ext/module/attribute_accessors"
11
+ require "active_support/core_ext/object/blank"
12
12
 
13
13
  module MimeActor
14
14
  # # MimeActor Scene
@@ -16,9 +16,27 @@ module MimeActor
16
16
  # Scene provides configuration for `action` + `format` definitions
17
17
  #
18
18
  # @example register a `html` format on action `index`
19
- # act_on_format :html, on: :index
19
+ # respond_act_to :html, on: :index
20
+ #
21
+ # # this method should be defined in the class
22
+ # def index_html; end
20
23
  # @example register `html`, `json` formats on actions `index`, `show`
21
- # act_on_format :html, :json , on: [:index, :show]
24
+ # respond_act_to :html, :json , on: [:index, :show]
25
+ #
26
+ # # these methods should be defined in the class
27
+ # def index_html; end
28
+ # def index_json; end
29
+ # def show_html; end
30
+ # def show_json; end
31
+ # @example register a `html` format on action `index` with respond handler method
32
+ # respond_act_to :html, on: :index, with: :render_html
33
+ #
34
+ # # this method should be defined in the class
35
+ # def render_html; end
36
+ # @example register a `html` format on action `index` with respond handler proc
37
+ # respond_act_to :html, on: :index do
38
+ # render :index
39
+ # end
22
40
  #
23
41
  # NOTE: Calling the same `action`/`format` multiple times will overwrite previous `action` + `format` definitions.
24
42
  #
@@ -34,54 +52,87 @@ module MimeActor
34
52
  module ClassMethods
35
53
  # Register `action` + `format` definitions.
36
54
  #
37
- # @param options [Array]
38
- # @option options [Array] klazzes the collection of `format`
39
- # @option options [Hash] on the collection of `action`
55
+ # @param formats the collection of `format`
56
+ # @param on the collection of `action`
57
+ # @param with the respond handler when `block` is not provided
58
+ # @param block the `block` to evaluate when `with` is not provided
40
59
  #
41
60
  # @example register a `html` format on action `index`
42
- # compose_scene :html, on: :index
61
+ # respond_act_to :html, on: :index
62
+ #
63
+ # # this method should be defined in the class
64
+ # def index_html; end
43
65
  # @example register `html`, `json` formats on actions `index`, `show`
44
- # compose_scene :html, :json , on: [:index, :show]
66
+ # respond_act_to :html, :json , on: [:index, :show]
67
+ #
68
+ # # these methods should be defined in the class
69
+ # def index_html; end
70
+ # def index_json; end
71
+ # def show_html; end
72
+ # def show_json; end
73
+ # @example register a `html` format on action `index` with respond handler method
74
+ # respond_act_to :html, on: :index, with: :render_html
75
+ #
76
+ # # this method should be defined in the class
77
+ # def render_html; end
78
+ # @example register a `html` format on action `index` with respond handler proc
79
+ # respond_act_to :html, on: :index do
80
+ # render :index
81
+ # end
45
82
  #
46
83
  # For each unique `action` being registered, it will have a corresponding `action` method being defined.
47
- def compose_scene(*options)
48
- config = options.extract_options!
49
- validate!(:formats, options)
84
+ def respond_act_to(*formats, on: nil, with: nil, &block)
85
+ validate!(:formats, formats)
50
86
 
51
- actions = config[:on]
52
- if !actions
53
- raise ArgumentError, "action is required"
54
- elsif actions.is_a?(Enumerable)
87
+ raise ArgumentError, "provide either the with: argument or a block" if with.present? && block_given?
88
+
89
+ if block_given?
90
+ with = block
91
+ elsif with.present?
92
+ validate!(:with, with)
93
+ end
94
+
95
+ case actions = on
96
+ when Enumerable
55
97
  validate!(:actions, actions)
56
- else
98
+ when Symbol, String
57
99
  validate!(:action, actions)
100
+ else
101
+ raise ArgumentError, "action is required"
58
102
  end
59
103
 
60
- options.each do |format|
61
- Array.wrap(actions).each do |action|
62
- action_defined = (instance_methods + private_instance_methods).include?(action.to_sym)
63
- raise MimeActor::ActionExisted, action if !acting_scenes.key?(action) && action_defined
64
-
65
- acting_scenes[action] ||= Set.new
66
- acting_scenes[action] |= [format]
67
-
68
- next if action_defined
69
-
70
- class_eval(
71
- # def index
72
- # self.respond_to?(:start_scene) && self.start_scene(:index)
73
- # end
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
104
+ Array.wrap(actions).each do |action|
105
+ formats.each { |format| compose_scene(action, format, with) }
81
106
  end
82
107
  end
83
108
 
84
- alias act_on_format compose_scene
109
+ # TODO: remove on next breaking change release
110
+ alias act_on_format respond_act_to
111
+
112
+ private
113
+
114
+ def compose_scene(action, format, actor)
115
+ action_defined = (instance_methods + private_instance_methods).include?(action.to_sym)
116
+ raise MimeActor::ActionExisted, action if !acting_scenes.key?(action) && action_defined
117
+
118
+ acting_scenes[action] ||= {}
119
+ acting_scenes[action][format] = actor
120
+
121
+ define_scene(action) unless action_defined
122
+ end
123
+
124
+ def define_scene(action)
125
+ module_eval(
126
+ # def index
127
+ # self.respond_to?(:start_scene) && self.start_scene(:index)
128
+ # end
129
+ <<-RUBY, __FILE__, __LINE__ + 1
130
+ def #{action}
131
+ self.respond_to?(:start_scene) && self.start_scene(:#{action})
132
+ end
133
+ RUBY
134
+ )
135
+ end
85
136
  end
86
137
  end
87
138
  end
@@ -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` via `rescue_actor` if defined, otherwise, error will be re-raised
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`
@@ -45,13 +46,13 @@ module MimeActor
45
46
  # @param block the `block` to be evaluated
46
47
  #
47
48
  # @example Dispatch a cue that prints out a text
48
- # dispatch = self.class.dispatch_cue(action: :create, format: :json, context: self) do
49
+ # dispatch = self.class.dispatch_act(action: :create, format: :json, context: self) do
49
50
  # puts "completed the dispatch"
50
51
  # end
51
52
  #
52
53
  # dispatch.call == "completed the dispatch" # true
53
54
  #
54
- def dispatch_cue(action: nil, format: nil, context: self, &block)
55
+ def dispatch_act(action: nil, format: nil, context: self, &block)
55
56
  raise ArgumentError, "block must be provided" unless block_given?
56
57
 
57
58
  lambda do
@@ -60,27 +61,50 @@ module MimeActor
60
61
  (respond_to?(:rescue_actor) && rescue_actor(e, action:, format:, context:)) || raise
61
62
  end
62
63
  end
64
+
65
+ # TODO: remove on next breaking change release
66
+ alias dispatch_cue dispatch_act
63
67
  end
64
68
 
65
- # Calls the `actor` method if defined, supports passing arguments to the `actor` method.
66
- #
69
+ # Calls the `actor` and passing arguments to it.
67
70
  # If a block is given, the result from the `actor` method will be yieled to the block.
68
71
  #
69
- # @param actor_name
70
- def cue_actor(actor_name, *args)
71
- unless self.class.actor?(actor_name)
72
- raise MimeActor::ActorNotFound, actor_name if raise_on_missing_actor
73
-
74
- logger.warn { "actor not found, expected :#{actor_name}" }
75
- return
76
- end
72
+ # NOTE: method call on actor if it is String or Symbol. Proc#call if actor is Proc
73
+ #
74
+ # @param actor either a method name or a block to evaluate
75
+ def cue_actor(actor, *args)
76
+ result = case actor
77
+ when String, Symbol
78
+ actor_method_call(actor, *args)
79
+ when Proc
80
+ actor_proc_call(actor, *args)
81
+ else
82
+ raise TypeError, "invalid actor, got: #{actor.inspect}"
83
+ end
77
84
 
78
- result = public_send(actor_name, *args)
79
85
  if block_given?
80
86
  yield result
81
87
  else
82
88
  result
83
89
  end
84
90
  end
91
+
92
+ private
93
+
94
+ def actor_method_call(actor_method, *args)
95
+ unless self.class.actor?(actor_method)
96
+ raise MimeActor::ActorNotFound, actor_method if raise_on_missing_actor
97
+
98
+ logger.warn { "actor #{actor_method.inspect} not found" }
99
+ return
100
+ end
101
+
102
+ public_send(actor_method, *args)
103
+ end
104
+
105
+ def actor_proc_call(actor_proc, *args)
106
+ passable_args = actor_proc.arity.negative? ? args : args.take(actor_proc.arity)
107
+ instance_exec(*passable_args, &actor_proc)
108
+ end
85
109
  end
86
110
  end
@@ -3,6 +3,7 @@
3
3
  # :markup: markdown
4
4
 
5
5
  require "active_support/concern"
6
+ require "active_support/core_ext/object/blank"
6
7
  require "set" # required by mime_type with ruby <= 3.1
7
8
  require "action_dispatch/http/mime_type"
8
9
 
@@ -31,7 +32,7 @@ module MimeActor
31
32
  # @param rule the name of validator
32
33
  def validate!(rule, *args)
33
34
  validator = "validate_#{rule}"
34
- raise NameError, "Validator not found, got: #{validator}" unless respond_to?(validator, true)
35
+ raise NameError, "Validator not found, got: #{validator.inspect}" unless respond_to?(validator, true)
35
36
 
36
37
  error = send(validator, *args)
37
38
  raise error if error
@@ -41,7 +42,7 @@ module MimeActor
41
42
  #
42
43
  # @param unchecked the `action` to be validated
43
44
  def validate_action(unchecked)
44
- ArgumentError.new("action must be a Symbol") unless unchecked.is_a?(Symbol)
45
+ TypeError.new("action must be a Symbol") unless unchecked.is_a?(Symbol)
45
46
  end
46
47
 
47
48
  # Validate `actions` must be a collection of Symbol
@@ -49,16 +50,16 @@ module MimeActor
49
50
  # @param unchecked the `actions` to be validated
50
51
  def validate_actions(unchecked)
51
52
  rejected = unchecked.reject { |action| action.is_a?(Symbol) }
52
- NameError.new("invalid actions, got: #{rejected.join(", ")}") if rejected.size.positive?
53
+ NameError.new("invalid actions, got: #{rejected.map(&:inspect).join(", ")}") if rejected.size.positive?
53
54
  end
54
55
 
55
56
  # Validate `format` must be a Symbol and a valid MIME type
56
57
  #
57
58
  # @param unchecked the `format` to be validated
58
59
  def validate_format(unchecked)
59
- return ArgumentError.new("format must be a Symbol") unless unchecked.is_a?(Symbol)
60
+ return TypeError.new("format must be a Symbol") unless unchecked.is_a?(Symbol)
60
61
 
61
- NameError.new("invalid format, got: #{unchecked}") unless scene_formats.include?(unchecked)
62
+ NameError.new("invalid format, got: #{unchecked.inspect}") unless scene_formats.include?(unchecked)
62
63
  end
63
64
 
64
65
  # Validate `formats` must be an collection of Symbol which each of them is a valid MIME type
@@ -69,25 +70,25 @@ module MimeActor
69
70
  filtered = unfiltered & scene_formats
70
71
  rejected = unfiltered - filtered
71
72
 
72
- NameError.new("invalid formats, got: #{rejected.join(", ")}") if rejected.size.positive?
73
+ NameError.new("invalid formats, got: #{rejected.map(&:inspect).join(", ")}") if rejected.size.positive?
73
74
  end
74
75
 
75
- # Validate `with` or `block` must be provided and if `with` is provided, it must be a Symbol or Proc
76
+ # Validate `klazz` must be a Class/Module or a String referencing a Class/Module
77
+ #
78
+ # @param unchecked the `klazz` to be validated
79
+ def validate_klazz(unchecked)
80
+ return if unchecked.is_a?(Module) || unchecked.is_a?(String)
81
+
82
+ TypeError.new("#{unchecked.inspect} must be a Class/Module or a String referencing a Class/Module")
83
+ end
84
+
85
+ # Validate `with` must be a Symbol or Proc
76
86
  #
77
87
  # @param unchecked the `with` to be validated
78
- # @param block the `block` to be valiated
79
- def validate_with(unchecked, block)
80
- if unchecked.present? && block.present?
81
- return ArgumentError.new("provide either the with: keyword argument or a block")
82
- end
83
- unless unchecked.present? || block.present?
84
- return ArgumentError.new("provide the with: keyword argument or a block")
85
- end
86
-
87
- return if block.present?
88
+ def validate_with(unchecked)
88
89
  return if unchecked.is_a?(Proc) || unchecked.is_a?(Symbol)
89
90
 
90
- ArgumentError.new("with handler must be a Symbol or Proc, got: #{unchecked.inspect}")
91
+ TypeError.new("with handler must be a Symbol or Proc, got: #{unchecked.inspect}")
91
92
  end
92
93
  end
93
94
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # :markup: markdown
4
+
3
5
  module MimeActor
4
6
  def self.version
5
7
  gem_version
@@ -11,9 +13,9 @@ module MimeActor
11
13
 
12
14
  module VERSION
13
15
  MAJOR = 0
14
- MINOR = 5
15
- BUILD = 4
16
- PRE = nil
16
+ MINOR = 6
17
+ BUILD = 1
18
+ PRE = "beta"
17
19
 
18
20
  STRING = [MAJOR, MINOR, BUILD, PRE].compact.join(".")
19
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.5.4
4
+ version: 0.6.1.beta
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-29 00:00:00.000000000 Z
11
+ date: 2024-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -68,6 +68,7 @@ files:
68
68
  - LICENSE
69
69
  - README.md
70
70
  - Rakefile
71
+ - codecov.yml
71
72
  - lib/mime_actor.rb
72
73
  - lib/mime_actor/action.rb
73
74
  - lib/mime_actor/errors.rb