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
|
module ActiveModel
# :stopdoc:
module Type
class Registry
def initialize
@registrations = []
end
def register(type_name, klass = nil, **options, &block)
block ||= proc { |_, *args| klass.new(*args) }
registrations << registration_klass.new(type_name, block, **options)
end
def lookup(symbol, *args)
registration = find_registration(symbol, *args)
if registration
registration.call(self, symbol, *args)
else
raise ArgumentError, "Unknown type #{symbol.inspect}"
end
end
# TODO Change this to private once we've dropped Ruby 2.2 support.
# Workaround for Ruby 2.2 "private attribute?" warning.
protected
attr_reader :registrations
private
def registration_klass
Registration
end
def find_registration(symbol, *args)
registrations.find { |r| r.matches?(symbol, *args) }
end
end
class Registration
# Options must be taken because of https://bugs.ruby-lang.org/issues/10856
def initialize(name, block, **)
@name = name
@block = block
end
def call(_registry, *args, **kwargs)
if kwargs.any? # https://bugs.ruby-lang.org/issues/10856
block.call(*args, **kwargs)
else
block.call(*args)
end
end
def matches?(type_name, *args, **kwargs)
type_name == name
end
# TODO Change this to private once we've dropped Ruby 2.2 support.
# Workaround for Ruby 2.2 "private attribute?" warning.
protected
attr_reader :name, :block
end
end
# :startdoc:
end
|