module Dry::Types::Decorator

Common API for types

@api public

Attributes

type[R]

@return [Type]

Public Class Methods

new(type, *, **) click to toggle source

@param [Type] type

Calls superclass method Dry::Types::Options::new
# File lib/dry/types/decorator.rb, line 15
def initialize(type, *, **)
  super
  @type = type
end

Public Instance Methods

constrained?() click to toggle source

@return [Boolean]

@api public

# File lib/dry/types/decorator.rb, line 41
def constrained?
  type.constrained?
end
default?() click to toggle source

@return [Boolean]

@api public

# File lib/dry/types/decorator.rb, line 34
def default?
  type.default?
end
respond_to_missing?(meth, include_private = false) click to toggle source

@param [Symbol] meth @param [Boolean] include_private

@return [Boolean]

@api public

Calls superclass method
# File lib/dry/types/decorator.rb, line 51
def respond_to_missing?(meth, include_private = false)
  super || type.respond_to?(meth)
end
to_proc() click to toggle source

Wrap the type with a proc

@return [Proc]

@api public

# File lib/dry/types/decorator.rb, line 60
def to_proc
  proc { |value| self.(value) }
end
try(input, &block) click to toggle source

@param [Object] input @param [#call, nil] block

@return [Result,Logic::Result] @return [Object] if block given and try fails

@api public

# File lib/dry/types/decorator.rb, line 27
def try(input, &block)
  type.try(input, &block)
end

Private Instance Methods

__new__(type) click to toggle source

Replace underlying type

@api private

# File lib/dry/types/decorator.rb, line 100
def __new__(type)
  self.class.new(type, *@__args__.drop(1), **@options)
end
decorate?(response) click to toggle source

@param [Object] response

@return [Boolean]

@api private

# File lib/dry/types/decorator.rb, line 71
def decorate?(response)
  response.is_a?(type.class)
end
method_missing(meth, *args, &block) click to toggle source

Delegates missing methods to {#type}

@param [Symbol] meth @param [Array] args @param [#call, nil] block

@api private

Calls superclass method
# File lib/dry/types/decorator.rb, line 82
def method_missing(meth, *args, &block)
  if type.respond_to?(meth)
    response = type.public_send(meth, *args, &block)

    if decorate?(response)
      __new__(response)
    else
      response
    end
  else
    super
  end
end