Class: RiotMongoMapper::HasAssociationAssertion

Inherits:
Riot::AssertionMacro
  • Object
show all
Defined in:
lib/riot-mongo_mapper/has_association.rb

Instance Method Summary collapse

Instance Method Details

#evaluate(model, *association_macro_info) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/riot-mongo_mapper/has_association.rb', line 5

def evaluate(model, *association_macro_info)
  association_name, key_name, options = association_macro_info
  association = model.associations[key_name.to_sym]
  association_class = case association_name
    when :many
      MongoMapper::Plugins::Associations::ManyAssociation
    when :one
      MongoMapper::Plugins::Associations::OneAssociation
    end
  if association.nil?
    fail("expected #{model} to have association #{association}")
  else
    association_valid = association.class == association_class
    options_valid = options ? options.all? { |field,value| association.options[field] == value } : true
    options_valid && association_valid ? pass("#{model} has association '#{association_name}' on '#{key_name}' with options #{options.inspect}") :
                                         fail("expected #{model} to have association #{association_name} with options #{options.inspect} on key #{key_name}")
  end
end