Class: Mongoid::Matchers::Associations::HaveAssociationMatcher
- Inherits:
-
Object
- Object
- Mongoid::Matchers::Associations::HaveAssociationMatcher
- Defined in:
- lib/matchers/associations.rb
Instance Method Summary (collapse)
- - (Object) as_inverse_of(association_inverse_name)
- - (Object) description
- - (Object) failure_message_for_should
- - (Object) failure_message_for_should_not
-
- (HaveAssociationMatcher) initialize(name, association_type)
constructor
A new instance of HaveAssociationMatcher.
- - (Boolean) matches?(actual)
- - (Object) of_type(klass)
- - (Object) stored_as(store_as)
- - (Object) type_description(type = nil, passive = true)
Constructor Details
- (HaveAssociationMatcher) initialize(name, association_type)
A new instance of HaveAssociationMatcher
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/matchers/associations.rb', line 16 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
- (Object) as_inverse_of(association_inverse_name)
34 35 36 37 38 39 |
# File 'lib/matchers/associations.rb', line 34 def as_inverse_of(association_inverse_name) raise "#{@association[:type].inspect} does not respond to :inverse_of" unless [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 |
- (Object) description
92 93 94 |
# File 'lib/matchers/associations.rb', line 92 def description @expectation_message end |
- (Object) failure_message_for_should
84 85 86 |
# File 'lib/matchers/associations.rb', line 84 def "Expected #{@actual.inspect} to #{@expectation_message}, got #{@negative_result_message}" end |
- (Object) failure_message_for_should_not
88 89 90 |
# File 'lib/matchers/associations.rb', line 88 def "Expected #{@actual.inspect} to not #{@expectation_message}, got #{@positive_result_message}" end |
- (Boolean) matches?(actual)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/matchers/associations.rb', line 46 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].nil? and @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 |
- (Object) of_type(klass)
28 29 30 31 32 |
# File 'lib/matchers/associations.rb', line 28 def of_type(klass) @association[:class] = klass @expectation_message << " of type #{@association[:class].inspect}" self end |
- (Object) stored_as(store_as)
41 42 43 44 |
# File 'lib/matchers/associations.rb', line 41 def stored_as(store_as) @association[:type] = HAS_MANY_AS_ARRAY if store_as == :array self end |
- (Object) type_description(type = nil, passive = true)
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/matchers/associations.rb', line 96 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_MANY_AS_ARRAY.name (passive ? 'reference' : 'references') << ' many as array' when BELONGS_TO.name (passive ? 'be referenced in' : 'referenced in') else raise "Unknown association type '%s'" % type end end |