Module: Dry::Types::Decorator

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

Instance Attribute Summary collapse

Attributes included from Options

#options

Instance Method Summary collapse

Methods included from Options

#meta, #pristine, #with

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Delegates missing methods to #type

Parameters:

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


65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dry/types/decorator.rb', line 65

def method_missing(meth, *args, &block)
  if type.respond_to?(meth)
    response = type.__send__(meth, *args, &block)

    if decorate?(response)
      self.class.new(response, options)
    else
      response
    end
  else
    super
  end
end

Instance Attribute Details

#typeType (readonly)

Returns:



9
10
11
# File 'lib/dry/types/decorator.rb', line 9

def type
  @type
end

Instance Method Details

#constrained?Boolean

Returns:

  • (Boolean)


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

def constrained?
  type.constrained?
end

#constructorConstructor

Returns:



18
19
20
# File 'lib/dry/types/decorator.rb', line 18

def constructor
  type.constructor
end

#decorate?(response) ⇒ Boolean (private)

Parameters:

  • response (Object)

Returns:

  • (Boolean)


57
58
59
# File 'lib/dry/types/decorator.rb', line 57

def decorate?(response)
  response.kind_of?(type.class)
end

#default?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/dry/types/decorator.rb', line 37

def default?
  type.default?
end

#initialize(type) ⇒ Object

Parameters:



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

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)


49
50
51
# File 'lib/dry/types/decorator.rb', line 49

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

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

Parameters:

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

Returns:

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

    if block given and try fails



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

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

#valid?(value) ⇒ Boolean

Parameters:

  • value (Object)

Returns:

  • (Boolean)


32
33
34
# File 'lib/dry/types/decorator.rb', line 32

def valid?(value)
  type.valid?(value)
end