Sha256: d0b029f14b4f2ba66822609399ce3ea9e451fb2e11f5932f79821155d9994b2a

Contents?: true

Size: 1.41 KB

Versions: 35

Compression:

Stored size: 1.41 KB

Contents

require 'concurrent/map'

module ActiveRecord
  module Type
    class TypeMap # :nodoc:
      def initialize
        @mapping = {}
        @cache = Concurrent::Map.new do |h, key|
          h.fetch_or_store(key, Concurrent::Map.new)
        end
      end

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

      def fetch(lookup_key, *args, &block)
        @cache[lookup_key].fetch_or_store(args) do
          perform_fetch(lookup_key, *args, &block)
        end
      end

      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

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

      def clear
        @mapping.clear
      end

      private

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

        if matching_pair
          matching_pair.last.call(lookup_key, *args)
        else
          yield lookup_key, *args
        end
      end

      def default_value
        @default_value ||= ActiveModel::Type::Value.new
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 4 rubygems

Version Path
activerecord-5.0.7.2 lib/active_record/type/type_map.rb
activerecord-5.0.7.1 lib/active_record/type/type_map.rb
activerecord-5.0.7 lib/active_record/type/type_map.rb
activerecord-5.0.6 lib/active_record/type/type_map.rb
activerecord-5.0.6.rc1 lib/active_record/type/type_map.rb
activerecord-5.0.5 lib/active_record/type/type_map.rb
activerecord-5.0.5.rc2 lib/active_record/type/type_map.rb
activerecord-5.0.5.rc1 lib/active_record/type/type_map.rb
activerecord-5.0.4 lib/active_record/type/type_map.rb
activerecord-5.0.4.rc1 lib/active_record/type/type_map.rb
activerecord-5.0.3 lib/active_record/type/type_map.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activerecord-5.0.2/lib/active_record/type/type_map.rb
activerecord-5.0.2 lib/active_record/type/type_map.rb
activerecord-5.0.2.rc1 lib/active_record/type/type_map.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/type/type_map.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/type/type_map.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/type/type_map.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/type/type_map.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/type/type_map.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/type/type_map.rb