mime_actor 1.0.0 → 1.1.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.
@@ -2,112 +2,119 @@
2
2
 
3
3
  # :markup: markdown
4
4
 
5
- require "active_support/concern"
6
5
  require "active_support/core_ext/object/blank"
7
6
  require "set" # required by mime_type with ruby <= 3.1
8
7
  require "action_dispatch/http/mime_type"
8
+ require "action_pack/version"
9
9
 
10
10
  module MimeActor
11
11
  # # MimeActor Validator
12
12
  #
13
- # Validator provides validation rules for `action`, `format`, and `with` handlers by default.
13
+ # Validator provides validation rules for `action`, `format`, and `with` handlers.
14
14
  #
15
15
  # @example Raise error when rule is violated
16
- # validate!(:action, action_param)
16
+ # MimeActor::Validator.validate!(:action, action_param)
17
17
  #
18
18
  # @example Return the error when rule is violated
19
- # ex = validate_action(action_param)
20
- # raise ex if error
19
+ # error = MimeActor::Validator.validate_action(action_param)
20
+ # raise error if error
21
21
  #
22
22
  module Validator
23
- extend ActiveSupport::Concern
23
+ module_function
24
24
 
25
- included do
26
- mattr_accessor :scene_formats, instance_writer: false, default: Mime::SET.symbols.to_set
25
+ # Raise the error returned by validator if any.
26
+ #
27
+ # @param rule the name of validator
28
+ def validate!(rule, *args)
29
+ validator = "validate_#{rule}"
30
+ raise NameError, "Validator not found, got: #{validator.inspect}" unless respond_to?(validator)
31
+
32
+ error = public_send(validator, *args)
33
+ raise error if error
27
34
  end
28
35
 
29
- module ClassMethods
30
- # Raise the error returned by validator if any.
31
- #
32
- # @param rule the name of validator
33
- def validate!(rule, *args)
34
- validator = "validate_#{rule}"
35
- raise NameError, "Validator not found, got: #{validator.inspect}" unless respond_to?(validator)
36
+ # Validate `action` must be a Symbol
37
+ #
38
+ # @param unchecked the `action` to be validated
39
+ def validate_action(unchecked)
40
+ TypeError.new("action must be a Symbol") unless unchecked.is_a?(Symbol)
41
+ end
36
42
 
37
- error = send(validator, *args)
38
- raise error if error
39
- end
43
+ # Validate `actions` must be a collection of Symbol
44
+ #
45
+ # @param unchecked the `actions` to be validated
46
+ def validate_actions(unchecked)
47
+ return TypeError.new("actions must not be empty") if unchecked.empty?
40
48
 
41
- # Validate `action` must be a Symbol
42
- #
43
- # @param unchecked the `action` to be validated
44
- def validate_action(unchecked)
45
- TypeError.new("action must be a Symbol") unless unchecked.is_a?(Symbol)
46
- end
49
+ rejected = unchecked.grep_v(Symbol)
50
+ NameError.new("invalid actions, got: #{rejected.map(&:inspect).join(", ")}") if rejected.size.positive?
51
+ end
47
52
 
48
- # Validate `actions` must be a collection of Symbol
49
- #
50
- # @param unchecked the `actions` to be validated
51
- def validate_actions(unchecked)
52
- return TypeError.new("actions must not be empty") if unchecked.empty?
53
+ # Validate against `actions` rule if argument is a Enumerable. otherwise, validate against `action` rule.
54
+ #
55
+ # @param unchecked the `actions` or `action` to be validated
56
+ def validate_action_or_actions(unchecked)
57
+ unchecked.is_a?(Enumerable) ? validate_actions(unchecked) : validate_action(unchecked)
58
+ end
53
59
 
54
- rejected = unchecked.reject { |action| action.is_a?(Symbol) }
55
- NameError.new("invalid actions, got: #{rejected.map(&:inspect).join(", ")}") if rejected.size.positive?
56
- end
60
+ # Validate `format` must be a Symbol and a valid MIME type
61
+ #
62
+ # @param unchecked the `format` to be validated
63
+ def validate_format(unchecked)
64
+ return TypeError.new("format must be a Symbol") unless unchecked.is_a?(Symbol)
57
65
 
