-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
function.rb
215 lines (187 loc) · 5.97 KB
/
function.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# frozen_string_literal: true
require "concurrent/map"
module Dry
module Types
class Constructor < Nominal
# Function is used internally by Constructor types
#
# @api private
class Function
# Wrapper for unsafe coercion functions
#
# @api private
class Safe < Function
def call(input, &block)
@fn.(input, &block)
rescue ::NoMethodError, ::TypeError, ::ArgumentError => e
CoercionError.handle(e, &block)
end
end
# Coercion via a method call on a known object
#
# @api private
class MethodCall < Function
@cache = ::Concurrent::Map.new
# Choose or build the base class
#
# @return [Function]
def self.call_class(method, public, safe)
@cache.fetch_or_store([method, public, safe]) do
if public
::Class.new(PublicCall) do
include PublicCall.call_interface(method, safe)
define_method(:__to_s__) do
"#<PublicCall for :#{method}>"
end
end
elsif safe
PrivateCall
else
PrivateSafeCall
end
end
end
# Coercion with a publicly accessible method call
#
# @api private
class PublicCall < MethodCall
@interfaces = ::Concurrent::Map.new
# Choose or build the interface
#
# @return [::Module]
def self.call_interface(method, safe)
@interfaces.fetch_or_store([method, safe]) do
::Module.new do
if safe
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def call(input, &block) # def call(input, &block)
@target.#{method}(input, &block) # @target.coerve(input, &block)
end # end
RUBY
else
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def call(input, &block) # def call(input, &block)
@target.#{method}(input) # @target.coerce(input)
rescue ::NoMethodError, ::TypeError, ::ArgumentError => error # rescue ::NoMethodError, ::TypeError, ::ArgumentError => error
CoercionError.handle(error, &block) # CoercionError.handle(error, &block)
end # end
RUBY
end
end
end
end
end
# Coercion via a private method call
#
# @api private
class PrivateCall < MethodCall
def call(input, &block)
@target.send(@name, input, &block)
end
end
# Coercion via an unsafe private method call
#
# @api private
class PrivateSafeCall < PrivateCall
def call(input, &block)
@target.send(@name, input)
rescue ::NoMethodError, ::TypeError, ::ArgumentError => e
CoercionError.handle(e, &block)
end
end
# @api private
#
# @return [MethodCall]
def self.[](fn, safe)
public = fn.receiver.respond_to?(fn.name)
MethodCall.call_class(fn.name, public, safe).new(fn)
end
attr_reader :target, :name
def initialize(fn)
super
@target = fn.receiver
@name = fn.name
end
def to_ast
[:method, target, name]
end
end
class Wrapper < Function
# @return [Object]
def call(input, type, &block)
@fn.(input, type, &block)
rescue ::NoMethodError, ::TypeError, ::ArgumentError => e
CoercionError.handle(e, &block)
end
alias_method :[], :call
def arity
2
end
end
# Choose or build specialized invokation code for a callable
#
# @param [#call] fn
# @return [Function]
def self.[](fn)
raise ::ArgumentError, "Missing constructor block" if fn.nil?
if fn.is_a?(Function)
fn
elsif fn.respond_to?(:arity) && fn.arity.equal?(2)
Wrapper.new(fn)
elsif fn.is_a?(::Method)
MethodCall[fn, yields_block?(fn)]
elsif yields_block?(fn)
new(fn)
else
Safe.new(fn)
end
end
# @return [Boolean]
def self.yields_block?(fn)
*, (last_arg,) =
if fn.respond_to?(:parameters)
fn.parameters
else
fn.method(:call).parameters
end
last_arg.equal?(:block)
end
include ::Dry::Equalizer(:fn, immutable: true)
attr_reader :fn
def initialize(fn)
@fn = fn
end
# @return [Object]
def call(input, &block)
@fn.(input, &block)
end
alias_method :[], :call
# @return [Integer]
def arity
1
end
def wrapper?
arity.equal?(2)
end
# @return [Array]
def to_ast
if fn.is_a?(::Proc)
[:id, FnContainer.register(fn)]
else
[:callable, fn]
end
end
# @return [Function]
def >>(other)
f = Function[other]
Function[-> x, &b { f.(self.(x, &b), &b) }]
end
# @return [Function]
def <<(other)
f = Function[other]
Function[-> x, &b { self.(f.(x, &b), &b) }]
end
end
end
end
end