Class: Mongomatic::Expectations::OfLength

Inherits:
Expectation
  • Object
show all
Defined in:
lib/mongomatic/expectations/of_length.rb

Instance Attribute Summary

Attributes inherited from Expectation

#instance, #message, #opts, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expectation

#add_error_msg, define_to_be?, define_to_not_be?, inherited, #initialize

Constructor Details

This class inherits a constructor from Mongomatic::Expectations::Expectation

Class Method Details

.nameObject



4
5
6
# File 'lib/mongomatic/expectations/of_length.rb', line 4

def self.name
  "of_length"
end

Instance Method Details

#to_beObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/mongomatic/expectations/of_length.rb', line 8

def to_be
  return true if opts[:allow_nil] && value.nil?
    
  length = (value) ? value.size : value.to_s.size
  add_error_msg if opts[:minimum] && length < opts[:minimum]
  add_error_msg if opts[:maximum] && length > opts[:maximum]
  if opts[:range]
    add_error_msg unless opts[:range].include?(length)
  end  
end