class Shoulda::Matchers::ActiveModel::ComparisonMatcher
@private
Constants
- ERROR_MESSAGES
Public Class Methods
new(matcher, value, operator)
click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/comparison_matcher.rb, line 37 def initialize(matcher, value, operator) super(nil) unless matcher.respond_to? :diff_to_compare raise ArgumentError, 'matcher is invalid' end @matcher = 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/comparison_matcher.rb, line 81 def comparison_description "#{comparison_expectation} #{@value}" end
comparison_submatchers()
click to toggle source
# File lib/shoulda/matchers/active_model/comparison_matcher.rb, line 85 def comparison_submatchers @_comparison_submatchers ||= NumericalityMatchers::Submatchers.new(build_comparison_submatchers) end
expects_custom_validation_message?()
click to toggle source
# File lib/shoulda/matchers/active_model/comparison_matcher.rb, line 72 def expects_custom_validation_message? @expects_custom_validation_message end
for(attribute)
click to toggle source
# File lib/shoulda/matchers/active_model/comparison_matcher.rb, line 61 def for(attribute) @attribute = attribute self end
matches?(subject)
click to toggle source
# File lib/shoulda/matchers/active_model/comparison_matcher.rb, line 76 def matches?(subject) @subject = subject comparison_submatchers.matches?(subject) end
simple_description()
click to toggle source
# File lib/shoulda/matchers/active_model/comparison_matcher.rb, line 49 def simple_description description = '' if expects_strict? description = ' strictly' end description + "disallow :#{attribute} from being a number that is not " + "#{comparison_expectation} #{@value}" end
with_message(message)
click to toggle source
# File lib/shoulda/matchers/active_model/comparison_matcher.rb, line 66 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/comparison_matcher.rb, line 114 def assertions ERROR_MESSAGES[@operator][:assertions] end
build_comparison_submatchers()
click to toggle source
# File lib/shoulda/matchers/active_model/comparison_matcher.rb, line 92 def build_comparison_submatchers comparison_combos.map do |diff, submatcher_method_name| matcher = __send__(submatcher_method_name, diff, nil) matcher.with_message(@message, values: { count: option_value }) matcher end end
comparison_combos()
click to toggle source
# File lib/shoulda/matchers/active_model/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/comparison_matcher.rb, line 151 def comparison_expectation ERROR_MESSAGES[@operator][:label].to_s.tr('_', ' ') end
diffs_to_compare()
click to toggle source
# File lib/shoulda/matchers/active_model/comparison_matcher.rb, line 131 def diffs_to_compare diff_to_compare = @matcher.diff_to_compare values = case option_value when String then diffs_when_string(diff_to_compare) else [-1, 0, 1].map { |sign| option_value + (diff_to_compare * sign) } end if @matcher.given_numeric_column? values else values.map(&:to_s) end end
diffs_when_string(diff_to_compare)
click to toggle source
# File lib/shoulda/matchers/active_model/comparison_matcher.rb, line 145 def diffs_when_string(diff_to_compare) [-1, 0, 1].map do |sign| option_value[0..-2] + (option_value[-1].ord + diff_to_compare * sign).chr end end
option_value()
click to toggle source
# File lib/shoulda/matchers/active_model/comparison_matcher.rb, line 118 def option_value if defined?(@_option_value) @_option_value else @_option_value = case @value when Proc then @value.call(@subject) when Symbol then @subject.send(@value) else @value end end end
submatcher_method_names()
click to toggle source
# File lib/shoulda/matchers/active_model/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