Sha256: 7bee9373d9ae0e69b58d38410f442c3f4f6c365a4e2efb0540a850c6ee833d86

Contents?: true

Size: 1.2 KB

Versions: 12

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require 'dry/core/class_attributes'

module ROM
  module SQL
    class Schema < ROM::Schema
      # @api private
      class AttributesInferrer
        extend Dry::Core::ClassAttributes
        extend Initializer

        defines :type_builders

        CONSTRAINT_DB_TYPE = 'add_constraint'.freeze

        option :type_builder

        option :attr_class, optional: true

        # @api private
        def call(schema, gateway)
          dataset = schema.name.dataset

          columns = filter_columns(gateway.connection.schema(dataset))

          inferred = columns.map do |name, definition|
            type = type_builder.(**definition)

            attr_class.new(type.meta(source: schema.name), name: name) if type
          end.compact

          missing = columns.map(&:first) - inferred.map { |attr| attr.name }

          [inferred, missing]
        end

        undef :with

        # @api private
        def with(new_options)
          self.class.new(options.merge(new_options))
        end

        # @api private
        def filter_columns(schema)
          schema.reject { |_, definition| definition[:db_type] == CONSTRAINT_DB_TYPE }
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

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