lib/convenient_service/support/command.rb in convenient_service-0.12.0 vs lib/convenient_service/support/command.rb in convenient_service-0.13.0

- old
+ new

@@ -1,25 +1,64 @@ # frozen_string_literal: true module ConvenientService module Support ## - # @abstract Subclass and override {#initialize} and {#call} to implement a Command. + # @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 ## - # TODO: Specs. + # @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 ## - # TODO: Specs. Raise error if not overridden. + # @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