Sha256: 7860806089626b6a0a1430a3dd2b8acc130e8601b22eac6e0e5ee7affba967b8

Contents?: true

Size: 1.28 KB

Versions: 7

Compression:

Stored size: 1.28 KB

Contents

module AutomaticForeignKeyMatchers

  class HaveIndex

    def initialize(expectation, options = {})
      set_required_columns(expectation, options)
    end

    def matches?(model)
      @model = model
      @model.indexes.any? do |index|
        index.columns.to_set == @required_columns &&
          (@unique  ? index.unique : true) &&
          (@name    ? index.name == @name.to_s : true)
      end
    end

    def failure_message_for_should(should_not = false)
      invert = should_not ? "not to" : ""
      "Expected #{@model.table_name} to #{invert} contain index on #{@required_columns.entries.inspect}"
    end

    def failure_message_for_should_not
      failure_message_for_should(true)
    end

    def on(expectation)
      set_required_columns(expectation)
      self
    end

    private
    def set_required_columns(expectation, options = {})
      @required_columns = Array(expectation).collect(&:to_s).to_set
      @unique = options.delete(:unique)
      @name   = options.delete(:name)
    end

  end

  def have_index(*expectation)
    options = expectation.extract_options!
    HaveIndex.new(expectation, options)
  end

  def have_unique_index(*expectation)
    options = expectation.extract_options!
    options[:unique] = true
    HaveIndex.new(expectation, options)
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
automatic_foreign_key-1.3.0 spec/support/matchers/have_index.rb
automatic_foreign_key-1.2.0 spec/support/matchers/have_index.rb
automatic_foreign_key-1.1.8 spec/support/matchers/have_index.rb
automatic_foreign_key-1.1.7 spec/support/matchers/have_index.rb
automatic_foreign_key-1.1.6 spec/support/matchers/have_index.rb
automatic_foreign_key-1.1.5 spec/support/matchers/have_index.rb
automatic_foreign_key-1.1.0 spec/support/matchers/have_index.rb