Class: Mongoid::Matchers::Validations::ValidateUniquenessOfMatcher

Inherits:
HaveValidationMatcher show all
Includes:
WithMessage
Defined in:
lib/matchers/validations/uniqueness_of.rb

Instance Method Summary collapse

Methods included from WithMessage

#with_message

Methods inherited from HaveValidationMatcher

#check_on, #failure_message_for_should, #failure_message_for_should_not, #on

Constructor Details

#initialize(field) ⇒ ValidateUniquenessOfMatcher

Returns a new instance of ValidateUniquenessOfMatcher.



6
7
8
# File 'lib/matchers/validations/uniqueness_of.rb', line 6

def initialize(field)
  super(field, :uniqueness)
end

Instance Method Details

#allow_blank?(allow_blank) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/matchers/validations/uniqueness_of.rb', line 21

def allow_blank?(allow_blank)
  @allow_blank = allow_blank
  self
end

#case_insensitiveObject



16
17
18
19
# File 'lib/matchers/validations/uniqueness_of.rb', line 16

def case_insensitive
  @case_insensitive = true
  self
end

#descriptionObject



37
38
39
40
41
42
43
44
# File 'lib/matchers/validations/uniqueness_of.rb', line 37

def description
  options_desc = []
  options_desc << " scoped to #{@scope.inspect}" if @scope
  options_desc << " allowing blank values" if @allow_blank
  options_desc << " allowing case insensitive values" if @case_insensitive
  options_desc << " with message '#{@expected_message}'" if @expected_message
  super << options_desc.to_sentence
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
# File 'lib/matchers/validations/uniqueness_of.rb', line 26

def matches?(actual)
  return false unless @result = super(actual)

  check_scope if @scope
  check_allow_blank if @allow_blank
  check_case_sensitivity if @case_insensitive
  check_expected_message if @expected_message

  @result
end

#scoped_to(*scope) ⇒ Object Also known as: scoped_on



10
11
12
13
# File 'lib/matchers/validations/uniqueness_of.rb', line 10

def scoped_to(*scope)
  @scope = [scope].flatten.map(&:to_sym)
  self
end