Sha256: 131b5f5fe5f3763a50f57fe10ab96588dbc5c21666268cd86afb388b7db06c03

Contents?: true

Size: 1002 Bytes

Versions: 9

Compression:

Stored size: 1002 Bytes

Contents

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::Int => '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

9 entries across 9 versions & 1 rubygems

Version Path
rom-sql-2.5.0 lib/rom/sql/type_serializer.rb
rom-sql-2.4.0 lib/rom/sql/type_serializer.rb
rom-sql-2.3.0 lib/rom/sql/type_serializer.rb
rom-sql-2.2.1 lib/rom/sql/type_serializer.rb
rom-sql-2.2.0 lib/rom/sql/type_serializer.rb
rom-sql-2.1.0 lib/rom/sql/type_serializer.rb
rom-sql-2.0.0 lib/rom/sql/type_serializer.rb
rom-sql-2.0.0.rc1 lib/rom/sql/type_serializer.rb
rom-sql-2.0.0.beta3 lib/rom/sql/type_serializer.rb