Sha256: db0b77edefd0c23bb1cb15b7b763a01a919d44d6e07643c9b8595af613d8cc99

Contents?: true

Size: 918 Bytes

Versions: 1

Compression:

Stored size: 918 Bytes

Contents

require "active_support"
require "active_model"

module UseCasePattern
  module Base
    extend ActiveSupport::Concern
    include ActiveModel::Validations

    module ClassMethods
      # The perform method of a UseCase should always return itself
      def perform(*args)
        new(*args).tap { |use_case| use_case.perform }
      end

      # Raise an exception if perform generates any errors
      def perform!(*args)
        new(*args).tap { |use_case| use_case.perform! }
      end
    end

    # Implement all the steps required to complete this use case
    def perform
      raise NotImplementedError
    end

    def perform!
      perform

      if failure?
        raise_validation_error
      end
    end

    # Has this use case performed its task successfully?
    def success?
      errors.none?
    end

    # Did this use case have any errors?
    def failure?
      errors.any?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
use_case_pattern-1.0.0 lib/use_case_pattern/base.rb