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, #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)


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

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

#typeDefinition (readonly)

Returns:



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

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

#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)


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

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

#default?Boolean

Returns:

  • (Boolean)


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

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)


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

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

#try(input, &block) ⇒ Result

Parameters:

  • input (Object)
  • block (#call)

Returns:



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

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

#valid?(value) ⇒ Boolean

Parameters:

  • value (Object)

Returns:

  • (Boolean)


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

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