Sha256: ab0a321ea5c7b442556a80c117a7383e777ef343b1e3560c412902adce8d8596

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 KB

Contents

module Datacaster
  class Result
    def initialize(valid, value_or_errors)
      @value_or_errors = value_or_errors
      if !valid && !@value_or_errors.is_a?(Hash) && !@value_or_errors.is_a?(Array)
        @value_or_errors = Array(@value_or_errors)
      end

      @valid = !!valid
    end

    def valid?
      @valid
    end

    def value
      @valid ? @value_or_errors : nil
    end

    def value!
      raise "Tried to unwrap value of error result: #{inspect}" unless valid?
      value
    end

    def raw_errors
      @valid ? nil : @value_or_errors
    end

    def errors
      @errors ||= @valid ? nil : resolve_i18n(raw_errors)
    end

    def inspect
      if @valid
        "#<Datacaster::ValidResult(#{@value_or_errors.inspect})>"
      else
        "#<Datacaster::ErrorResult(#{@value_or_errors.inspect})>"
      end
    end

    def to_dry_result
      if @valid
        Dry::Monads::Result::Success.new(@value_or_errors)
      else
        Dry::Monads::Result::Failure.new(errors)
      end
    end

    private

    def resolve_i18n(o)
      case o
      when Array
        o.map { |x| resolve_i18n(x) }
      when Hash
        o.transform_values { |x| resolve_i18n(x) }
      when I18nValues::Base
        o.resolve
      else
        o
      end
    end
  end

  def self.ValidResult(object)
    if object.is_a?(Result)
      raise "Can't create valid result from error #{object.inspect}" unless object.valid?
      object
    else
      Result.new(true, object)
    end
  end

  def self.ErrorResult(object)
    if object.is_a?(Result)
      raise "Can't create error result from valid #{object.inspect}" if object.valid?
      object
    else
      Result.new(false, object)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
datacaster-3.1.5 lib/datacaster/result.rb
datacaster-3.1.3 lib/datacaster/result.rb
datacaster-3.1.2 lib/datacaster/result.rb
datacaster-3.1.1 lib/datacaster/result.rb
datacaster-3.1.0 lib/datacaster/result.rb