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
- #description ⇒ Object
- #failure_message_for_should ⇒ Object
- #failure_message_for_should_not ⇒ Object
-
#initialize(name, association_type) ⇒ HaveAssociationMatcher
constructor
A new instance of HaveAssociationMatcher.
- #matches?(actual) ⇒ Boolean
- #of_type(klass) ⇒ Object
- #type_description(type = nil, passive = true) ⇒ Object
Constructor Details
#initialize(name, association_type) ⇒ HaveAssociationMatcher
Returns a new instance of HaveAssociationMatcher.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/matchers/associations.rb', line 5 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}" end |
Instance Method Details
#as_inverse_of(association_inverse_name) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/matchers/associations.rb', line 23 def as_inverse_of(association_inverse_name) raise "#{@association[:type].inspect} does not respond to :inverse_of" unless [Mongoid::Associations::BelongsToRelated, Mongoid::Associations::EmbeddedIn].include?(@association[:type]) @association[:inverse_of] = association_inverse_name.to_s @expectation_message << " which is an inverse of #{@association[:inverse_of].inspect}" self end |
#description ⇒ Object
76 77 78 |
# File 'lib/matchers/associations.rb', line 76 def description @expectation_message end |
#failure_message_for_should ⇒ Object
68 69 70 |
# File 'lib/matchers/associations.rb', line 68 def "Expected #{@actual.inspect} to #{@expectation_message}, got #{@negative_result_message}" end |
#failure_message_for_should_not ⇒ Object
72 73 74 |
# File 'lib/matchers/associations.rb', line 72 def "Expected #{@actual.inspect} to not #{@expectation_message}, got #{@positive_result_message}" end |
#matches?(actual) ⇒ Boolean
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/matchers/associations.rb', line 30 def matches?(actual) @actual = actual.is_a?(Class) ? actual : actual.class association = @actual.associations[@association[:name]] if association.nil? @negative_result_message = "no association named #{@association[:name]}" return false else @positive_result_message = "association named #{@association[:name]}" end if association.association != @association[:type] @negative_result_message = "#{@actual.inspect} #{type_description(association.association, false)} #{@association[:name]}" return false else @positive_result_message = "#{@actual.inspect} #{type_description(association.association, false)} #{@association[:name]}" end if @association[:class] != association.klass @negative_result_message = "#{@positive_result_message} of type #{association.klass.inspect}" return false else @positive_result_message = "#{@positive_result_message} of type #{association.klass.inspect}" end if @association[:inverse_of] if @association[:inverse_of].to_s != association.inverse_of.to_s @negative_result_message = "#{@positive_result_message} which is an inverse of #{association.inverse_of}" return false else @positive_result_message = "#{@positive_result_message} which is an inverse of #{association.inverse_of}" end end true end |
#of_type(klass) ⇒ Object
17 18 19 20 21 |
# File 'lib/matchers/associations.rb', line 17 def of_type(klass) @association[:class] = klass @expectation_message << " of type #{@association[:class].inspect}" self end |
#type_description(type = nil, passive = true) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/matchers/associations.rb', line 80 def type_description(type = nil, passive = true) type ||= @association[:type] case type.name when "Mongoid::Associations::EmbedsOne" (passive ? 'embed' : 'embeds') << ' one' when "Mongoid::Associations::EmbedsMany" (passive ? 'embed' : 'embeds') << ' many' when "Mongoid::Associations::EmbeddedIn" (passive ? 'be' : 'is') << ' embedded in' when "Mongoid::Associations::HasOneRelated" (passive ? 'have' : 'has') << ' one related' when "Mongoid::Associations::HasManyRelated" (passive ? 'have' : 'has') << ' many related' when "Mongoid::Associations::BelongsToRelated" (passive ? 'belong' : 'belongs') << ' to related' else raise "Unknown association type" end end |