Class: Shoulda::Matchers::ActiveModel::NumericalityMatchers::RangeMatcher

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

Constant Summary collapse

OPERATORS =
[:>=, :<=].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_custom_validation_message?, #expects_strict?, #failure_message, #failure_message_when_negated, #on, #strict, #with_message

Methods included from Qualifiers::IgnoringInterferenceByWriter

#ignoring_interference_by_writer

Constructor Details

#initialize(numericality_matcher, attribute, range) ⇒ RangeMatcher

Returns a new instance of RangeMatcher.



13
14
15
16
17
18
19
20
21
22
# File 'lib/shoulda/matchers/active_model/numericality_matchers/range_matcher.rb', line 13

def initialize(numericality_matcher, attribute, range)
  super(attribute)
  unless numericality_matcher.respond_to? :diff_to_compare
    raise ArgumentError, 'numericality_matcher is invalid'
  end

  @numericality_matcher = numericality_matcher
  @range = range
  @attribute = attribute
end

Instance Method Details

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/shoulda/matchers/active_model/numericality_matchers/range_matcher.rb', line 24

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

#range_descriptionObject



41
42
43
# File 'lib/shoulda/matchers/active_model/numericality_matchers/range_matcher.rb', line 41

def range_description
  "from #{Shoulda::Matchers::Util.inspect_range(@range)}"
end

#simple_descriptionObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/shoulda/matchers/active_model/numericality_matchers/range_matcher.rb', line 29

def simple_description
  description = ''

  if expects_strict?
    description << ' strictly'
  end

  description +
    "disallow :#{attribute} from being a number that is not " +
    range_description
end

#submatchersObject



45
46
47
# File 'lib/shoulda/matchers/active_model/numericality_matchers/range_matcher.rb', line 45

def submatchers
  @_submatchers ||= NumericalityMatchers::Submatchers.new(build_submatchers)
end