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

@private

Public Class Methods

new(numericality_matcher, value, operator) click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 7
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 = nil
end

Public Instance Methods

comparison_description() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 31
def comparison_description
  "#{expectation} #{@value}"
end
for(attribute) click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 17
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 22
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 27
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 66
def all_bounds_correct?
  comparison_combos.all? do |diff, checker_type|
    __send__(checker_type, @value + diff, @message)
  end
end
comparison_combos() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 37
def comparison_combos
  allow = :allows_value_of
  disallow = :disallows_value_of
  checker_types =
    case @operator
      when :> then [allow, disallow, disallow]
      when :>= then [allow, allow, disallow]
      when :== then [disallow, allow, disallow]
      when :< then [disallow, disallow, allow]
      when :<= then [disallow, allow, allow]
    end
  diffs_to_compare.zip(checker_types)
end
diffs_to_compare() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 51
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 56
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