Class: NoBrainer::Matchers::Validations::ValidateInclusionOfMatcher

Inherits:
HaveValidationMatcher show all
Defined in:
lib/matchers/validations/inclusion_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(name) ⇒ ValidateInclusionOfMatcher

Returns a new instance of ValidateInclusionOfMatcher.



7
8
9
# File 'lib/matchers/validations/inclusion_of.rb', line 7

def initialize(name)
  super(name, :inclusion)
end

Instance Method Details

#descriptionObject



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

def description
  options_desc = []
  options_desc << " allowing these values: #{@allowed_values}" if @allowed_values
  "#{super}#{options_desc.to_sentence}"
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  if @allowed_values
    raw_validator_allowed_values = @validator.options[:in]

    validator_allowed_values = case raw_validator_allowed_values
                               when Range then raw_validator_allowed_values.to_a
                               when Proc then raw_validator_allowed_values.call(actual)
                               else raw_validator_allowed_values end

    not_allowed_values = @allowed_values - validator_allowed_values
    if not_allowed_values.empty?
      @positive_result_message = @positive_result_message += ' allowing all values mentioned'
    else
      @negative_result_message = @negative_result_message += " not allowing these values: #{not_allowed_values.inspect}"
      result = false
    end
  end

  result
end

#to_allow(*values) ⇒ Object



11
12
13
14
# File 'lib/matchers/validations/inclusion_of.rb', line 11

def to_allow(*values)
  @allowed_values = values.map { |v| v.respond_to?(:to_a) ? v.to_a : v }.flatten
  self
end