Class: Shoulda::Lotus::Matchers::ValidateLengthOfMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/lotus/matchers/validate_length_of_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ ValidateLengthOfMatcher



9
10
11
# File 'lib/shoulda/lotus/matchers/validate_length_of_matcher.rb', line 9

def initialize(attribute)
  @attribute = attribute
end

Instance Method Details

#descriptionObject



25
26
27
# File 'lib/shoulda/lotus/matchers/validate_length_of_matcher.rb', line 25

def description
  "'#{@attribute}' size between '#{@minimum}..#{@maximum}'"
end

#failure_messageObject



29
30
31
# File 'lib/shoulda/lotus/matchers/validate_length_of_matcher.rb', line 29

def failure_message
  "'#{@attribute}' is not between '#{@minimum}..#{@maximum}'"
end

#failure_message_when_negatedObject



33
34
35
# File 'lib/shoulda/lotus/matchers/validate_length_of_matcher.rb', line 33

def failure_message_when_negated
  "'#{@attribute}' is between '#{@minimum}..#{@maximum}'"
end

#is_at_least(minimum) ⇒ Object



37
38
39
40
# File 'lib/shoulda/lotus/matchers/validate_length_of_matcher.rb', line 37

def is_at_least(minimum)
  @minimum = minimum
  self
end

#is_at_most(maximum) ⇒ Object



42
43
44
45
# File 'lib/shoulda/lotus/matchers/validate_length_of_matcher.rb', line 42

def is_at_most(maximum)
  @maximum = maximum
  self
end

#is_equal_to(limit) ⇒ Object



47
48
49
50
# File 'lib/shoulda/lotus/matchers/validate_length_of_matcher.rb', line 47

def is_equal_to(limit)
  @minimum, @maximum = limit
  self
end

#matches?(target) ⇒ Boolean



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/shoulda/lotus/matchers/validate_length_of_matcher.rb', line 13

def matches?(target)
  errors = []

  target.send("#{@attribute}=", '*' * (minimum - 1))
  errors << Matcher.new(target, @attribute, :size).matches?

  target.send("#{@attribute}=", '*' * (maximum + 1))
  errors << Matcher.new(target, @attribute, :size).matches?

  errors.all? { |error| error }
end