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

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

Instance Method Summary collapse

Constructor Details

#initialize(name, association_type) ⇒ HaveAssociationMatcher

Returns a new instance of HaveAssociationMatcher.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/matchers/associations.rb', line 17

def initialize(name, association_type)
  @association = {}
  @association[:name] = name.to_s
  @association[:type] = association_type
  # begin
  #   @association[:class] = name.to_s.classify.constantize
  # rescue
  # end
  @expectation_message = "#{type_description} #{@association[:name].inspect}"
  @expectation_message << " of type #{@association[:class].inspect}" unless @association[:class].nil?
end

Instance Method Details

#as_inverse_of(association_inverse_name) ⇒ Object



35
36
37
38
39
40
# File 'lib/matchers/associations.rb', line 35

def as_inverse_of(association_inverse_name)
  raise "#{@association[:type].inspect} does not respond to :inverse_of" unless [HAS_MANY, HAS_AND_BELONGS_TO_MANY, BELONGS_TO, EMBEDDED_IN].include?(@association[:type])
  @association[:inverse_of] = association_inverse_name.to_s
  @expectation_message << " which is an inverse of #{@association[:inverse_of].inspect}"
  self
end

#descriptionObject



183
184
185
# File 'lib/matchers/associations.rb', line 183

def description
  @expectation_message
end

#failure_message_for_shouldObject



175
176
177
# File 'lib/matchers/associations.rb', line 175

def failure_message_for_should
  "Expected #{@actual.inspect} to #{@expectation_message}, got #{@negative_result_message}"
end

#failure_message_for_should_notObject



179
180
181
# File 'lib/matchers/associations.rb', line 179

def failure_message_for_should_not
  "Expected #{@actual.inspect} to not #{@expectation_message}, got #{@positive_result_message}"
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/matchers/associations.rb', line 83

def matches?(actual)
  @actual = actual.is_a?(Class) ? actual : actual.class
   = @actual.relations[@association[:name]]

  if .nil?
    @negative_result_message = "no association named #{@association[:name]}"
    return false
  else
    @positive_result_message = "association named #{@association[:name]}"
  end

  relation = .relation
  if relation != @association[:type]
    @negative_result_message = "#{@actual.inspect} #{type_description(relation, false)} #{@association[:name]}"
    return false
  else
    @positive_result_message = "#{@actual.inspect} #{type_description(relation, false)} #{@association[:name]}"
  end

  if !@association[:class].nil? and @association[:class] != .klass
    @negative_result_message = "#{@positive_result_message} of type #{metadata.klass.inspect}"
    return false
  else
    @positive_result_message = "#{@positive_result_message}#{" of type #{metadata.klass.inspect}" if @association[:class]}"
  end

  if @association[:inverse_of]
    if @association[:inverse_of].to_s != .inverse_of.to_s
      @negative_result_message = "#{@positive_result_message} which is an inverse of #{metadata.inverse_of}"
      return false
    else
      @positive_result_message = "#{@positive_result_message} which is an inverse of #{metadata.inverse_of}"
    end
  end

  if @association[:order]
    if @association[:order].to_s != .order.to_s
      @negative_result_message = "#{@positive_result_message} ordered by #{metadata.order}"
      return false
    else
      @positive_result_message = "#{@positive_result_message} ordered by #{metadata.order}"
    end
  end

  if @association[:dependent]
    if @association[:dependent].to_s != .dependent.to_s
      @negative_result_message = "#{@positive_result_message} which specified dependent as #{metadata.dependent}"
      return false
    else
      @positive_result_message = "#{@positive_result_message} which specified dependent as #{metadata.dependent}"
    end
  end

  if @association[:autosave]
    if .autosave != true
      @negative_result_message = "#{@positive_result_message} which did not set autosave"
      return false
    else
      @positive_result_message = "#{@positive_result_message} which set autosave"
    end
  end

  if @association[:autobuild]
    if .autobuilding? != true
      @negative_result_message = "#{@positive_result_message} which did not set autobuild"
      return false
    else
      @positive_result_message = "#{@positive_result_message} which set autobuild"
    end
  end

  if @association[:index]
    if .index != true
      @negative_result_message = "#{@positive_result_message} which did not set index"
      return false
    else
      @positive_result_message = "#{@positive_result_message} which set index"
    end
  end

  if @association[:foreign_key]
    if .foreign_key != @association[:foreign_key]
      @negative_result_message = "#{@positive_result_message} with foreign key #{metadata.foreign_key.inspect}"
      return false
    else
      @positive_result_message = "#{@positive_result_message} with foreign key #{metadata.foreign_key.inspect}"
    end
  end

  return true
end

#of_type(klass) ⇒ Object



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

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

#ordered_by(association_field_name) ⇒ Object



42
43
44
45
46
47
# File 'lib/matchers/associations.rb', line 42

def ordered_by(association_field_name)
  raise "#{@association[:type].inspect} does not respond to :order" unless [HAS_MANY, HAS_AND_BELONGS_TO_MANY, EMBEDS_MANY].include?(@association[:type])
  @association[:order] = association_field_name.to_s
  @expectation_message << " ordered by #{@association[:order].inspect}"
  self
end

#stored_as(store_as) ⇒ Object

Raises:

  • (NotImplementedError)


73
74
75
# File 'lib/matchers/associations.rb', line 73

def stored_as(store_as)
  raise NotImplementedError, "`references_many #{@association[:name]} :stored_as => :array` has been removed in Mongoid 2.0.0.rc, use `references_and_referenced_in_many #{@association[:name]}` instead"
end

#type_description(type = nil, passive = true) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/matchers/associations.rb', line 187

def type_description(type = nil, passive = true)
  type ||= @association[:type]
  case type.name
  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'
  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 in' : 'referenced in')
  else
    raise "Unknown association type '%s'" % type
  end
end

#with_autobuildObject



67
68
69
70
71
# File 'lib/matchers/associations.rb', line 67

def with_autobuild
  @association[:autobuild] = true
  @expectation_message << " which specifies autobuild as #{@association[:autobuild].to_s}"
  self
end

#with_autosaveObject



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

def with_autosave
  @association[:autosave] = true
  @expectation_message << " which specifies autosave as #{@association[:autosave].to_s}"
  self
end

#with_dependent(method_name) ⇒ Object



49
50
51
52
53
# File 'lib/matchers/associations.rb', line 49

def with_dependent(method_name)
  @association[:dependent] = method_name
  @expectation_message << " which specifies dependent as #{@association[:dependent].to_s}"
  self
end

#with_foreign_key(foreign_key) ⇒ Object



77
78
79
80
81
# File 'lib/matchers/associations.rb', line 77

def with_foreign_key(foreign_key)
  @association[:foreign_key] = foreign_key.to_s
  @expectation_message << " using foreign key #{@association[:foreign_key].inspect}"
  self
end

#with_indexObject



61
62
63
64
65
# File 'lib/matchers/associations.rb', line 61

def with_index
  @association[:index] = true
  @expectation_message << " which specifies index as #{@association[:index].to_s}"
  self
end