Sha256: e476850d506a79e8a965219ad36400a5f8cc7363f761ce16b42b2985d8ed3e05

Contents?: true

Size: 1.81 KB

Versions: 26

Compression:

Stored size: 1.81 KB

Contents

module Foobara
  class Outcome
    class UnsuccessfulOutcomeError < StandardError
      attr_accessor :errors

      def initialize(errors)
        self.errors = errors

        message = errors.map(&:message).join(", ")

        super(message)
      end
    end

    class << self
      def success(result)
        new.tap do |outcome|
          outcome.result = result
        end
      end

      def errors(*errors)
        errors = errors.flatten

        if errors.empty?
          # :nocov:
          raise "No errors given"
          # :nocov:
        end

        new.tap do |outcome|
          outcome.add_errors(errors)
        end
      end

      def error(error)
        errors(Util.array(error))
      end

      def raise!(errors)
        self.errors(errors).raise! unless errors.empty?
      end
    end

    attr_accessor :result
    attr_reader :error_collection

    def initialize(result: nil, errors: nil, error_collection: ErrorCollection.new)
      @error_collection = error_collection

      self.result = result

      if errors
        add_errors(errors)
      end
    end

    foobara_delegate :has_errors?,
                     :errors,
                     :each_error,
                     :has_error?,
                     :add_error,
                     :add_errors,
                     to: :error_collection

    def success?
      !has_errors?
    end

    def fatal?
      errors.any?(&:fatal?)
    end

    def raise!
      return if success?

      if errors.size == 1
        raise errors.first
      else
        raise UnsuccessfulOutcomeError, errors
      end
    end

    def result!
      raise!
      result
    end

    def errors_hash
      error_collection.to_h
    end

    def errors_sentence
      error_collection.to_sentence
    end

    def error_keys
      error_collection.keys
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
foobara-0.0.26 projects/common/src/outcome.rb
foobara-0.0.25 projects/common/src/outcome.rb
foobara-0.0.24 projects/common/src/outcome.rb
foobara-0.0.23 projects/common/src/outcome.rb
foobara-0.0.22 projects/common/src/outcome.rb
foobara-0.0.21 projects/common/src/outcome.rb
foobara-0.0.20 projects/common/src/outcome.rb
foobara-0.0.19 projects/common/src/outcome.rb
foobara-0.0.18 projects/common/src/outcome.rb
foobara-0.0.17 projects/common/src/outcome.rb
foobara-0.0.16 projects/common/src/outcome.rb
foobara-0.0.15 projects/common/src/outcome.rb
foobara-0.0.14 projects/common/src/outcome.rb
foobara-0.0.13 projects/common/src/outcome.rb
foobara-0.0.12 projects/common/src/outcome.rb
foobara-0.0.11 projects/common/src/outcome.rb
foobara-0.0.10 projects/common/src/outcome.rb
foobara-0.0.9 projects/common/src/outcome.rb
foobara-0.0.8 projects/common/src/outcome.rb
foobara-0.0.7 projects/common/src/outcome.rb