class Shoulda::Matchers::ActiveModel::NumericalityMatchers::ComparisonMatcher

@private

Constants

ERROR_MESSAGES

Public Class Methods

new(numericality_matcher, value, operator) click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 15
def initialize(numericality_matcher, value, operator)
  unless numericality_matcher.respond_to? :diff_to_compare
    raise ArgumentError, 'numericality_matcher is invalid'
  end
  @numericality_matcher = numericality_matcher
  @value = value
  @operator = operator
  @message = ERROR_MESSAGES[operator]
  @comparison_combos = comparison_combos
  @strict = false
end

Public Instance Methods

comparison_description() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 41
def comparison_description
  "#{expectation} #{@value}"
end
failure_message() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 45
def failure_message
  last_failing_submatcher.failure_message
end
Also aliased as: failure_message_for_should
failure_message_for_should()
Alias for: failure_message
failure_message_for_should_not()
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 50
def failure_message_when_negated
  last_failing_submatcher.failure_message_when_negated
end
for(attribute) click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 27
def for(attribute)
  @attribute = attribute
  self
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 32
def matches?(subject)
  @subject = subject
  all_bounds_correct?
end
with_message(message) click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 37
def with_message(message)
  @message = message
end

Private Instance Methods

all_bounds_correct?() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 57
def all_bounds_correct?
  failing_submatchers.empty?
end
assertions() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 101
def assertions
  case @operator
  when :>
    [false, false, true]
  when :>=
    [false, true, true]
  when :==
    [false, true, false]
  when :<
    [true, false, false]
  when :<=
    [true, true, false]
  end
end
comparison_combos() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 87
def comparison_combos
  diffs_to_compare.zip(submatcher_method_names)
end
diffs_to_compare() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 116
def diffs_to_compare
  diff = @numericality_matcher.diff_to_compare
  [-diff, 0, diff]
end
expectation() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 121
def expectation
  case @operator
    when :> then "greater than"
    when :>= then "greater than or equal to"
    when :== then "equal to"
    when :< then "less than"
    when :<= then "less than or equal to"
  end
end
failing_submatchers() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 61
def failing_submatchers
  submatchers_and_results.
    select { |x| !x[:matched] }.
    map { |x| x[:matcher] }
end
last_failing_submatcher() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 67
def last_failing_submatcher
  failing_submatchers.last
end
submatcher_method_names() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 91
def submatcher_method_names
  assertions.map do |value|
    if value
      :allow_value_matcher
    else
      :disallow_value_matcher
    end
  end
end
submatchers() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 71
def submatchers
  @_submatchers ||=
    comparison_combos.map do |diff, submatcher_method_name|
      matcher = __send__(submatcher_method_name, @value + diff, nil)
      matcher.with_message(@message, values: { count: @value })
      matcher
    end
end
submatchers_and_results() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 80
def submatchers_and_results
  @_submatchers_and_results ||=
    submatchers.map do |matcher|
      { matcher: matcher, matched: matcher.matches?(@subject) }
    end
end