Class: Dry::Types::Constructor

Inherits:
Nominal
  • Object
show all
Defined in:
lib/dry/types/constructor.rb,
lib/dry/types/constructor/wrapper.rb,
lib/dry/types/constructor/function.rb

Direct Known Subclasses

Array::Constructor, Hash::Constructor

Defined Under Namespace

Modules: Wrapper Classes: Function

Constant Summary

Constants inherited from Nominal

Nominal::ALWAYS

Instance Attribute Summary collapse

Attributes inherited from Nominal

#primitive

Attributes included from Options

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Nominal

#coerce, #constrained?, #default?, #failure, #name, #optional?, #primitive?, #success, #try_coerce

Methods included from Printable

#to_s

Methods included from Builder

#&, #>, #constrained, #constructor_type, #default, #enum, #fallback, #maybe, #optional, #|

Methods included from Meta

#meta, #pristine, #with

Methods included from Options

#with

Methods included from Type

#call, #valid?

Constructor Details

#initialize(type, fn: nil, **options) ⇒ Constructor

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.

Instantiate a new constructor type instance

Parameters:



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

def initialize(type, fn: nil, **options)
  @type = type
  @fn = fn

  super(type, **options, fn: fn)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *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:

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


184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/dry/types/constructor.rb', line 184

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

    if response.is_a?(Type) && response.instance_of?(type.class)
      response.constructor_type[response, **options]
    else
      response
    end
  else
    super
  end
end

Instance Attribute Details

#fn#call (readonly)

Returns:



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

def fn
  @fn
end

#typeType (readonly)

Returns:



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

def type
  @type
end

Class Method Details

.[](type, fn:, **options) ⇒ Object

Parameters:



35
36
37
38
39
40
41
42
43
# File 'lib/dry/types/constructor.rb', line 35

def self.[](type, fn:, **options)
  function = Function[fn]

  if function.wrapper?
    wrapper_type.new(type, fn: function, **options)
  else
    new(type, fn: function, **options)
  end
end

.new(input, fn: Undefined, **options, &block) ⇒ Object

Parameters:



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

def self.new(input, fn: Undefined, **options, &block)
  type = input.is_a?(Builder) ? input : Nominal.new(input)
  super(type, **options, fn: Function[Undefined.default(fn, block)])
end

.wrapper_typeObject

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.



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

def self.wrapper_type
  @wrapper_type ||=
    if self < Wrapper
      self
    else
      const_set(:Wrapping, ::Class.new(self).include(Wrapper))
    end
end

Instance Method Details

#call_safe(input) ⇒ Object

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.

Returns:

  • (Object)


72
73
74
75
# File 'lib/dry/types/constructor.rb', line 72

def call_safe(input)
  coerced = fn.(input) { |output = input| return yield(output) }
  type.call_safe(coerced) { |output = coerced| yield(output) }
end

#call_unsafe(input) ⇒ Object

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.

Returns:

  • (Object)


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

def call_unsafe(input)
  type.call_unsafe(fn.(input))
end

#constrained_typeClass

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.

Returns:

  • (Class)


124
125
126
# File 'lib/dry/types/constructor.rb', line 124

def constrained_type
  Constrained::Coercible
end

#constructor(new_fn = nil, **options, &block) ⇒ Constructor Also known as: append, >>

Build a new constructor by appending a block to the coercion function

Parameters:

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

Returns:



109
110
111
112
113
114
115
116
117
# File 'lib/dry/types/constructor.rb', line 109

def constructor(new_fn = nil, **options, &block)
  next_fn = Function[new_fn || block]

  if next_fn.wrapper?
    self.class.wrapper_type.new(with(**options), fn: next_fn)
  else
    with(**options, fn: fn >> next_fn)
  end
end

#laxLax

Build a lax type

Returns:



153
154
155
# File 'lib/dry/types/constructor.rb', line 153

def lax
  Lax.new(constructor_type[type.lax, **options])
end

#prepend(new_fn = nil, **options, &block) ⇒ Constructor Also known as: <<

Build a new constructor by prepending a block to the coercion function

Parameters:

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

Returns:



144
145
146
# File 'lib/dry/types/constructor.rb', line 144

def prepend(new_fn = nil, **options, &block)
  with(**options, fn: fn << (new_fn || block))
end

#to_ast(meta: true) ⇒ Object

See Also:



131
132
133
# File 'lib/dry/types/constructor.rb', line 131

def to_ast(meta: true)
  [:constructor, [type.to_ast(meta: meta), fn.to_ast]]
end

#to_procProc

Wrap the type with a proc

Returns:

  • (Proc)


162
163
164
# File 'lib/dry/types/constructor.rb', line 162

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

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

Parameters:

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

Returns:

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

    if block given and try fails



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

def try(input, &block)
  value = fn.(input)
rescue CoercionError => e
  failure = failure(input, e)
  block_given? ? yield(failure) : failure
else
  type.try(value, &block)
end