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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5eb8de0fc146859794812ae3cb81cb99a1a775668af9681e22409fd8df0b245e
4
- data.tar.gz: b39aa38f9a21c2b8a794ba1edaf01e184fbe0277cc256721e61df9f2fa98a3c4
3
+ metadata.gz: 031455cd4203f02b1760129516e1a8722af2f84d7580cf1799f886d98262cdea
4
+ data.tar.gz: 78b2841148310600b408fd3d2932741ab61280f24afb28f0b251e62bcba91419
5
5
  SHA512:
6
- metadata.gz: c4c8f432f1cd6d7a1678b9ed1da3cada46a28ed9406003f4fa9cac78375567dbc15f600c11cf3ef10acd76c8d4449b24385db6397be8f5dab6fc9f1ab6b9c3d9
7
- data.tar.gz: bbe236716e0a3969737388f9390349563d088581486b17b8601b150bf88ccaf8a68beadb913c1496cfad0652727a17689a0f2ad9db46c4917bac6449e36e8a21
6
+ metadata.gz: 2852a58bb4dc628d293e6d5974b452144496be454a2a594aec90baaffcf5fbbcdd096745778853f36cc5e8ef4eb577340175bd76533ba2be1852799ddf5cc2a6
7
+ data.tar.gz: f548c1d8fe2c32a2aba34c4b9189e3e02272fdcfdc850d8b88e6cb6eea54514a1c95cc8ede181656f5ba0e92111f2566db1cbc98b647dd77f0922cd2ff1357be
data/.rubocop.yml CHANGED
@@ -19,6 +19,10 @@ Style/BlockComments:
19
19
  Metrics/AbcSize:
20
20
  Enabled: false
21
21
 
22
+ Metrics/BlockLength:
23
+ AllowedMethods:
24
+ - class_methods
25
+
22
26
  Metrics/CyclomaticComplexity:
23
27
  Enabled: false
24
28
 
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
- 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
@@ -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
- 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
 
@@ -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` through configuration and passed over to `ActionController::MimeResponds`
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
  #
@@ -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
- # rescue_actor_from RuntimeError, format: :json, with: :handle_json_error
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
- module ClassMethods
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
- # rescue_actor_from StandardError, format: :html, with: :handle_html_error
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
- # rescue_actor_from StandardError, format: :json, action: :show do |ex|
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 rescue_actor_from(*klazzes, action: nil, format: nil, with: nil, &block)
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
- if action.is_a?(Enumerable)
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
- if format.is_a?(Enumerable)
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
- 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
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]
@@ -31,7 +31,7 @@ module MimeActor
31
31
  mattr_accessor :acting_scenes, instance_writer: false, default: {}
32
32
  end
33
33
 
34
- module ClassMethods
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
- # compose_scene :html, on: :index
42
+ # act_on_format :html, on: :index
43
43
  # @example register `html`, `json` formats on actions `index`, `show`
44
- # compose_scene :html, :json , on: [:index, :show]
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 compose_scene(*options)
47
+ def act_on_format(*options)
48
48
  config = options.extract_options!
49
49
  validate!(:formats, options)
50
50
 
51
- actions = config[:on]
52
- if !actions
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
- else
54
+ when Symbol, String
57
55
  validate!(:action, actions)
56
+ else
57
+ raise ArgumentError, "action is required"
58
58
  end
59
59
 
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
60
+ Array.wrap(actions).each do |action|
61
+ options.each { |format| compose_scene(action, format) }
62
+ end
63
+ end
64
64
 
65
- acting_scenes[action] ||= Set.new
66
- acting_scenes[action] |= [format]
65
+ private
67
66
 
68
- next if action_defined
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
- 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
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
- alias act_on_format compose_scene
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
@@ -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
- module ClassMethods
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` 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`
@@ -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
- module ClassMethods
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
@@ -11,8 +11,8 @@ module MimeActor
11
11
 
12
12
  module VERSION
13
13
  MAJOR = 0
14
- MINOR = 5
15
- BUILD = 4
14
+ MINOR = 6
15
+ BUILD = 0
16
16
  PRE = nil
17
17
 
18
18
  STRING = [MAJOR, MINOR, BUILD, PRE].compact.join(".")
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.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-29 00:00:00.000000000 Z
11
+ date: 2024-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack