Class: Mongoid::Matchers::Associations::HaveAssociationMatcher
- Inherits:
-
Object
- Object
- Mongoid::Matchers::Associations::HaveAssociationMatcher
- Defined in:
- lib/matchers/associations.rb
Instance Method Summary collapse
- #as_inverse_of(association_inverse_name) ⇒ Object
- #cyclic ⇒ Object
- #description ⇒ Object
- #failure_message_for_should ⇒ Object (also: #failure_message)
- #failure_message_for_should_not ⇒ Object (also: #failure_message_when_negated)
-
#initialize(name, association_type) ⇒ HaveAssociationMatcher
constructor
A new instance of HaveAssociationMatcher.
- #matches?(actual) ⇒ Boolean
- #of_type(klass) ⇒ Object
- #ordered_by(association_field_name) ⇒ Object
- #stored_as(store_as) ⇒ Object
- #type_description(type = nil, passive = true) ⇒ Object
- #with_autobuild ⇒ Object
- #with_autosave ⇒ Object
- #with_cascading_callbacks ⇒ Object
- #with_counter_cache ⇒ Object
- #with_dependent(method_name) ⇒ Object
- #with_foreign_key(foreign_key) ⇒ Object
- #with_index ⇒ Object
- #with_optional ⇒ Object
- #with_polymorphism ⇒ Object
- #with_touch(touch = true) ⇒ Object
Constructor Details
#initialize(name, association_type) ⇒ HaveAssociationMatcher
Returns a new instance of HaveAssociationMatcher.
29 30 31 32 33 34 35 |
# File 'lib/matchers/associations.rb', line 29 def initialize(name, association_type) @association = {} @association[:name] = name.to_s @association[:type] = association_type @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
43 44 45 46 47 |
# File 'lib/matchers/associations.rb', line 43 def as_inverse_of(association_inverse_name) @association[:inverse_of] = association_inverse_name.to_s @expectation_message << " which is an inverse of #{@association[:inverse_of].inspect}" self end |
#cyclic ⇒ Object
98 99 100 101 102 |
# File 'lib/matchers/associations.rb', line 98 def cyclic @association[:cyclic] = true @expectation_message << " which specifies cyclic as #{@association[:cyclic]}" self end |
#description ⇒ Object
313 314 315 |
# File 'lib/matchers/associations.rb', line 313 def description @expectation_message end |
#failure_message_for_should ⇒ Object Also known as: failure_message
302 303 304 |
# File 'lib/matchers/associations.rb', line 302 def "Expected #{@actual.inspect} to #{@expectation_message}, got #{@negative_result_message}" end |
#failure_message_for_should_not ⇒ Object Also known as: failure_message_when_negated
306 307 308 |
# File 'lib/matchers/associations.rb', line 306 def "Expected #{@actual.inspect} to not #{@expectation_message}, got #{@positive_result_message}" end |
#matches?(actual) ⇒ Boolean
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/matchers/associations.rb', line 134 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 = if Mongoid::Compatibility::Version.mongoid7_or_newer? .class else .relation end 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? && (@association[:class] != .klass) @negative_result_message = "#{@positive_result_message} of type #{.klass.inspect}" return false else @positive_result_message = "#{@positive_result_message}#{" of type #{.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 #{.inverse_of}" return false else @positive_result_message = "#{@positive_result_message} which is an inverse of #{.inverse_of}" end end if @association[:order] if @association[:order].to_s != .order.to_s @negative_result_message = "#{@positive_result_message} ordered by #{.order}" return false else @positive_result_message = "#{@positive_result_message} ordered by #{.order}" end end if @association[:order_operator] if @association[:order_operator] != .order.operator @negative_result_message = "#{@positive_result_message} #{order_way(@association[:order_operator] * -1)}" return false else @positive_result_message = "#{@positive_result_message} #{order_way(@association[:order_operator])}" end end if @association[:dependent] if @association[:dependent].to_s != .dependent.to_s @negative_result_message = "#{@positive_result_message} which specified dependent as #{.dependent}" return false else @positive_result_message = "#{@positive_result_message} which specified dependent as #{.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[:polymorphic] if .polymorphic? != true @negative_result_message = "#{@positive_result_message} which did not set polymorphic" return false else @positive_result_message = "#{@positive_result_message} which set polymorphic" end end if @association[:cascade_callbacks] if .cascading_callbacks? != true @negative_result_message = "#{@positive_result_message} which did not set cascade_callbacks" return false else @positive_result_message = "#{@positive_result_message} which set cascade_callbacks" end end if @association[:cyclic] if .cyclic? != true @negative_result_message = "#{@positive_result_message} which did not set cyclic" return false else @positive_result_message = "#{@positive_result_message} which set cyclic" end end if @association[:store_as] if .store_as != @association[:store_as] @negative_result_message = "#{@positive_result_message} which is stored as #{.store_as}" return false else @positive_result_message = "#{@positive_result_message} which is stored as #{.store_as}" end end if @association[:index] if .indexed? @positive_result_message = "#{@positive_result_message} which set index" else @negative_result_message = "#{@positive_result_message} which did not set index" return false end end if @association[:foreign_key] if .foreign_key != @association[:foreign_key] @negative_result_message = "#{@positive_result_message} with foreign key #{.foreign_key.inspect}" return false else @positive_result_message = "#{@positive_result_message} with foreign key #{.foreign_key.inspect}" end end if @association[:counter_cache] if .counter_cached? != true @negative_result_message = "#{@positive_result_message} which did not set counter_cache" return false else @positive_result_message = "#{@positive_result_message} which set counter_cache" end end unless @association[:touch].nil? if .[:touch] != @association[:touch] @negative_result_message = "#{@positive_result_message} which sets touch as #{.[:touch].inspect}" return false else @positive_result_message = "#{@positive_result_message} which sets touch as #{.[:touch].inspect}" end end if @association[:optional] if .[:optional] != true @negative_result_message = "#{@positive_result_message} which did not set optional" return false else @positive_result_message = "#{@positive_result_message} which set optional" end end true end |
#of_type(klass) ⇒ Object
37 38 39 40 41 |
# File 'lib/matchers/associations.rb', line 37 def of_type(klass) @association[:class] = klass @expectation_message << " of type #{@association[:class].inspect}" self end |
#ordered_by(association_field_name) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/matchers/associations.rb', line 49 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}" if association_field_name.is_a? association_kind_of @association[:order_operator] = association_field_name.operator @expectation_message << " #{order_way(@association[:order_operator])}" end self end |
#stored_as(store_as) ⇒ Object
104 105 106 107 108 |
# File 'lib/matchers/associations.rb', line 104 def stored_as(store_as) @association[:store_as] = store_as.to_s @expectation_message << " which is stored as #{@association[:store_as].inspect}" self end |
#type_description(type = nil, passive = true) ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/matchers/associations.rb', line 317 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 format("Unknown association type '%s'", type) end end |
#with_autobuild ⇒ Object
80 81 82 83 84 |
# File 'lib/matchers/associations.rb', line 80 def with_autobuild @association[:autobuild] = true @expectation_message << " which specifies autobuild as #{@association[:autobuild]}" self end |
#with_autosave ⇒ Object
68 69 70 71 72 |
# File 'lib/matchers/associations.rb', line 68 def with_autosave @association[:autosave] = true @expectation_message << " which specifies autosave as #{@association[:autosave]}" self end |
#with_cascading_callbacks ⇒ Object
92 93 94 95 96 |
# File 'lib/matchers/associations.rb', line 92 def with_cascading_callbacks @association[:cascade_callbacks] = true @expectation_message << " which specifies cascade_callbacks as #{@association[:cascade_callbacks]}" self end |
#with_counter_cache ⇒ Object
116 117 118 119 120 |
# File 'lib/matchers/associations.rb', line 116 def with_counter_cache @association[:counter_cache] = true @expectation_message << " which specifies counter_cache as #{@association[:counter_cache]}" self end |
#with_dependent(method_name) ⇒ Object
62 63 64 65 66 |
# File 'lib/matchers/associations.rb', line 62 def with_dependent(method_name) @association[:dependent] = method_name @expectation_message << " which specifies dependent as #{@association[:dependent]}" self end |
#with_foreign_key(foreign_key) ⇒ Object
110 111 112 113 114 |
# File 'lib/matchers/associations.rb', line 110 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_index ⇒ Object
74 75 76 77 78 |
# File 'lib/matchers/associations.rb', line 74 def with_index @association[:index] = true @expectation_message << " which specifies index as #{@association[:index]}" self end |
#with_optional ⇒ Object
128 129 130 131 132 |
# File 'lib/matchers/associations.rb', line 128 def with_optional @association[:optional] = true @expectation_message << " which specifies optional as #{@association[:optional]}" self end |
#with_polymorphism ⇒ Object
86 87 88 89 90 |
# File 'lib/matchers/associations.rb', line 86 def with_polymorphism @association[:polymorphic] = true @expectation_message << " which specifies polymorphic as #{@association[:polymorphic]}" self end |
#with_touch(touch = true) ⇒ Object
122 123 124 125 126 |
# File 'lib/matchers/associations.rb', line 122 def with_touch(touch = true) @association[:touch] = touch @expectation_message << " which specifies touch as #{@association[:touch]}" self end |