class MiniTest::Matchers::ActiveModel::ValidateLengthMatcher

Public Class Methods

new(attr) click to toggle source
Calls superclass method
# File lib/matchers/validate_length_matcher.rb, line 49
def initialize attr
  @minimum, @maximum, @within, @is = nil

  super attr, :length
end

Public Instance Methods

description() click to toggle source
Calls superclass method
# File lib/matchers/validate_length_matcher.rb, line 99
def description
  desc = []
  desc << " with minimum #{@minimum}" if @minimum
  desc << " with maximum #{@maximum}" if @maximum
  desc << " within range #{@within}"  if @within
  desc << " is equal to #{@is}"       if @is

  super << desc.to_sentence
end
in(value)
Alias for: within
is(value) click to toggle source
# File lib/matchers/validate_length_matcher.rb, line 76
def is value
  @is = value
  self
end
Also aliased as: is_equal_to
is_at_least(value)
Alias for: with_minimum
is_at_most(value)
Alias for: with_maximum
is_equal_to(value)
Alias for: is
matches?(subject) click to toggle source

TODO: add helper methods for :too_long, :too_short and :wrong_length options. See api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_length_of

Calls superclass method
# File lib/matchers/validate_length_matcher.rb, line 86
def matches? subject
  validate_invalid_options! @minimum, @maximum, @within, @is

  return false unless @result = super(subject)

  check :minimum  if @minimum
  check :maximum  if @maximum
  check_range     if @within
  check_precision if @is

  @result
end
with_max(value)
Alias for: with_maximum
with_maximum(value) click to toggle source
# File lib/matchers/validate_length_matcher.rb, line 62
def with_maximum value
  @maximum = value
  self
end
Also aliased as: with_max, is_at_most
with_min(value)
Alias for: with_minimum
with_minimum(value) click to toggle source
# File lib/matchers/validate_length_matcher.rb, line 55
def with_minimum value
  @minimum = value
  self
end
Also aliased as: with_min, is_at_least
within(value) click to toggle source
# File lib/matchers/validate_length_matcher.rb, line 69
def within value
  raise ArgumentError, 'within must be a Range' unless value.is_a? Range
  @within = value
  self
end
Also aliased as: in

Private Instance Methods

check(option) click to toggle source
# File lib/matchers/validate_length_matcher.rb, line 111
def check option
  actual = @validator.options[option]

  if actual == instance_variable_get("@#{option}")
    @positive_message << " with #{option} of #{actual}"
  else
    @negative_message << " with #{option} of #{actual}"
    @result = false
  end
end
check_precision() click to toggle source
# File lib/matchers/validate_length_matcher.rb, line 135
def check_precision
  actual = @validator.options[:is]

  if actual == @is
    @positive_message << " is equal to #{@is}"
  else
    @negative_message << " is equal to #{actual}"
    @result = false
  end
end
check_range() click to toggle source
# File lib/matchers/validate_length_matcher.rb, line 122
def check_range
  min, max   = @within.min, @within.max
  actual_min = @validator.options[:minimum]
  actual_max = @validator.options[:maximum]

  if actual_min == min && actual_max == max
    @positive_message << " with range #{@within}"
  else
    @negative_message << " with range #{actual_min..actual_max}"
    @result = false
  end
end