Sha256: 854134478b1b622e3543dc8abfc016972474c116a33a9dd2cff0a2970e418f5f

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require 'lite/errors'
require 'lite/memoize'

module Lite
  module Service
    class Command

      class << self

        def call(*args)
          klass = new(*args)
          klass.call
          klass
        end

      end

      attr_reader :result

      def initialize(*args)
        @args = args
      end

      def call
        raise Lite::Service::NotImplementedError unless defined?(run)
        return @result if called?

        @called = true
        @result = run
      end

      def cache
        @cache ||= Lite::Memoize::Instance.new
      end

      def called?
        @called ||= false
      end

      def errors
        @errors ||= Lite::Errors::Messages.new
      end

      def errored?
        !errors.empty?
      end

      def fail!
        raise Lite::Service::ValidationError
      end

      def failure?
        called? && errored?
      end

      def recall!
        @called = false
        [cache, errors].each(&:clear)
        call
      end

      def result!
        result if valid?
      end

      def success?
        called? && !errored?
      end

      def validate!
        return true if success?

        fail!
      end

      alias valid? validate!

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lite-service-1.0.1 lib/lite/service/command.rb