Class: MiniTest::Matchers::ActiveModel::ValidateLengthMatcher

Inherits:
ValidationMatcher show all
Defined in:
lib/matchers/validate_length_matcher.rb

Instance Method Summary collapse

Methods inherited from ValidationMatcher

#failure_message, #negative_failure_message, #on, #with_message

Methods included from Helpers

#class_of, #to_sentence

Constructor Details

#initialize(attr) ⇒ ValidateLengthMatcher


49
50
51
52
53
# File 'lib/matchers/validate_length_matcher.rb', line 49

def initialize attr
  @minimum, @maximum, @within, @is = nil

  super attr, :length
end

Instance Method Details

#descriptionObject


99
100
101
102
103
104
105
106
107
# 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

#is(value) ⇒ Object Also known as: is_equal_to


76
77
78
79
# File 'lib/matchers/validate_length_matcher.rb', line 76

def is value
  @is = value
  self
end

#matches?(subject) ⇒ Boolean

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


86
87
88
89
90
91
92
93
94
95
96
97
# 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_maximum(value) ⇒ Object Also known as: with_max, is_at_most


62
63
64
65
# File 'lib/matchers/validate_length_matcher.rb', line 62

def with_maximum value
  @maximum = value
  self
end

#with_minimum(value) ⇒ Object Also known as: with_min, is_at_least


55
56
57
58
# File 'lib/matchers/validate_length_matcher.rb', line 55

def with_minimum value
  @minimum = value
  self
end

#within(value) ⇒ Object Also known as: in

Raises:

  • (ArgumentError)

69
70
71
72
73
# 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