Sha256: 65a72b062f35b5dda5b5fd822fc93397c1bcf66cbd96b0276f438eb123970ca8

Contents?: true

Size: 1.76 KB

Versions: 5

Compression:

Stored size: 1.76 KB

Contents

require 'dry/types/decorator'

module Dry
  module Types
    class Default
      include Type
      include Dry::Equalizer(:type, :options, :value)
      include Decorator
      include Builder

      class Callable < Default
        include Dry::Equalizer(:type, :options)

        # Evaluates given callable
        # @return [Object]
        def evaluate
          value.call(type)
        end
      end

      # @return [Object]
      attr_reader :value

      alias_method :evaluate, :value

      # @param [Object, #call] value
      # @return [Class] {Default} or {Default::Callable}
      def self.[](value)
        if value.respond_to?(:call)
          Callable
        else
          self
        end
      end

      # @param [Type] type
      # @param [Object] value
      def initialize(type, value, **options)
        super
        @value = value
      end

      # @param [Array] args see {Dry::Types::Builder#constrained}
      # @return [Default]
      def constrained(*args)
        type.constrained(*args).default(value)
      end

      # @return [true]
      def default?
        true
      end

      # @param [Object] input
      # @return [Result::Success]
      def try(input)
        success(call(input))
      end

      def valid?(value = Undefined)
        value.equal?(Undefined) || super
      end

      # @param [Object] input
      # @return [Object] value passed through {#type} or {#default} value
      def call(input = Undefined)
        if input.equal?(Undefined)
          evaluate
        else
          output = type[input]
          output.nil? ? evaluate : output
        end
      end
      alias_method :[], :call

      private

      # Replace underlying type
      def __new__(type)
        self.class.new(type, value, options)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dry-types-0.14.1 lib/dry/types/default.rb
dry-types-0.14.0 lib/dry/types/default.rb
dry-types-0.13.4 lib/dry/types/default.rb
dry-types-0.13.3 lib/dry/types/default.rb
dry-types-0.13.2 lib/dry/types/default.rb