123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Type::HashLookupTypeMap

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: activerecord/lib/active_record/type/hash_lookup_type_map.rb

Class Method Summary

Instance Method Summary

Constructor Details

.new(parent = nil) ⇒ HashLookupTypeMap

[ GitHub ]

  
# File 'activerecord/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)

[ GitHub ]

  
# File 'activerecord/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

#clear

[ GitHub ]

  
# File 'activerecord/lib/active_record/type/hash_lookup_type_map.rb', line 34

def clear
  @mapping.clear
  @cache.clear
end

#fetch(lookup_key, *args, &block)

[ GitHub ]

  
# File 'activerecord/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

[ GitHub ]

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

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

#keys

[ GitHub ]

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

def keys
  @mapping.keys
end

#lookup(lookup_key, *args)

[ GitHub ]

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

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

#perform_fetch(type, *args, &block) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/type/hash_lookup_type_map.rb', line 52

def perform_fetch(type, *args, &block)
  @mapping.fetch(type, block).call(type, *args)
end

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

Raises:

  • (::ArgumentError)
[ GitHub ]

  
# File 'activerecord/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