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

Raises:

  • (ArgumentError)


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

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

  raise ArgumentError, 'Missing constructor block' if fn.nil?

  super(type, **options, fn: fn)
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)


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

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)


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

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

#constrained_typeClass

Returns:

  • (Class)


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

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:



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

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

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

#nameString

Returns:

  • (String)


40
41
42
# File 'lib/dry/types/constructor.rb', line 40

def name
  type.name
end

#primitiveClass

Returns:

  • (Class)


35
36
37
# File 'lib/dry/types/constructor.rb', line 35

def primitive
  type.primitive
end

#register_fn(fn) ⇒ Object (private)



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

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)


101
102
103
# File 'lib/dry/types/constructor.rb', line 101

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

#to_ast(meta: true) ⇒ Object

See Also:



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

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



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

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

#valid?(value) ⇒ Boolean

Parameters:

  • value (Object)

Returns:

  • (Boolean)


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

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