Sha256: b40049c280cde2f76808d47892f2e2cbe516147f6baf6f8e0652bce368b9f897

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 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, inspect: false)

      # @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, inspect: false)

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

6 entries across 6 versions & 1 rubygems

Version Path
dry-types-1.2.1 lib/dry/types/result.rb
dry-types-1.2.0 lib/dry/types/result.rb
dry-types-1.1.1 lib/dry/types/result.rb
dry-types-1.1.0 lib/dry/types/result.rb
dry-types-1.0.1 lib/dry/types/result.rb
dry-types-1.0.0 lib/dry/types/result.rb