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

Inherits:
ValidationMatcher show all
Defined in:
lib/shoulda/matchers/active_model/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?, #failure_message, #failure_message_when_negated, #on, #strict

Methods included from Qualifiers::IgnoringInterferenceByWriter

#ignoring_interference_by_writer

Constructor Details

#initialize(matcher, value, operator) ⇒ ComparisonMatcher

Returns a new instance of ComparisonMatcher.



37
38
39
40
41
42
43
44
45
46
47
# 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

Instance Method Details

#comparison_descriptionObject



81
82
83
# File 'lib/shoulda/matchers/active_model/comparison_matcher.rb', line 81

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

#comparison_submatchersObject



85
86
87
88
# 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?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/shoulda/matchers/active_model/comparison_matcher.rb', line 72

def expects_custom_validation_message?
  @expects_custom_validation_message
end

#for(attribute) ⇒ Object



61
62
63
64
# File 'lib/shoulda/matchers/active_model/comparison_matcher.rb', line 61

def for(attribute)
  @attribute = attribute
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/shoulda/matchers/active_model/comparison_matcher.rb', line 76

def matches?(subject)
  @subject = subject
  comparison_submatchers.matches?(subject)
end

#simple_descriptionObject



49
50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Object



66
67
68
69
70
# File 'lib/shoulda/matchers/active_model/comparison_matcher.rb', line 66

def with_message(message)
  @expects_custom_validation_message = true
  @message = message
  self
end