Class: ActiveRecord::Type::TypeMap

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/type/type_map.rb

Overview

:nodoc:

Direct Known Subclasses

HashLookupTypeMap

Instance Method Summary collapse

Constructor Details

#initializeTypeMap

Returns a new instance of TypeMap.


8
9
10
11
12
13
# File 'lib/active_record/type/type_map.rb', line 8

def initialize
  @mapping = {}
  @cache = Concurrent::Map.new do |h, key|
    h.fetch_or_store(key, Concurrent::Map.new)
  end
end

Instance Method Details

#alias_type(key, target_key) ⇒ Object


36
37
38
39
40
41
# File 'lib/active_record/type/type_map.rb', line 36

def alias_type(key, target_key)
  register_type(key) do |sql_type, *args|
     = sql_type[/\(.*\)/, 0]
    lookup("#{target_key}#{}", *args)
  end
end

#clearObject


43
44
45
# File 'lib/active_record/type/type_map.rb', line 43

def clear
  @mapping.clear
end

#fetch(lookup_key, *args, &block) ⇒ Object


19
20
21
22
23
# File 'lib/active_record/type/type_map.rb', line 19

def fetch(lookup_key, *args, &block)
  @cache[lookup_key].fetch_or_store(args) do
    perform_fetch(lookup_key, *args, &block)
  end
end

#lookup(lookup_key, *args) ⇒ Object


15
16
17
# File 'lib/active_record/type/type_map.rb', line 15

def lookup(lookup_key, *args)
  fetch(lookup_key, *args) { Type.default_value }
end

#register_type(key, value = nil, &block) ⇒ Object

Raises:

  • (::ArgumentError)

25
26
27
28
29
30
31
32
33
34
# File 'lib/active_record/type/type_map.rb', line 25

def register_type(key, value = nil, &block)
  raise ::ArgumentError unless value || block
  @cache.clear

  if block
    @mapping[key] = block
  else
    @mapping[key] = proc { value }
  end
end