Sha256: e23d8ae3d7391725a2dde8e2217009496113e6a27e75742fb1ad375d1fcf8e7b

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require 'set'

module ROM
  module SQL
    class Schema < ROM::Schema
      # @api public
      class IndexDSL
        extend Initializer

        option :attr_class

        attr_reader :registry

        # @api private
        def initialize(*, **, &)
          super

          @registry = []

          instance_exec(&)
        end

        # @api public
        def index(*attributes, **options)
          registry << [attributes, options]
        end

        # @api private
        def call(schema_name, attrs)
          attributes = attrs.map do |attr|
            attr_class.new(attr[:type], **(attr[:options] || {})).meta(source: schema_name)
          end

          registry.to_set do |attr_names, options|
            build_index(attributes, attr_names, options)
          end
        end

        private

        # @api private
        def build_index(attributes, attr_names, options)
          index_attributes = attr_names.map do |name|
            attributes.find { |a| a.name == name }.unwrap
          end

          Index.new(index_attributes, **options)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-sql-3.7.0 lib/rom/sql/schema/index_dsl.rb