Sha256: b20d9604bad386d7d7fb177259dca0a6672172ac6830d9c32cb815a5ab0b2bd2

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

require 'dry/core/class_attributes'

module ROM
  module SQL
    # @api private
    class TypeSerializer
      extend Dry::Core::ClassAttributes

      defines :registry

      def self.register(db_type, builder)
        registry[db_type] = builder
      end

      def self.[](db_type)
        registry[db_type]
      end

      registry Hash.new(new.freeze)

      defines :mapping

      mapping(
        Types::Integer => 'integer',
        Types::String => 'varchar',
        Types::Time => 'timestamp',
        Types::Date => 'date',
        Types::Bool => 'boolean',
        Types::Decimal => 'numeric',
        Types::Float => 'float'
      )

      def call(type)
        return type.meta[:db_type] if type.meta[:db_type]

        meta = type.meta[:read] ? { read: type.meta[:read] } : EMPTY_HASH

        self.class.mapping.fetch(type.with(meta: meta)) {
          if block_given?
            yield(type)
          end or raise "Cannot serialize #{type}"
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-sql-3.7.0 lib/rom/sql/type_serializer.rb