Class: Riot::DataMapper::HasAssociation

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

Constant Summary collapse

MAPPINGS =
{
  :has_n      => 'OneToMany',
  :has_1      => 'OneToOne',
  :belongs_to => 'ManyToOne',
}

Instance Method Summary collapse

Instance Method Details

#evaluate(model, type, assoc, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/riot-datamapper/has_association.rb', line 12

def evaluate(model, type, assoc, options = {})
  relationship = model.relationships[assoc]
  fail_msg     = "expected #{model} to have an association with :#{assoc}"
  pass_msg     = "#{model} has an association with :#{assoc}"
  type_msg     = " of type '#{type}'"
  options_msg  = " with options #{options.inspect}"

  return fail(fail_msg) if relationship.nil?

  if options[:through]
    through_type = 'ManyToMany'
    if relationship.through.name == options[:through]
      pass_msg << options_msg
    else
      return fail(fail_msg + options_msg)
    end
  end

  if options[:required]
    if relationship.instance_variable_get("@required") == options[:required]
      pass_msg << options_msg unless options[:through]
    else
      return fail(fail_msg + options_msg)
    end
  end

  if relationship.class.to_s.include?(through_type || MAPPINGS[type])
    pass_msg << type_msg
  else
    return fail(fail_msg + type_msg)
  end

  pass(pass_msg)
end