# frozen_string_literal: true module Composable module Core module Command class NotImplementedError < ::StandardError; end attr_reader :result module ClassMethods def call(*args, **kwargs) new(*args, **kwargs).call end def call!(*args, **kwargs) new(*args, **kwargs).call(raise_exception: true) end end def self.prepended(base) base.extend ClassMethods base.include ActiveModel::Validations base.include ActiveSupport::Rescuable end def call(raise_exception: false) fail NotImplementedError unless defined?(super) @called = true @result = super() raise Error, errors.full_messages.to_sentence if raise_exception && failure? self rescue Exception => exception raise if raise_exception || !rescue_with_handler(exception) self end def success? called? && !failure? end def failure? called? && errors.any? end private def called? @called ||= false end end end end