Class: ActiveRecord::Type::TypeMap
- Defined in:
- activerecord/lib/active_record/type/type_map.rb
Overview
:nodoc:
Direct Known Subclasses
Instance Method Summary collapse
- #alias_type(key, target_key) ⇒ Object
- #clear ⇒ Object
- #fetch(lookup_key, *args, &block) ⇒ Object
-
#initialize ⇒ TypeMap
constructor
A new instance of TypeMap.
- #lookup(lookup_key, *args) ⇒ Object
- #register_type(key, value = nil, &block) ⇒ Object
Constructor Details
#initialize ⇒ TypeMap
Returns a new instance of TypeMap.
8 9 10 11 12 13 |
# File 'activerecord/lib/active_record/type/type_map.rb', line 8 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
36 37 38 39 40 41 |
# File 'activerecord/lib/active_record/type/type_map.rb', line 36 def alias_type(key, target_key) register_type(key) do |sql_type, *args| = sql_type[/\(.*\)/, 0] lookup("#{target_key}#{}", *args) end end |
#clear ⇒ Object
43 44 45 |
# File 'activerecord/lib/active_record/type/type_map.rb', line 43 def clear @mapping.clear end |
#fetch(lookup_key, *args, &block) ⇒ Object
19 20 21 22 23 |
# File 'activerecord/lib/active_record/type/type_map.rb', line 19 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
15 16 17 |
# File 'activerecord/lib/active_record/type/type_map.rb', line 15 def lookup(lookup_key, *args) fetch(lookup_key, *args) { Type.default_value } end |
#register_type(key, value = nil, &block) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'activerecord/lib/active_record/type/type_map.rb', line 25 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 |