Class: ActiveRecord::Type::TypeMap

Inherits:
Object
  • Object
show all
Defined in:
activerecord/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.


6
7
8
9
10
11
# File 'activerecord/lib/active_record/type/type_map.rb', line 6

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


34
35
36
37
38
39
# File 'activerecord/lib/active_record/type/type_map.rb', line 34

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

#clearObject


41
42
43
# File 'activerecord/lib/active_record/type/type_map.rb', line 41

def clear
  @mapping.clear
end

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


17
18
19
20
21
# File 'activerecord/lib/active_record/type/type_map.rb', line 17

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


13
14
15
# File 'activerecord/lib/active_record/type/type_map.rb', line 13

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

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

Raises:

  • (::ArgumentError)

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

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