58
- # Validate against `actions` rule if argument is a Enumerable. otherwise, validate against `action` rule.
59
- #
60
- # @param unchecked the `actions` or `action` to be validated
61
- def validate_action_or_actions(unchecked)
62
- unchecked.is_a?(Enumerable) ? validate_actions(unchecked) : validate_action(unchecked)
63
- end
66
+ NameError.new("invalid format, got: #{unchecked.inspect}") unless supported_format?(unchecked)
67
+ end
64
68
 
65
- # Validate `format` must be a Symbol and a valid MIME type
66
- #
67
- # @param unchecked the `format` to be validated
68
- def validate_format(unchecked)
69
- return TypeError.new("format must be a Symbol") unless unchecked.is_a?(Symbol)
69
+ # Validate `formats` must be an collection of Symbol which each of them is a valid MIME type
70
+ #
71
+ # @param unchecked the `formats` to be validated
72
+ def validate_formats(unchecked)
73
+ return TypeError.new("formats must not be empty") if unchecked.empty?
70
74
 
71
- NameError.new("invalid format, got: #{unchecked.inspect}") unless scene_formats.include?(unchecked)
72
- end
75
+ rejected = unchecked.to_set.reject { |format| supported_format?(format) }
73
76
 
74
- # Validate `formats` must be an collection of Symbol which each of them is a valid MIME type
75
- #
76
- # @param unchecked the `formats` to be validated
77
- def validate_formats(unchecked)
78
- return TypeError.new("formats must not be empty") if unchecked.empty?
77
+ NameError.new("invalid formats, got: #{rejected.map(&:inspect).join(", ")}") if rejected.size.positive?
78
+ end
79
79
 
80
- unfiltered = unchecked.to_set
81
- filtered = unfiltered & scene_formats
82
- rejected = unfiltered - filtered
80
+ # Validate against `formats` rule if argument is a Enumerable. otherwise, validate against `format` rule.
81
+ #
82
+ # @param unchecked the `formats` or `format` to be validated
83
+ def validate_format_or_formats(unchecked)
84
+ unchecked.is_a?(Enumerable) ? validate_formats(unchecked) : validate_format(unchecked)
85
+ end
83
86
 
84
- NameError.new("invalid formats, got: #{rejected.map(&:inspect).join(", ")}") if rejected.size.positive?
85
- end
87
+ # Validate `klazz` must be a Class/Module or a String referencing a Class/Module
88
+ #
89
+ # @param unchecked the `klazz` to be validated
90
+ def validate_klazz(unchecked)
91
+ return if unchecked.is_a?(Module) || unchecked.is_a?(String)
86
92
 
87
- # Validate against `formats` rule if argument is a Enumerable. otherwise, validate against `format` rule.
88
- #
89
- # @param unchecked the `formats` or `format` to be validated
90
- def validate_format_or_formats(unchecked)
91
- unchecked.is_a?(Enumerable) ? validate_formats(unchecked) : validate_format(unchecked)
93
+ TypeError.new("#{unchecked.inspect} must be a Class/Module or a String referencing a Class/Module")
94
+ end
95
+
96
+ # Validate `callable` must be a Symbol or Proc, a lambda must not require more than `max_arguments`
97
+ #
98
+ # @param unchecked the `callable` to be validated
99
+ # @param max_arguments the arguments the `callable` is called with
100
+ def validate_callable(unchecked, max_arguments = 0)
101
+ unless unchecked.is_a?(Proc) || unchecked.is_a?(Symbol)
102
+ return TypeError.new("#{unchecked.inspect} must be a Symbol or Proc")
92
103
  end
104
+ return unless unchecked.is_a?(Proc) && unchecked.lambda?
93
105
 
94
- # Validate `klazz` must be a Class/Module or a String referencing a Class/Module
95
- #
96
- # @param unchecked the `klazz` to be validated
97
- def validate_klazz(unchecked)
98
- return if unchecked.is_a?(Module) || unchecked.is_a?(String)
106
+ required = unchecked.parameters.count { |type, _| type == :req }
107
+ return if required <= max_arguments
99
108
 
100
- TypeError.new("#{unchecked.inspect} must be a Class/Module or a String referencing a Class/Module")
101
- end
109
+ requirement = max_arguments.zero? ? "arguments" : "more than #{max_arguments} argument"
110
+ ArgumentError.new("#{unchecked.inspect} must not require #{requirement}")
111
+ end
102
112
 
103
- # Validate `callable` must be a Symbol or Proc
104
- #
105
- # @param unchecked the `callable` to be validated
106
- def validate_callable(unchecked)
107
- return if unchecked.is_a?(Proc) || unchecked.is_a?(Symbol)
113
+ def supported_format?(format)
114
+ return Mime::SET.symbols.include?(format) if ActionPack.version < Gem::Version.new("7.1")
108
115
 
