Class: Dry::Types::Constructor
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.
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
Delegates missing methods to #type
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
9
10
11
|
# File 'lib/dry/types/constructor.rb', line 9
def fn
@fn
end
|
12
13
14
|
# File 'lib/dry/types/constructor.rb', line 12
def type
@type
end
|
Class Method Details
.new(input, options = {}, &block) ⇒ Object
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:
[]
38
39
40
|
# File 'lib/dry/types/constructor.rb', line 38
def call(input)
type[fn[input]]
end
|
#constrained_type ⇒ 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
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
|
#primitive ⇒ 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
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
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
65
66
67
|
# File 'lib/dry/types/constructor.rb', line 65
def valid?(value)
super && type.valid?(value)
end
|