Class: Dry::Types::Constructor

Inherits:
Definition show all
Defined in:
lib/dry/types/constructor.rb

Instance Attribute Summary collapse

Attributes inherited from Definition

#options

Attributes included from Options

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Definition

[], #constrained?, #default?, #failure, #name, #primitive?, #result, #success

Methods included from Builder

#constrained, #default, #enum, #maybe, #optional, #safe, #|

Methods included from Options

#meta, #with

Constructor Details

#initialize(type, options = {}, &block) ⇒ Constructor

Returns a new instance of Constructor.

Parameters:



25
26
27
28
29
# File 'lib/dry/types/constructor.rb', line 25

def initialize(type, options = {}, &block)
  @type = type
  @fn = options.fetch(:fn, block)
  super
end

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:



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dry/types/constructor.rb', line 87

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

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

Instance Attribute Details

#fn#call (readonly)

Returns:



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

def fn
  @fn
end

#typeDefinition (readonly)

Returns:



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

def type
  @type
end

Class Method Details

.new(input, options = {}, &block) ⇒ Object

Parameters:



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

def self.new(input, options = {}, &block)
  type = input.is_a?(Builder) ? input : Definition.new(input)
  super(type, options, &block)
end

Instance Method Details

#call(input) ⇒ Object Also known as: []

Parameters:

  • input (Object)

Returns:

  • (Object)


38
39
40
# File 'lib/dry/types/constructor.rb', line 38

def call(input)
  type[fn[input]]
end

#constrained_typeClass

Returns:

  • (Class)


70
71
72
# File 'lib/dry/types/constructor.rb', line 70

def constrained_type
  Constrained::Coercible
end

#constructor(new_fn = nil, **options, &block) ⇒ Constructor

Parameters:

  • new_fn (#call, nil) (defaults to: nil)
  • options (Hash)
  • block (#call)

Returns:



56
57
58
59
60
61
# File 'lib/dry/types/constructor.rb', line 56

def constructor(new_fn = nil, **options, &block)
  left = new_fn || block
  right = fn

  with(options.merge(fn: -> input { left[right[input]] }))
end

#primitiveClass

Returns:

  • (Class)


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

def primitive
  type.primitive
end

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

Parameters:

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

Returns:

  • (Boolean)


79
80
81
# File 'lib/dry/types/constructor.rb', line 79

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

#try(input, &block) ⇒ Result

Parameters:

  • input (Object)
  • block (#call)

Returns:



46
47
48
49
50
# File 'lib/dry/types/constructor.rb', line 46

def try(input, &block)
  type.try(fn[input], &block)
rescue TypeError => e
  failure(input, e.message)
end

#valid?(value) ⇒ Boolean

Parameters:

  • value (Object)

Returns:

  • (Boolean)


65
66
67
# File 'lib/dry/types/constructor.rb', line 65

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