Class: Shoulda::Matchers::ActiveModel::NumericalityMatchers::ComparisonMatcher

Inherits:
ValidationMatcher
  • Object
show all
Defined in:
lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb

Constant Summary collapse

ERROR_MESSAGES =
{
  :> => {
    label: :greater_than,
    assertions: [false, false, true],
  },
  :>= => {
    label: :greater_than_or_equal_to,
    assertions: [false, true, true],
  },
  :< => {
    label: :less_than,
    assertions: [true, false, false],
  },
  :<= => {
    label: :less_than_or_equal_to,
    assertions: [true, true, false],
  },
  :== => {
    label: :equal_to,
    assertions: [false, true, false],
  },
  :!= => {
    label: :other_than,
    assertions: [true, false, true],
  },
}.freeze

Instance Attribute Summary

Attributes included from Qualifiers::IgnoringInterferenceByWriter

#ignore_interference_by_writer

Instance Method Summary collapse

Methods inherited from ValidationMatcher

#allow_blank, #description, #does_not_match?, #expects_strict?, #on, #strict

Methods included from Qualifiers::IgnoringInterferenceByWriter

#ignoring_interference_by_writer

Constructor Details

#initialize(numericality_matcher, value, operator) ⇒ ComparisonMatcher

Returns a new instance of ComparisonMatcher.



34
35
36
37
38
39
40
41
42
43
44
# 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

Instance Method Details

#comparison_descriptionObject



86
87
88
# File 'lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb', line 86

def comparison_description
  "#{comparison_expectation} #{@value}"
end

#expects_custom_validation_message?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb', line 69

def expects_custom_validation_message?
  @expects_custom_validation_message
end

#failure_messageObject



78
79
80
# File 'lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb', line 78

def failure_message
  last_failing_submatcher.failure_message
end

#failure_message_when_negatedObject



82
83
84
# 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

#for(attribute) ⇒ Object



58
59
60
61
# File 'lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb', line 58

def for(attribute)
  @attribute = attribute
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb', line 73

def matches?(subject)
  @subject = subject
  all_bounds_correct?
end

#simple_descriptionObject



46
47
48
49
50
51
52
53
54
55
56
# 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

#with_message(message) ⇒ Object



63
64
65
66
67
# 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