Sha256: 0d6065a6cde4515bb2d491d3fb706be959dcde925002fa38b6e9a4ed0351486f

Contents?: true

Size: 1.63 KB

Versions: 8

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Service
    module Plugins
      module HasResultMethodSteps
        module Services
          class RunOwnMethodInOrganizer
            include MethodStepConfig

            attr_reader :method_name, :organizer, :kwargs

            def initialize(method_name:, organizer:, **kwargs)
              @method_name = method_name
              @organizer = organizer
              @kwargs = kwargs
            end

            def result
              raise Errors::MethodForStepIsNotDefined.new(service_class: organizer.class, method_name: method_name) unless own_method

              ##
              # NOTE: `kwargs` are intentionally NOT passed, since all the corresponding methods are available inside `own_method` body.
              #
              own_method.call
            end

            private

            ##
            # @return [Method, nil]
            #
            # @internal
            #   TODO: A possible bottleneck. Should be removed if receives negative feedback.
            #
            #   NOTE: `own_method.bind(organizer).call` is logically the same as `own_method.bind_call(organizer)`.
            #   - https://ruby-doc.org/core-2.7.1/UnboundMethod.html#method-i-bind_call
            #   - https://blog.saeloun.com/2019/10/17/ruby-2-7-adds-unboundmethod-bind_call-method.html
            #
            def own_method
              method = Utils::Module.get_own_instance_method(organizer.class, method_name, private: true)

              return unless method

              method.bind(organizer)
            end
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
convenient_service-0.6.0 lib/convenient_service/service/plugins/has_result_method_steps/services/run_own_method_in_organizer.rb
convenient_service-0.5.0 lib/convenient_service/service/plugins/has_result_method_steps/services/run_own_method_in_organizer.rb
convenient_service-0.4.0 lib/convenient_service/service/plugins/has_result_method_steps/services/run_own_method_in_organizer.rb
convenient_service-0.3.1 lib/convenient_service/service/plugins/has_result_method_steps/services/run_own_method_in_organizer.rb
convenient_service-0.3.0 lib/convenient_service/service/plugins/has_result_method_steps/services/run_own_method_in_organizer.rb
convenient_service-0.2.1 lib/convenient_service/service/plugins/has_result_method_steps/services/run_own_method_in_organizer.rb
convenient_service-0.2.0 lib/convenient_service/service/plugins/has_result_method_steps/services/run_own_method_in_organizer.rb
convenient_service-0.1.0 lib/convenient_service/service/plugins/has_result_method_steps/services/run_own_method_in_organizer.rb