Sha256: e5581d3865acfc42ef24fa0312ec0ab4b019a3a1c751d2033635ac71be2b5037

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Support
    ##
    # @abstract Subclass and override {#initialize} and {#call} to implement a `Command`.
    #
    class Command
      module Exceptions
        class CallIsNotOverridden < ::ConvenientService::Exception
          def initialize_with_kwargs(command:)
            message = <<~TEXT
              Call method (#call) of `#{command.class}` is NOT overridden.
            TEXT

            initialize(message)
          end
        end
      end

      class << self
        ##
        # @return [Object] Can be any type.
        #
        def call(...)
          new(...).call
        end

        ##
        # @return [Object] Can be any type.
        #
        # @internal
        #   NOTE: Delegates to `call` instead of aliasing in order to have an ability
        #   to use the same RSpec stubs for short and usual syntax.
        #
        #   For example:
        #
        #     allow(Command).to receive(:call).with(:foo).and_call_original
        #
        #   works for both `Command.call(:foo)` and `Command[:foo]`.
        #
        def [](...)
          call(...)
        end
      end

      ##
      # @abstract
      # @return [void]
      #
      def initialize(...)
      end

      ##
      # @abstract
      # @return [Object] Can be any type.
      # @raise [ConvenientService::Support::Command::Exceptions::CallIsNotOverridden]
      #
      def call
        ::ConvenientService.raise Exceptions::CallIsNotOverridden.new(command: self)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
convenient_service-0.19.1 lib/convenient_service/support/command.rb
convenient_service-0.19.0 lib/convenient_service/support/command.rb
convenient_service-0.18.0 lib/convenient_service/support/command.rb
convenient_service-0.17.0 lib/convenient_service/support/command.rb