# 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