Class: AcceptValuesFor::Matcher
- Inherits:
-
Object
- Object
- AcceptValuesFor::Matcher
- Defined in:
- lib/accept_values_for/matcher.rb
Instance Method Summary collapse
- #description ⇒ Object
- #does_not_match?(model) ⇒ Boolean
- #failure_message ⇒ Object (also: #failure_message_for_should)
- #failure_message_when_negated ⇒ Object (also: #failure_message_for_should_not)
-
#initialize(attribute, *values) ⇒ Matcher
constructor
A new instance of Matcher.
- #matches?(model) ⇒ Boolean
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
#description ⇒ Object
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
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_message ⇒ Object Also known as: failure_message_for_should
27 28 29 30 31 32 33 |
# File 'lib/accept_values_for/matcher.rb', line 27 def 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_negated ⇒ Object Also known as: failure_message_for_should_not
35 36 37 |
# File 'lib/accept_values_for/matcher.rb', line 35 def "expected #{@model.inspect} to not accept values #{formatted_failed_values} for #{@attribute.inspect} attribute, but was" end |
#matches?(model) ⇒ 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 |