Sha256: b3b546c127bc902c10167490e9bf790b87f9269a509d55a48651d8f05e988cf8

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

require 'set'

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

        option :attr_class

        attr_reader :registry

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

          @registry = []

          instance_exec(&block)
        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.map { |attr_names, options|
            build_index(attributes, attr_names, options)
          }.to_set
        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

3 entries across 3 versions & 1 rubygems

Version Path
rom-sql-3.1.0 lib/rom/sql/schema/index_dsl.rb
rom-sql-3.0.1 lib/rom/sql/schema/index_dsl.rb
rom-sql-3.0.0 lib/rom/sql/schema/index_dsl.rb