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, #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.
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
Delegates missing methods to #type
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
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:
[]
46
47
48
|
# File 'lib/dry/types/constructor.rb', line 46
def call(input)
type[fn[input]]
end
|
#constrained_type ⇒ 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
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
|
#name ⇒ String
40
41
42
|
# File 'lib/dry/types/constructor.rb', line 40
def name
type.name
end
|
#primitive ⇒ Class
35
36
37
|
# File 'lib/dry/types/constructor.rb', line 35
def primitive
type.primitive
end
|
#register_fn(fn) ⇒ Object
#respond_to_missing?(meth, include_private = false) ⇒ 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
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, ...
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
74
75
76
|
# File 'lib/dry/types/constructor.rb', line 74
def valid?(value)
super && type.valid?(value)
end
|