Sha256: 0639ab8b1752c4368222da9a40c9b64c40dc523d795789318c6aeffd646b8981

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 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(command:)
            message = <<~TEXT
              Call method (#call) of `#{command.class}` is NOT overridden.
            TEXT

            super(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
        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.16.0 lib/convenient_service/support/command.rb
convenient_service-0.15.0 lib/convenient_service/support/command.rb
convenient_service-0.14.0 lib/convenient_service/support/command.rb
convenient_service-0.13.0 lib/convenient_service/support/command.rb