Sha256: 475db1bfcfb297923d45baa9426679a531dc6d2cf95aab23e9c81a0b99dbf42f

Contents?: true

Size: 819 Bytes

Versions: 3

Compression:

Stored size: 819 Bytes

Contents

require 'dry/types/decorator'

module Dry
  module Types
    class Default
      include Decorator
      include Builder

      class Callable < Default
        def evaluate
          value.call
        end
      end

      attr_reader :value

      alias_method :evaluate, :value

      def self.[](value)
        if value.respond_to?(:call)
          Callable
        else
          self
        end
      end

      def initialize(type, options)
        super
        @value = options.fetch(:value)
      end

      def constrained(*args)
        type.constrained(*args).default(value)
      end

      def call(input)
        if input.nil?
          evaluate
        else
          output = type[input]
          output.nil? ? evaluate : output
        end
      end
      alias_method :[], :call
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-types-0.7.2 lib/dry/types/default.rb
dry-types-0.7.1 lib/dry/types/default.rb
dry-types-0.7.0 lib/dry/types/default.rb