mime_actor 1.0.1 → 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.
@@ -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,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # :markup: markdown
4
-
5
- require "active_support/version"
6
- # required by active_support/logger
7
- require "logger" if ActiveSupport.version < "7.1"
8
- # required by active_support/tagged_logging
9
- require "active_support/isolated_execution_state" if ActiveSupport.version >= "7.0"
10
-
11
- require "active_support/concern"
12
- require "active_support/configurable"
13
- require "active_support/logger"
14
- require "active_support/tagged_logging"
15
-
16
- module MimeActor
17
- # # MimeActor Logging
18
- #
19
- # Provides a configurable logger.
20
- module Logging
21
- extend ActiveSupport::Concern
22
-
23
- include ActiveSupport::Configurable
24
-
25
- included do
26
- default_logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new($stdout))
27
- if ActiveSupport.version >= "7.0"
28
- config_accessor :logger, default: default_logger
29
- else
30
- config_accessor :logger do
31
- default_logger
32
- end
33
- end
34
- end
35
-
36
- private
37
-
38
- def fill_run_sheet(*scenes, &block)
39
- return yield unless logger.respond_to?(:tagged)
40
-
41
- scenes.unshift "MimeActor" unless logger.formatter.current_tags.include?("MimeActor")
42
-
43
- logger.tagged(*scenes, &block)
44
- end
45
- end
46
- 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