Sha256: 1905210c25db5104253387209826af2f43d9d98d49378283ea6295ff2a37da96

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require 'lite/errors'

module Lite
  module Command
    module Extensions
      module Errors

        module ClassMethods

          def perform(*args)
            klass = call(*args)

            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
        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

1 entries across 1 versions & 1 rubygems

Version Path
lite-command-1.0.4 lib/lite/command/extensions/errors.rb