109
- TypeError.new("#{unchecked.inspect} must be a Symbol or Proc")
110
- end
116
+ Mime.symbols.include?(format)
111
117
  end
118
+ private_class_method :supported_format?
112
119
  end
113
120
  end
@@ -13,7 +13,7 @@ module MimeActor
13
13
 
14
14
  module VERSION
15
15
  MAJOR = 1
16
- MINOR = 0
16
+ MINOR = 1
17
17
  BUILD = 0
18
18
  PRE = nil
19
19
 
data/lib/mime_actor.rb CHANGED
@@ -3,24 +3,11 @@
3
3
  # :markup: markdown
4
4
  # :include: ../README.md
5
5
 
6
+ require "active_support/version"
7
+ # active_support < 7.1 uses Logger without requiring it
8
+ require "logger" if ActiveSupport.version < Gem::Version.new("7.1")
9
+ require "active_support" # load the core before its components, required by rails >= 8.2
10
+
6
11
  require "mime_actor/version"
7
12
  require "mime_actor/errors"
8
-
9
- # load mime_actor components
10
13
  require "mime_actor/action"
11
- require "mime_actor/callbacks"
12
- require "mime_actor/logging"
13
- require "mime_actor/rescue"
14
- require "mime_actor/scene"
15
- require "mime_actor/stage"
16
- require "mime_actor/validator"
17
-
18
- require "mime_actor/deprecator"
19
-
20
- begin
21
- require "rails"
22
- rescue LoadError
23
- # noop
24
- end
25
-
26
- require "mime_actor/railtie" if defined?(Rails::Railtie)
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mime_actor
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Chang
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-10-03 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: actionpack
@@ -51,14 +50,9 @@ files:
51
50
  - lib/mime_actor.rb
52
51
  - lib/mime_actor/action.rb
53
52
  - lib/mime_actor/callbacks.rb
54
- - lib/mime_actor/deprecator.rb
55
- - lib/mime_actor/dispatcher.rb
56
53
  - lib/mime_actor/errors.rb
57
- - lib/mime_actor/logging.rb
58
- - lib/mime_actor/railtie.rb
59
54
  - lib/mime_actor/rescue.rb
60
55
  - lib/mime_actor/scene.rb
61
- - lib/mime_actor/stage.rb
62
56
  - lib/mime_actor/validator.rb
63
57
  - lib/mime_actor/version.rb
64
58
  - sig/mime_actor.rbs
@@ -71,7 +65,6 @@ metadata:
71
65
  homepage_uri: https://github.com/ryancyq/mime_actor
72
66
  source_code_uri: https://github.com/ryancyq/mime_actor
73
67
  changelog_uri: https://github.com/ryancyq/mime_actor/blob/main/CHANGELOG.md
74
- post_install_message:
75
68
  rdoc_options: []
76
69
  require_paths:
77
70
  - lib
@@ -86,8 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
79
  - !ruby/object:Gem::Version
87
80
  version: '0'
88
81
  requirements: []
89
- rubygems_version: 3.5.16
90
- signing_key:
82
+ rubygems_version: 3.6.9
91
83
  specification_version: 4
92
84
  summary: Action processing with Callback + Rescue handlers for different MIME types
