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, #optional?, #primitive?, #result, #success

Methods included from Builder

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

Methods included from Options

#meta, #pristine, #with

Constructor Details

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

Returns a new instance of Constructor.

Parameters:

  • type (Type)
  • options (Hash) (defaults to: {})
  • block (#call, nil)


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:

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


101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/dry/types/constructor.rb', line 101

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

#typeType (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:

  • input (Builder, Object)
  • options (Hash) (defaults to: {})
  • block (#call, nil)


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)


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

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

Returns:



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

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

#register_fn(fn) ⇒ Object (private)



86
87
88
# File 'lib/dry/types/constructor.rb', line 86

def register_fn(fn)
  Dry::Types::FnContainer.register(fn)
end

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

Parameters:

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

Returns:

  • (Boolean)


93
94
95
# File 'lib/dry/types/constructor.rb', line 93

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

#to_ast(meta: true) ⇒ Object

See Also:



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

def to_ast(meta: true)
  [:constructor, [type.to_ast(meta: meta),
                  register_fn(fn),
                  meta ? self.meta : EMPTY_HASH]]
end

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

Parameters:

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

Returns:

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

    if block given and try fails



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

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)


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

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