Class: ActiveRecord::Type::HashLookupTypeMap

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ HashLookupTypeMap

Returns a new instance of HashLookupTypeMap.


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

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

Instance Method Details

#alias_type(type, alias_type) ⇒ Object


39
40
41
# File 'lib/active_record/type/hash_lookup_type_map.rb', line 39

def alias_type(type, alias_type)
  register_type(type) { |_, *args| lookup(alias_type, *args) }
end

#clearObject


34
35
36
37
# File 'lib/active_record/type/hash_lookup_type_map.rb', line 34

def clear
  @mapping.clear
  @cache.clear
end

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


17
18
19
20
21
# File 'lib/active_record/type/hash_lookup_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

#key?(key) ⇒ Boolean

Returns:


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

def key?(key)
  @mapping.key?(key)
end

#keysObject


47
48
49
# File 'lib/active_record/type/hash_lookup_type_map.rb', line 47

def keys
  @mapping.keys
end

#lookup(lookup_key, *args) ⇒ Object


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

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

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

Raises:

  • (::ArgumentError)

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

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