class RSpec::Matchers::Sequel::ReferTo

Public Class Methods

new(table) click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 51
def initialize(table)
  @table       = table
  @foreign_key = nil
  @primary_key = nil
  @on_update   = nil
  @on_delete   = nil
end

Public Instance Methods

description() click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 31
def description
  text = [%(have reference to "#{@table}")]
  text << %(with column "#{@foreign_key}") if @foreign_key
  text << %(with primary key column "#{@primary_key}") if @primary_key
  text << %(with "#{@on_update}" action on update) if @on_update
  text << %(with "#{@on_delete}" action on delete) if @on_delete

  text.join(' ')
end
failure_message() click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 41
def failure_message
  %(expected "#{@relation}" to #{description} but #{@error})
end
failure_message_when_negated() click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 45
def failure_message_when_negated
  %(did not expect "#{@relation}" to #{description})
end
from_fk(key) click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 11
def from_fk(key)
  @foreign_key = key
  self
end
matches?(subject) click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 5
def matches?(subject)
  get_reference_for(subject)

  refer_to? && correct_fk? && correct_pk? && correct_update? && correct_delete?
end
on_delete(action) click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 26
def on_delete(action)
  @on_delete = action
  self
end
on_update(action) click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 21
def on_update(action)
  @on_update = action
  self
end
to_pk(key) click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 16
def to_pk(key)
  @primary_key = key
  self
end

Private Instance Methods

correct_delete?() click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 101
def correct_delete?
  return true unless @on_delete

  if @reference[:on_delete] == @on_delete
    true
  else
    @error = %(reference does not have action "#{@on_delete}" on delete)
    false
  end
end
correct_fk?() click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 68
def correct_fk?
  return true unless @foreign_key

  if @reference[:columns].include?(@foreign_key)
    true
  else
    @error = %("#{@relation}" does not have a foreign key column "#{@foreign_key}")
    false
  end
end
correct_pk?() click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 79
def correct_pk?
  return true unless @primary_key

  if @reference[:key].first == @primary_key
    true
  else
    @error = %("#{@table}" does not have a primary key column "#{@primary_key}")
    false
  end
end
correct_update?() click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 90
def correct_update?
  return true unless @on_update

  if @reference[:on_update] == @on_update
    true
  else
    @error = %(reference does not have action "#{@on_update}" on update)
    false
  end
end
get_reference_for(relation) click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 112
def get_reference_for(relation)
  @relation  = relation
  @reference = DB.foreign_key_list(relation).select { |fk| fk[:table] == @table }.first
end
refer_to?() click to toggle source
# File lib/rspec/sequel_expectations/matchers/refer_to.rb, line 59
def refer_to?
  if @reference
    true
  else
    @error = %("#{@relation}" does not have a reference to "#{@table}")
    false
  end
end