-
Notifications
You must be signed in to change notification settings - Fork 9
/
option.rb
125 lines (112 loc) · 2.56 KB
/
option.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# frozen_string_literal: true
module Dry
module Types
class Option
include Type
include ::Dry::Equalizer(:type, :options, inspect: false, immutable: true)
include Decorator
include Builder
include Printable
# @param [Fear::Option, Object] input
#
# @return [Fear::Option]
#
# @api private
def call_unsafe(input = Undefined)
case input
when ::Fear::Option
input
when Undefined
Fear.none
else
Fear.option(type.call_unsafe(input))
end
end
# @param [Fear::Option, Object] input
#
# @return [Fear::Option]
#
# @api private
def call_safe(input = Undefined)
case input
when ::Fear::Option
input
when Undefined
Fear.none
else
Fear.option(type.call_safe(input) { |output = input| return yield(output) })
end
end
# @param [Object] input
#
# @return [Result::Success]
#
# @api public
def try(input = Undefined)
result = type.try(input)
if result.success?
Result::Success.new(Fear.option(result.input))
else
result
end
end
# @return [true]
#
# @api public
def default?
true
end
# @param [Object] value
#
# @see Dry::Types::Builder#default
#
# @raise [ArgumentError] if nil provided as default value
#
# @api public
def default(value)
if value.nil?
raise ArgumentError, "nil cannot be used as a default of a maybe type"
else
super
end
end
end
module Builder
# Turn a type into a maybe type
#
# @return [Option]
#
# @api public
def option
Option.new(Types["nil"] | self)
end
end
# @api private
class Schema
class Key
# @api private
def option
__new__(type.option)
end
end
end
# @api private
class Printer
MAPPING[Option] = :visit_option
# @api private
def visit_option(maybe)
visit(maybe.type) do |type|
yield "Fear::Option<#{type}>"
end
end
end
# Register non-coercible maybe types
NON_NIL.each_key do |name|
register("option.strict.#{name}", self[name.to_s].option)
end
# Register coercible maybe types
COERCIBLE.each_key do |name|
register("option.coercible.#{name}", self["coercible.#{name}"].option)
end
end
end