class Mongoid::Matchers::Validations::ValidateLengthOfMatcher

Public Class Methods

new(name) click to toggle source
# File lib/matchers/validations/length_of.rb, line 7
def initialize(name)
  super(name, :length)
end

Public Instance Methods

as_exactly(value) click to toggle source
# File lib/matchers/validations/length_of.rb, line 29
def as_exactly(value)
  @is = value
  self
end
Also aliased as: is
description() click to toggle source
# File lib/matchers/validations/length_of.rb, line 52
def description
  options_desc = []
  options_desc << "with minimum of #{@minimum}" if @minimum
  options_desc << "with maximum of #{@maximum}" if @maximum
  options_desc << "within the range of #{@within}" if @within
  options_desc << "as exactly #{@is}" if @is
  options_desc << "with message '#{@expected_message}'" if @expected_message
  super << " #{options_desc.to_sentence}"
end
greater_than(value)
Alias for: with_minimum
in(value)
Alias for: within
is(value)
Alias for: as_exactly
less_than(value)
Alias for: with_maximum
matches?(actual) click to toggle source
# File lib/matchers/validations/length_of.rb, line 40
def matches?(actual)
  return false unless @result = super(actual)

  check_maximum if @maximum
  check_minimum if @minimum
  check_range if @within
  check_exact if @is
  check_expected_message if @expected_message

  @result
end
with_maximum(value) click to toggle source
# File lib/matchers/validations/length_of.rb, line 11
def with_maximum(value)
  @maximum = value
  self
end
Also aliased as: less_than
with_message(message) click to toggle source
# File lib/matchers/validations/length_of.rb, line 35
def with_message(message)
  @expected_message = message
  self
end
with_minimum(value) click to toggle source
# File lib/matchers/validations/length_of.rb, line 17
def with_minimum(value)
  @minimum = value
  self
end
Also aliased as: greater_than
within(value) click to toggle source
# File lib/matchers/validations/length_of.rb, line 23
def within(value)
  @within = value
  self
end
Also aliased as: in

Private Instance Methods

actual_is() click to toggle source
# File lib/matchers/validations/length_of.rb, line 129
def actual_is
  actual_is = @validator.options[:is]
end
actual_max() click to toggle source
# File lib/matchers/validations/length_of.rb, line 137
def actual_max
  @validator.options[:maximum] || ((@validator.options[:in] || @validator.options[:within]).try(&:max))
end
actual_min() click to toggle source
# File lib/matchers/validations/length_of.rb, line 133
def actual_min
  @validator.options[:minimum] || ((@validator.options[:in] || @validator.options[:within]).try(&:min))
end
check_exact() click to toggle source
# File lib/matchers/validations/length_of.rb, line 107
def check_exact
  if actual_is == @is
    @positive_result_message << " as exactly #{@is}"
  else
    @negative_result_message << " as exactly #{actual_is}"
    @result = false
  end
end
check_expected_message() click to toggle source
# File lib/matchers/validations/length_of.rb, line 116
def check_expected_message
  actual_message = @validator.options[:message]
  if actual_message.nil?
    @negative_result_message << " with no custom message"
    @result = false
  elsif actual_message == @expected_message
    @positive_result_message << " with custom message '#{@expected_message}'"
  else
    @negative_result_message << " got message '#{actual_message}'"
    @result = false
  end
end
check_maximum() click to toggle source
# File lib/matchers/validations/length_of.rb, line 64
def check_maximum
  if actual_max.nil?
    @negative_result_message << " with no maximum"
    @result = false
  elsif actual_max == @maximum
    @positive_result_message << " with maximum of #{@maximum}"
  else
    @negative_result_message << " with maximum of #{actual_max}"
    @result = false
  end
end
check_minimum() click to toggle source
# File lib/matchers/validations/length_of.rb, line 76
def check_minimum
  if actual_min.nil?
    @negative_result_message << " with no minimum"
    @result = false
  elsif actual_min == @minimum
    @positive_result_message << " with minimum of #{@minimum}"
  else
    @negative_result_message << " with minimum of #{actual_min}"
    @result = false
  end
end
check_range() click to toggle source
# File lib/matchers/validations/length_of.rb, line 88
def check_range
  min, max = [@within.min, @within.max]
  if !actual_min.nil? and actual_max.nil?
    @negative_result_message << " with no minimum but with maximum of #{actual_max}"
    @result = false
  elsif actual_min.nil? and !actual_max.nil?
    @negative_result_message << " with minimum_of #{actual_min} but no maximum"
    @result = false
  elsif actual_min.nil? and actual_max.nil?
    @negative_result_message << " with no minimum and maximum"
    @result = false
  elsif actual_min == min && actual_max == max
    @positive_result_message << " within the range of #{@within.inspect}"
  else
    @negative_result_message << " within the range of #{(actual_min..actual_max).inspect}"
    @result = false
  end
end