Class: DataMapperMatchers::BelongsTo

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/matchers/dm/belongs_to.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ BelongsTo

args<Symbol>: :products



8
9
10
# File 'lib/spec/matchers/dm/belongs_to.rb', line 8

def initialize(name)
  @name = name
end

Instance Method Details

#descriptionObject



20
21
22
# File 'lib/spec/matchers/dm/belongs_to.rb', line 20

def description
  "belongs to"
end

#failure_messageObject



24
25
26
27
28
29
30
31
32
# File 'lib/spec/matchers/dm/belongs_to.rb', line 24

def failure_message
  belonging = @model.relationships.select { |name, relationship| relationship.name == @name && relationship.child_model == @model }
  belonging.map! { |array| array.first }
  if belonging.empty?
    "expected to belongs to #@name, but does not belongs to any model"
  else
    "expected to belongs to #@name, but belongs to just the following relationships: #{belonging.inspect}"
  end
end

#matches?(model) ⇒ Boolean

args<Model>: Category

Returns:

  • (Boolean)


13
14
15
16
17
18
# File 'lib/spec/matchers/dm/belongs_to.rb', line 13

def matches?(model)
  @model = model
  relationship = @model.relationships[@name]
  return false unless relationship
  relationship.name == @name && relationship.child_model == @model
end

#negative_failure_messageObject



34
35
36
# File 'lib/spec/matchers/dm/belongs_to.rb', line 34

def negative_failure_message
  "expected not to belongs to #@name, but belonged"
end