Sha256: f1f4cbfabf1c64b27b20b76fe014efecb76371f995569c373bec15654cf33654

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module Composable
  module Core
    module Command
      extend ActiveSupport::Concern

      class NotImplementedError < ::StandardError; end

      attr_reader :result

      prepended do
        include ActiveModel::Validations
        include ActiveSupport::Rescuable
      end

      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 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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
composable-core-0.0.12 lib/composable/core/command.rb
composable-core-0.0.11 lib/composable/core/command.rb