Sha256: c948f8c54e59080ada85cc526999c817cdf2d1b40cb1330fca6e8e58ef9110d0

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Motor
  module ActiveRecordUtils
    module Types
      MUTEX = Mutex.new
      DEFAULT_TYPE = 'string'

      UNIFIED_TYPES = {
        'smallint' => 'integer',
        'int' => 'integer',
        'int4' => 'integer',
        'int8' => 'integer',
        'int16' => 'integer',
        'bigint' => 'integer',
        'numeric' => 'float',
        'decimal' => 'float',
        'float4' => 'float',
        'float8' => 'float',
        'float16' => 'float',
        'text' => 'string',
        'citext' => 'string',
        'jsonb' => 'json',
        'timestamp' => 'datetime',
        'money' => 'currency'
      }.freeze

      module_function

      def all
        @all || MUTEX.synchronize do
          @all ||= build_types_hash
        end
      end

      def find_class_for_name(name)
        all.invert[name.to_s]
      end

      def find_name_for_class(klass)
        name = all[klass.to_s]

        return UNIFIED_TYPES.fetch(name, name) if name

        DEFAULT_TYPE
      end

      def build_types_hash
        type_map = ActiveRecord::Base.connection.send(:type_map)

        type_map.instance_variable_get('@mapping').map do |name, type|
          next unless name.is_a?(String)

          [type.call.class.to_s, name]
        end.compact.to_h
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
motor-admin-0.1.27 lib/motor/active_record_utils/types.rb
motor-admin-0.1.25 lib/motor/active_record_utils/types.rb
motor-admin-0.1.23 lib/motor/active_record_utils/types.rb