Sha256: 1bc172c012ffe68838cbd0f41330ae2974af1f56cfed64bfaa73437afdd2a193

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require 'dry/equalizer'

module Dry
  module Types
    # Result class used by {Type#try}
    #
    # @api public
    class Result
      include ::Dry::Equalizer(:input, immutable: true)

      # @return [Object]
      attr_reader :input

      # @param [Object] input
      #
      # @api private
      def initialize(input)
        @input = input
      end

      # Success result
      #
      # @api public
      class Success < Result
        # @return [true]
        #
        # @api public
        def success?
          true
        end

        # @return [false]
        #
        # @api public
        def failure?
          false
        end
      end

      # Failure result
      #
      # @api public
      class Failure < Result
        include ::Dry::Equalizer(:input, :error, immutable: true)

        # @return [#to_s]
        attr_reader :error

        # @param [Object] input
        #
        # @param [#to_s] error
        #
        # @api private
        def initialize(input, error)
          super(input)
          @error = error
        end

        # @return [String]
        #
        # @api private
        def to_s
          error.to_s
        end

        # @return [false]
        #
        # @api public
        def success?
          false
        end

        # @return [true]
        #
        # @api public
        def failure?
          true
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/dry-types-1.4.0/lib/dry/types/result.rb
dry-types-1.4.0 lib/dry/types/result.rb
dry-types-1.3.1 lib/dry/types/result.rb
dry-types-1.3.0 lib/dry/types/result.rb