Sha256: beeade207101d68ee1c800dd8df4541607dd9521fc6b0bb818c65f50e6dfa841
Contents?: true
Size: 1.8 KB
Versions: 1
Compression:
Stored size: 1.8 KB
Contents
require 'dry/types/decorator' module Dry module Types class Default include Type include Decorator include Builder include Printable include Dry::Equalizer(:type, :options, :value, inspect: false) class Callable < Default include Dry::Equalizer(:type, :options, inspect: false) # 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 Undefined.default(type[input]) { evaluate } 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dry-types-0.15.0 | lib/dry/types/default.rb |