-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
type_map.rb
58 lines (49 loc) · 1.31 KB
/
type_map.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
# frozen_string_literal: true
require "concurrent/map"
module ActiveRecord
module Type
class TypeMap # :nodoc:
def initialize(parent = nil)
@mapping = {}
@parent = parent
@cache = Concurrent::Map.new
end
def lookup(lookup_key)
fetch(lookup_key) { Type.default_value }
end
def fetch(lookup_key, &block)
@cache.fetch_or_store(lookup_key) do
perform_fetch(lookup_key, &block)
end
end
def register_type(key, value = nil, &block)
raise ::ArgumentError unless value || block
if block
@mapping[key] = block
else
@mapping[key] = proc { value }
end
@cache.clear
end
def alias_type(key, target_key)
register_type(key) do |sql_type|
metadata = sql_type[/\(.*\)/, 0]
lookup("#{target_key}#{metadata}")
end
end
protected
def perform_fetch(lookup_key, &block)
matching_pair = @mapping.reverse_each.detect do |key, _|
key === lookup_key
end
if matching_pair
matching_pair.last.call(lookup_key)
elsif @parent
@parent.perform_fetch(lookup_key, &block)
else
yield lookup_key
end
end
end
end
end