Class: Mongoid::Matchers::Associations::HaveAssociationMatcher

Inherits:
Matcher
  • Object
show all
Defined in:
lib/matchers/associations/associations.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, type) ⇒ HaveAssociationMatcher

Returns a new instance of HaveAssociationMatcher.



23
24
25
26
27
28
# 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

Instance Method Details

#as_inverse_of(inverse_of) ⇒ Object



36
37
38
39
40
# 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_messageObject



55
56
57
# File 'lib/matchers/associations/associations.rb', line 55

def failure_message
  "#{@klass} to #{@description}, got #{@negative_message}"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
# 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_messageObject



59
60
61
# File 'lib/matchers/associations/associations.rb', line 59

def negative_failure_message
  "#{@klass} to not #{@description}, got #{@positive_message}"
end

#of_type(klass) ⇒ Object



30
31
32
33
34
# File 'lib/matchers/associations/associations.rb', line 30

def of_type klass
  @association[:class] = klass
  @description << " of type #{@association[:class].inspect}"
  self
end