Class: MiniTest::Matchers::ActiveModel::ValidateInclusionMatcher

Inherits:
ValidationMatcher show all
Defined in:
lib/matchers/validate_inclusion_matcher.rb

Instance Method Summary collapse

Methods inherited from ValidationMatcher

#failure_message, #negative_failure_message, #on, #with_message

Methods included from Helpers

#class_of, #to_sentence

Constructor Details

#initialize(attr) ⇒ ValidateInclusionMatcher


12
13
14
# File 'lib/matchers/validate_inclusion_matcher.rb', line 12

def initialize attr
  super attr, :inclusion
end

Instance Method Details

#descriptionObject


45
46
47
48
49
50
51
# File 'lib/matchers/validate_inclusion_matcher.rb', line 45

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

#matches?(subject) ⇒ Boolean


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/matchers/validate_inclusion_matcher.rb', line 21

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

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

  result
end

#to_allow(*values) ⇒ Object


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

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