Class: Shoulda::Matchers::MatcherCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/matcher_collection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matchers) ⇒ MatcherCollection

Returns a new instance of MatcherCollection.



9
10
11
12
# File 'lib/shoulda/matchers/matcher_collection.rb', line 9

def initialize(matchers)
  @matchers = matchers
  @failed_matchers = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/shoulda/matchers/matcher_collection.rb', line 55

def method_missing(method, *args, &block)
  if all_matchers_respond_to?(method)
    matchers.each { |matcher| matcher.send(method, *args, &block) }
    self
  else
    super
  end
end

Class Method Details

.build(attrs, &block) ⇒ Object



5
6
7
# File 'lib/shoulda/matchers/matcher_collection.rb', line 5

def self.build(attrs, &block)
  new(attrs.map(&block))
end

Instance Method Details

#descriptionObject



14
15
16
# File 'lib/shoulda/matchers/matcher_collection.rb', line 14

def description
  matchers.map(&:description).join(' and ')
end

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/shoulda/matchers/matcher_collection.rb', line 26

def does_not_match?(subject)
  @subject = subject
  @failed_matchers = matchers.reject do |matcher|
    fresh_subject = fresh_subject_for(subject)
    if matcher.respond_to?(:does_not_match?)
      matcher.does_not_match?(fresh_subject)
    else
      !matcher.matches?(fresh_subject)
    end
  end
  @failed_matchers.empty?
end

#failure_messageObject



39
40
41
42
43
44
45
# File 'lib/shoulda/matchers/matcher_collection.rb', line 39

def failure_message
  if matchers.one?
    matchers.first.failure_message
  else
    build_failure_message('to')
  end
end

#failure_message_when_negatedObject



47
48
49
50
51
52
53
# File 'lib/shoulda/matchers/matcher_collection.rb', line 47

def failure_message_when_negated
  if matchers.one?
    matchers.first.failure_message_when_negated
  else
    build_failure_message('not to')
  end
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
# File 'lib/shoulda/matchers/matcher_collection.rb', line 18

def matches?(subject)
  @subject = subject
  @failed_matchers = matchers.reject do |matcher|
    matcher.matches?(fresh_subject_for(subject))
  end
  @failed_matchers.empty?
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/shoulda/matchers/matcher_collection.rb', line 64

def respond_to_missing?(method, include_private = false)
  all_matchers_respond_to?(method) || super
end