class Shoulda::Matchers::ActiveModel::NumericalityMatchers::ComparisonMatcher
@private
Constants
- ERROR_MESSAGES
Public Class Methods
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 34 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
Calls superclass method
Public Instance Methods
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 86 def comparison_description "#{comparison_expectation} #{@value}" end
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 69 def expects_custom_validation_message? @expects_custom_validation_message end
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 78 def failure_message last_failing_submatcher.failure_message end
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 82 def failure_message_when_negated last_failing_submatcher.failure_message_when_negated end
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 58 def for(attribute) @attribute = attribute self end
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 73 def matches?(subject) @subject = subject all_bounds_correct? end
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 46 def simple_description description = '' if expects_strict? description << ' strictly' end description + "disallow :#{attribute} from being a number that is not " + "#{comparison_expectation} #{@value}" end
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 63 def with_message(message) @expects_custom_validation_message = true @message = message self end
Private Instance Methods
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 92 def all_bounds_correct? failing_submatchers.empty? end
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 135 def assertions ERROR_MESSAGES[@operator][:assertions] end
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 121 def comparison_combos diffs_to_compare.zip(submatcher_method_names) end
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 150 def comparison_expectation ERROR_MESSAGES[@operator][:label].to_s.tr('_', ' ') end
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 139 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
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 96 def failing_submatchers submatchers_and_results. select { |x| !x[:matched] }. map { |x| x[:matcher] } end
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 102 def last_failing_submatcher failing_submatchers.last end
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 125 def submatcher_method_names assertions.map do |value| if value :allow_value_matcher else :disallow_value_matcher end end end
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 106 def submatchers @_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
Source
# File lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb, line 115 def submatchers_and_results @_submatchers_and_results ||= submatchers.map do |matcher| { matcher: matcher, matched: matcher.matches?(@subject) } end end