Sha256: 9b5c9bb15a6a2151defea63a58eb4924f98b6be4e5ce80a3271532ab9d594588

Contents?: true

Size: 1.01 KB

Versions: 12

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

12 entries across 12 versions & 1 rubygems

Version Path
rom-sql-3.6.4 lib/rom/sql/type_serializer.rb
rom-sql-3.6.3 lib/rom/sql/type_serializer.rb
rom-sql-3.6.2 lib/rom/sql/type_serializer.rb
rom-sql-3.6.1 lib/rom/sql/type_serializer.rb
rom-sql-3.6.0 lib/rom/sql/type_serializer.rb
rom-sql-3.5.0 lib/rom/sql/type_serializer.rb
rom-sql-3.4.0 lib/rom/sql/type_serializer.rb
rom-sql-3.3.3 lib/rom/sql/type_serializer.rb
rom-sql-3.3.2 lib/rom/sql/type_serializer.rb
rom-sql-3.3.1 lib/rom/sql/type_serializer.rb
rom-sql-3.3.0 lib/rom/sql/type_serializer.rb
rom-sql-3.2.0 lib/rom/sql/type_serializer.rb