Class: Neo4j::RSpec::Matchers::HasN::AssociationMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j/rspec/matchers/has_n.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, macro) ⇒ AssociationMatcher



144
145
146
147
148
149
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 144

def initialize(name, macro)
  @association = {}
  @association[:name] = name.to_sym
  @association[:macro] = macro
  @expectation_message = "#{macro_description} #{@association[:name]}"
end

Instance Attribute Details

#associationObject

Returns the value of attribute association.



141
142
143
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 141

def association
  @association
end

#expectation_messageObject

Returns the value of attribute expectation_message.



141
142
143
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 141

def expectation_message
  @expectation_message
end

#negative_result_messageObject

Returns the value of attribute negative_result_message.



141
142
143
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 141

def negative_result_message
  @negative_result_message
end

#positive_result_messageObject

Returns the value of attribute positive_result_message.



141
142
143
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 141

def positive_result_message
  @positive_result_message
end

Instance Method Details

#descriptionObject



208
209
210
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 208

def description
  expectation_message
end

#failure_messageObject



200
201
202
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 200

def failure_message
  "Expected #{@actual.class} to #{expectation_message}, got #{negative_result_message}"
end

#failure_message_when_negatedObject



204
205
206
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 204

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

#macro_descriptionObject



151
152
153
154
155
156
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 151

def macro_description
  case association[:macro]
  when :has_many then "have many"
  when :has_one then "have one"
  end
end

#matchersObject



158
159
160
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 158

def matchers
  association.select { |_, v| v.is_a? BaseMatcher }
end

#matches?(actual) ⇒ Boolean



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 182

def matches?(actual)
  @actual = actual
  actual_association = actual.class.associations[association[:name]]

  unless actual_association
    self.negative_result_message = "no association named #{association[:name]}"
    return false
  else
    self.positive_result_message = "association named #{association[:name]}"
  end

  matchers.each do |_, matcher|
    return false unless matcher.matches?(actual_association)
  end

  true
end

#with_direction(direction) ⇒ Object



162
163
164
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 162

def with_direction(direction)
  DirectionMatcher.new(self, direction).add
end

#with_model_class(model_class) ⇒ Object



178
179
180
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 178

def with_model_class(model_class)
  ModelClassesMatcher.new(self, model_class).add
end

#with_origin(origin) ⇒ Object



174
175
176
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 174

def with_origin(origin)
  OriginMatcher.new(self, origin).add
end

#with_type(type) ⇒ Object



166
167
168
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 166

def with_type(type)
  TypeMatcher.new(self, type).add
end

#without_typeObject



170
171
172
# File 'lib/neo4j/rspec/matchers/has_n.rb', line 170

def without_type
  TypeMatcher.new(self, false).add_negative
end