93
85
  in Rails
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # :markup: markdown
4
-
5
- require "active_support/deprecation"
6
-
7
- module MimeActor
8
- def self.deprecator
9
- @deprecator ||= ActiveSupport::Deprecation.new("0.8.0", "MimeActor")
10
- end
11
- end
@@ -1,65 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # :markup: markdown
4
-
5
- require "mime_actor/errors"
6
-
7
- module MimeActor
8
- module Dispatcher
9
- class MethodCall
10
- attr_reader :method_name, :args
11
-
12
- def initialize(method_name, *args)
13
- @method_name = method_name
14
- @args = args
15
- validate!
16
- end
17
-
18
- def call(target)
19
- raise MimeActor::ActorNotFound, method_name unless target.respond_to?(method_name, true)
20
-
21
- method_call = target.method(method_name)
22
- filtered_args = method_call.arity.negative? ? args : args.take(method_call.arity)
23
- method_call.call(*filtered_args)
24
- end
25
-
26
- private
27
-
28
- def validate!
29
- return if method_name.is_a?(String) || method_name.is_a?(Symbol)
30
-
31
- raise ArgumentError, "invalid method name: #{method_name.inspect}"
32
- end
33
- end
34
-
35
- class InstanceExec
36
- attr_reader :block, :args
37
-
38
- def initialize(block, *args)
39
- @block = block
40
- @args = args
41
- validate!
42
- end
43
-
44
- def call(target)
45
- filtered_args = block.arity.negative? ? args : args.take(block.arity)
46
- target.instance_exec(*filtered_args, &block)
47
- end
48
-
49
- private
50
-
51
- def validate!
52
- raise ArgumentError, "invalid block: #{block.inspect}" unless block.is_a?(Proc)
53
- end
54
- end
55
-
56
- def self.build(callable, *args)
57
- case callable
58
- when String, Symbol
59
- MethodCall.new(callable, *args)
60
- when Proc
61
- InstanceExec.new(callable, *args)
62
- end
63
- end
64
- end
65
- end
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # :markup: markdown
4
-
5
- # required by active_support/tagged_logging
6
- require "active_support/version"
7
- require "active_support/isolated_execution_state" if ActiveSupport.version >= "7.0"
8
-
9
- require "active_support/concern"
10
- require "active_support/configurable"
11
- require "active_support/logger"
12
- require "active_support/tagged_logging"
13
-
14
- module MimeActor
15
- # # MimeActor Logging
16
- #
17
- # Provides a configurable logger.
18
- module Logging
19
- extend ActiveSupport::Concern
20
-
21
- include ActiveSupport::Configurable
22
-
23
- included do
24
- default_logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new($stdout))
25
- if ActiveSupport.version >= "7.0"
26
- config_accessor :logger, default: default_logger
27
- else
28
- config_accessor :logger do
29
- default_logger
30
- end
31
- end
32
- end
33
-
34
- private
35
-
36
- def fill_run_sheet(*scenes, &block)
37
- return yield unless logger.respond_to?(:tagged)
38
-
39
- scenes.unshift "MimeActor" unless logger.formatter.current_tags.include?("MimeActor")
40
-
41
- logger.tagged(*scenes, &block)
42
- end
43
- end
44
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # :markup: markdown
4
-
5
- module MimeActor
6
- class Railtie < Rails::Railtie
7
- initializer "mime_actor.deprecator", before: :load_environment_config do |app|
8
- app.deprecators[:mime_actor] = MimeActor.deprecator if ActiveSupport.version >= "7.1"
9
- end
10
- end
11
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # :markup: markdown
4
-
5
- require "mime_actor/callbacks"
6
- require "mime_actor/dispatcher"
7
- require "mime_actor/logging"
8
- require "mime_actor/rescue"
9
-
10
- require "active_support/concern"
11
- require "active_support/core_ext/module/attribute_accessors"
12
-
13
- module MimeActor
14
- # # MimeActor Stage
15
- #
16
- # Stage provides helper methods for actor lookup and invocation.
17
- #
18
- module Stage
19
- extend ActiveSupport::Concern
20
-
21
- include Callbacks
22
- include Rescue
23
- include Logging
24
-
25
- included do
26
- mattr_accessor :raise_on_actor_error, instance_writer: false, default: false
27
- end
28
-
29
- # Calls the `actor` and passing arguments to it.
30
- # If a block is given, the result from the `actor` method will be yieled to the block.
31
- #
32
- # NOTE: method call on actor if it is String or Symbol. Proc#call if actor is Proc
33
- #
34
- # @param actor either a method name or a Proc to evaluate
35
- # @param args arguments to be passed when calling the actor
36
- #
37
- def cue_actor(actor, *args, action:, format:)
38
- dispatcher = MimeActor::Dispatcher.build(actor, *args)
39
- raise TypeError, "invalid actor, got: #{actor.inspect}" unless dispatcher
40
-
41
- self.class.validate!(:format, format)
42
-
43
- run_act_callbacks(action: action, format: format) do
44
- result = dispatcher.call(self)
45
- block_given? ? yield(result) : result
46
- end
47
- rescue MimeActor::ActorNotFound => e
48
- logger.error { "actor error, cause: #{e.inspect}" } unless raise_on_actor_error
49
- raise e if raise_on_actor_error
50
- rescue StandardError => e
51
- rescued = rescue_actor(e, action: action, format: format)
52
- rescued || raise
53
- end
54
- end
55
- end