123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Type::TypeMap

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

Class Method Summary

Instance Method Summary

Constructor Details

.new(parent = nil) ⇒ TypeMap

[ GitHub ]

  
# 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)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# 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)

[ GitHub ]

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

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

#perform_fetch(lookup_key, &block) (protected)

[ GitHub ]

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

def perform_fetch(lookup_key, &block)
  matching_pair = @mapping.reverse_each.detect do |key, _|
    key === lookup_key
  end

  if matching_pair
    matching_pair.last.call(lookup_key)
  elsif @parent
    @parent.perform_fetch(lookup_key, &block)
  else
    yield lookup_key
  end
end

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

Raises:

  • (::ArgumentError)
[ GitHub ]

  
# 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