Class: Mongoid::Matchers::Validations::ValidateExclusionMatcher

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

Instance Method Summary collapse

Methods inherited from HaveValidationMatcher

#failure_message, #negative_failure_message, #on, #with_message

Methods inherited from Matcher

#failure_message, #negative_failure_message

Methods included from Helpers

#class_of, #to_sentence

Constructor Details

#initialize(field) ⇒ ValidateExclusionMatcher

Returns a new instance of ValidateExclusionMatcher.



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

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

Instance Method Details

#descriptionObject



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

def description
  if Array === @not_allowed_values
    super << " not allowing the values: #{to_sentence(@not_allowed_values)}"
  elsif @not_allowed_values
    super << " not allowing the values in #{@not_allowed_values.inspect}"
  end
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/matchers/validations/exclusion.rb', line 14

def matches?(subject)
  return false unless result = super(subject)

  if Array === @not_allowed_values
    allowed_values = @not_allowed_values - @validator.options[:in].to_a
    if allowed_values.empty?
      @positive_message << ' not allowing all values mentioned'
    else
      @negative_message << ' allowing the values:'
      @negative_message << " #{to_sentence(allowed_values)}"
      result = false
    end
  elsif @not_allowed_values
    if @not_allowed_values == @validator.options[:in]
      @positive_message << " not allowing values in #{@not_allowed_values.inspect}"
    else
      @negative_message << " not allowing values in #{@validator.options[:in].inspect}"
      result = false
    end
  end

  result
end

#to_not_allow(*values) ⇒ Object



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

def to_not_allow(*values)
  @not_allowed_values = (values.length > 1) ? values.flatten : values[0]
  self
end