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
|
# File 'lib/toy_adt/public_api.rb', line 33
def sum(**types, &fn)
container_module = Module.new do
extend T::Helpers
sealed!
const_set(:FIELDS, types.keys)
module_eval(&fn) if block_given?
def self.match(&fn) = Matcher.new(from: self, &fn)
def self.match_call(input, &fn) = match(&fn).call(input)
end
types.each do |type, config|
klass = Class.new(T::Struct) do
include container_module
include DeconstructableSorbetStruct
const_set(:CONTAINER, container_module)
config.each do |field, type|
const field, type
end
def match(&fn) = self.class::CONTAINER.match(&fn).call(self)
end
container_module.const_set(type.capitalize, klass)
container_module.define_method(type) do
klass
end
end
container_module
end
|