class Mongoid::Matchers::Associations::HaveAssociationMatcher

Public Class Methods

new(name, type) click to toggle source
# File lib/matchers/associations/associations.rb, line 23
def initialize name, type
  @association = {}
  @association[:name] = name.to_s
  @association[:type] = type
  @description = "#{type_description} #{@association[:name].inspect}"
end

Public Instance Methods

as_inverse_of(inverse_of) click to toggle source
# File lib/matchers/associations/associations.rb, line 36
def as_inverse_of inverse_of
  @association[:inverse_of] = inverse_of
  @description << " as the inverse of #{@association[:inverse_of].inspect}"
  self
end
failure_message() click to toggle source
# File lib/matchers/associations/associations.rb, line 55
def failure_message
  "#{@klass} to #{@description}, got #{@negative_message}"
end
matches?(subject) click to toggle source
# File lib/matchers/associations/associations.rb, line 42
def matches? subject
  @klass    = class_of subject
  @metadata = @klass.relations[@association[:name]]
  @result   = true

  check_association_name
  check_association_type
  check_association_class if @association[:class]
  check_association_inverse_of if @association[:inverse_of]

  @result
end
negative_failure_message() click to toggle source
# File lib/matchers/associations/associations.rb, line 59
def negative_failure_message
  "#{@klass} to not #{@description}, got #{@positive_message}"
end
of_type(klass) click to toggle source
# File lib/matchers/associations/associations.rb, line 30
def of_type klass
  @association[:class] = klass
  @description << " of type #{@association[:class].inspect}"
  self
end

Private Instance Methods

association_type_failure_message() click to toggle source
# File lib/matchers/associations/associations.rb, line 89
def association_type_failure_message
  msg = "#{@klass.inspect}"
  msg << " #{type_description(@association[:type], false)}"
  msg << " #{@association[:name].inspect}"

  msg
end
check_association_class() click to toggle source
# File lib/matchers/associations/associations.rb, line 97
def check_association_class
  if @association[:class] != @metadata.klass
    @negative_message = "#{@positive_message} of type #{@metadata.klass}"
    @result = false
  else
    @positive_message << " of type #{@metadata.klass}" if @association[:class]
  end
end
check_association_inverse_of() click to toggle source
# File lib/matchers/associations/associations.rb, line 106
def check_association_inverse_of
  if @association[:inverse_of] != @metadata.inverse_of
    @negative_message = "..."
    @result = false
  else
    @positive_message << " as inverse of #{@metadata.inverse_of}" if @association[:inverse_of]
  end
end
check_association_name() click to toggle source
# File lib/matchers/associations/associations.rb, line 65
def check_association_name
  if @metadata.nil?
    @negative_message = "no association named #{@association[:name].inspect}"
    @result = false
  else
    @positive_message = "association named #{@association[:name].inspect}"
  end
end
check_association_type() click to toggle source
# File lib/matchers/associations/associations.rb, line 74
def check_association_type
  is_failure = if Mongoid::Compatibility::Version.mongoid7_or_newer?
    !@metadata.nil? && @metadata.class != @association[:type]
  else
    !@metadata.nil? && @metadata.relation != @association[:type]
  end

  if is_failure
    @negative_message = association_type_failure_message
    @result = false
  else
    @positive_message = association_type_failure_message
  end
end
type_description(type = nil, passive = true) click to toggle source
# File lib/matchers/associations/associations.rb, line 115
def type_description type = nil, passive = true
  type ||= @association[:type]
  case type.name
  when HAS_ONE.name
    (passive ? 'reference' : 'references') << ' one'
  when HAS_MANY.name
    (passive ? 'reference' : 'references') << ' many'
  when HAS_AND_BELONGS_TO_MANY.name
    (passive ? 'reference' : 'references') << ' and referenced in many'
  when BELONGS_TO.name
    (passive ? 'be referenced' : 'referenced') << ' in'
  when EMBEDS_ONE.name
    (passive ? 'embed' : 'embeds') << ' one'
  when EMBEDS_MANY.name
    (passive ? 'embed' : 'embeds') << ' many'
  when EMBEDDED_IN.name
    (passive ? 'be' : 'is') << ' embedded in'
  else
    raise "Unknown association type #{type}"
  end
end