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

@private

Constants

ERROR_MESSAGES

Public Class Methods

new(numericality_matcher, value, operator) click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 38
def initialize(numericality_matcher, value, operator)
  super(nil)
  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][:label]
end

Public Instance Methods

comparison_description() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 82
def comparison_description
  "#{comparison_expectation} #{@value}"
end
expects_custom_validation_message?() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 73
def expects_custom_validation_message?
  @expects_custom_validation_message
end
for(attribute) click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 62
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 77
def matches?(subject)
  @subject = subject
  submatchers.matches?(subject)
end
simple_description() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 50
def simple_description
  description = ''

  if expects_strict?
    description << ' strictly'
  end

  description +
    "disallow :#{attribute} from being a number that is not " +
    "#{comparison_expectation} #{@value}"
end
submatchers() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 86
def submatchers
  @_submatchers ||= NumericalityMatchers::Submatchers.new(build_submatchers)
end
with_message(message) click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 67
def with_message(message)
  @expects_custom_validation_message = true
  @message = message
  self
end

Private Instance Methods

assertions() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 114
def assertions
  ERROR_MESSAGES[@operator][:assertions]
end
build_submatchers() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 92
def build_submatchers
  comparison_combos.map do |diff, submatcher_method_name|
    matcher = __send__(submatcher_method_name, diff, nil)
    matcher.with_message(@message, values: { count: @value })
    matcher
  end
end
comparison_combos() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 100
def comparison_combos
  diffs_to_compare.zip(submatcher_method_names)
end
comparison_expectation() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 129
def comparison_expectation
  ERROR_MESSAGES[@operator][:label].to_s.tr('_', ' ')
end
diffs_to_compare() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 118
def diffs_to_compare
  diff_to_compare = @numericality_matcher.diff_to_compare
  values = [-1, 0, 1].map { |sign| @value + (diff_to_compare * sign) }

  if @numericality_matcher.given_numeric_column?
    values
  else
    values.map(&:to_s)
  end
end
submatcher_method_names() click to toggle source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 104
def submatcher_method_names
  assertions.map do |value|
    if value
      :allow_value_matcher
    else
      :disallow_value_matcher
    end
  end
end