Sha256: b9d1a1b9cd32bd1de446860a23b53353eb8982ad07ba94b0bc51953f1fe33ecc
Contents?: true
Size: 1.64 KB
Versions: 2
Compression:
Stored size: 1.64 KB
Contents
# frozen_string_literal: true require 'lite/errors' module Lite module Command module Extensions module Errors module ClassMethods def perform(*args, **kwargs, &block) klass = call(*args, **kwargs, &block) if klass.success? yield(klass.result, Lite::Command::Success, Lite::Command::Failure) else yield(klass.result, Lite::Command::Failure, Lite::Command::Success) end end end class << self def included(klass) klass.extend(ClassMethods) end end def errors @errors ||= Lite::Errors::Messages.new end def errored? !errors.empty? end def fail! raise Lite::Command::ValidationError end def failure? called? && errored? end def merge_errors!(klass, direction: :from) case direction when :from then errors.merge!(klass.errors) when :to then klass.errors.merge!(errors) end nil end def merge_exception!(exception, key: :internal) errors.add(key, "#{exception.class} - #{exception.message}") nil end def result! result if valid? end def status return :pending unless called? success? ? :success : :failure end def success? called? && !errored? end def validate! return true if success? fail! end alias valid? validate! end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
lite-command-1.3.1 | lib/lite/command/extensions/errors.rb |
lite-command-1.3.0 | lib/lite/command/extensions/errors.rb |