Sha256: 3704084c1d3abb3776b9b00bcdb5c5a0dcefe5959813f03da6bed7bd2548a787

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

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(name: name, source: schema.name)) if type
          end.compact

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

          [inferred, missing]
        end

        # @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

3 entries across 3 versions & 1 rubygems

Version Path
rom-sql-2.0.0 lib/rom/sql/schema/attributes_inferrer.rb
rom-sql-2.0.0.rc1 lib/rom/sql/schema/attributes_inferrer.rb
rom-sql-2.0.0.beta3 lib/rom/sql/schema/attributes_inferrer.rb