Class: AcceptValuesFor::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/accept_values_for/matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(attribute, *values) ⇒ Matcher

Returns a new instance of Matcher.



5
6
7
8
9
# File 'lib/accept_values_for/matcher.rb', line 5

def initialize(attribute, *values)
  @attribute = attribute
  @values = values
  @failed_values = {}
end

Instance Method Details

#descriptionObject



42
43
44
# File 'lib/accept_values_for/matcher.rb', line 42

def description
  "accept values #{@values.map(&:inspect).join(', ')} for #{@attribute.inspect} attribute"
end

#does_not_match?(model) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/accept_values_for/matcher.rb', line 19

def does_not_match?(model)
  base_matches?(model) do |value|
    if model.errors[@attribute].to_a.empty?
      @failed_values[value] = nil
    end
  end
end

#failure_messageObject Also known as: failure_message_for_should



27
28
29
30
31
32
33
# File 'lib/accept_values_for/matcher.rb', line 27

def failure_message
  result = "expected #{@model.inspect} to accept values #{formatted_failed_values} for #{@attribute.inspect}, but it was not\n"
  sorted_failed_values.each do |key|
    result << "\nValue: #{key.inspect}\tErrors: #{@attribute} #{@failed_values[key]}"
  end
  result
end

#failure_message_when_negatedObject Also known as: failure_message_for_should_not



35
36
37
# File 'lib/accept_values_for/matcher.rb', line 35

def failure_message_when_negated
  "expected #{@model.inspect} to not accept values #{formatted_failed_values} for #{@attribute.inspect} attribute, but was"
end

#matches?(model) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
# File 'lib/accept_values_for/matcher.rb', line 11

def matches?(model)
  base_matches?(model) do |value|
    unless model.errors[@attribute].to_a.empty?
      @failed_values[value] = Array(model.errors[@attribute]).join(", ")
    end
  end
end