Class: ActiveRecord::Type::TypeMap

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ TypeMap

Returns a new instance of TypeMap.



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

def initialize(parent = nil)
  @mapping = {}
  @parent = parent
  @cache = Concurrent::Map.new
end

Instance Method Details

#alias_type(key, target_key) ⇒ Object



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

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

#fetch(lookup_key, &block) ⇒ Object



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

def fetch(lookup_key, &block)
  @cache.fetch_or_store(lookup_key) do
    perform_fetch(lookup_key, &block)
  end
end

#lookup(lookup_key) ⇒ Object



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

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

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

Raises:

  • (::ArgumentError)


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

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