# frozen_string_literal: true require 'lite/errors' require 'lite/memoize' module Lite module Service class NotImplementedError < StandardError; end class ValidationError < StandardError; end module Command module ClassMethods def call(*args) klass = new(*args) klass.call klass end end attr_reader :result def self.prepended(base) base.extend(ClassMethods) end def call raise Lite::Service::NotImplementedError unless defined?(super) return @result if called? @called = true @result = super 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