Module: Dry::Types::Decorator

Includes:
Options
Included in:
Constrained, Default, Enum, Lax, Maybe, Schema::Key
Defined in:
lib/dry/types/decorator.rb

Overview

Common API for types

Instance Attribute Summary collapse

Attributes included from Options

#options

Instance Method Summary collapse

Methods included from Options

#with

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delegates missing methods to #type

Parameters:

  • meth (Symbol)
  • args (Array)
  • block (#call, nil)


82
83
84
85
86
87
88
89
90
91
92
93
94
# 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

Instance Attribute Details

#typeType (readonly)

Returns:



12
13
14
# File 'lib/dry/types/decorator.rb', line 12

def type
  @type
end

Instance Method Details

#constrained?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/dry/types/decorator.rb', line 41

def constrained?
  type.constrained?
end

#default?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/dry/types/decorator.rb', line 34

def default?
  type.default?
end

#initialize(type) ⇒ Object

Parameters:



15
16
17
18
# File 'lib/dry/types/decorator.rb', line 15

def initialize(type, *, **)
  super
  @type = type
end

#respond_to_missing?(meth, include_private = false) ⇒ Boolean

Parameters:

  • meth (Symbol)
  • include_private (Boolean) (defaults to: false)

Returns:

  • (Boolean)


51
52
53
# File 'lib/dry/types/decorator.rb', line 51

def respond_to_missing?(meth, include_private = false)
  super || type.respond_to?(meth)
end

#to_procProc

Wrap the type with a proc

Returns:

  • (Proc)


60
61
62
# File 'lib/dry/types/decorator.rb', line 60

def to_proc
  proc { |value| self.(value) }
end

#try(input, &block) ⇒ Result, ...

Parameters:

  • input (Object)
  • block (#call, nil)

Returns:

  • (Result, Logic::Result)
  • (Object)

    if block given and try fails



27
28
29
# File 'lib/dry/types/decorator.rb', line 27

def try(input, &block)
  type.try(input, &block)
end