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

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

Instance Method Summary collapse

Methods inherited from HaveValidationMatcher

#failure_message_for_should, #failure_message_for_should_not, #on, #with_message

Constructor Details

#initialize(field) ⇒ ValidateUniquenessOfMatcher

Returns a new instance of ValidateUniquenessOfMatcher.



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

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

Instance Method Details

#allow_blank?(allow_blank) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#case_insensitiveObject



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

def case_insensitive
  @case_insensitive = true
  self
end

#descriptionObject



35
36
37
38
39
40
41
# File 'lib/matchers/validations/uniqueness_of.rb', line 35

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
  super << options_desc.to_sentence
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  @result
end

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



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

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