Class: Mongoid::Matchers::Validations::HaveValidationMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/matchers/validations.rb

Instance Method Summary collapse

Constructor Details

#initialize(field, validation_type) ⇒ HaveValidationMatcher

Returns a new instance of HaveValidationMatcher.



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

def initialize(field, validation_type)
  @field = field.to_s
  @type = validation_type.to_s
  @options = {}
end

Instance Method Details

#descriptionObject



42
43
44
45
46
47
48
# File 'lib/matchers/validations.rb', line 42

def description
  desc = "have #{@type.inspect} validator on #{@field.inspect}"
  desc << " on #{@options[:on]}" if @options[:on]
  desc << " with message #{@expected_message.inspect}" if @expected_message

  desc
end

#failure_message_for_shouldObject Also known as: failure_message



31
32
33
# File 'lib/matchers/validations.rb', line 31

def failure_message_for_should
  "Expected #{@klass.inspect} to #{description}; instead got #{@negative_result_message}"
end

#failure_message_for_should_notObject Also known as: failure_message_when_negated



35
36
37
# File 'lib/matchers/validations.rb', line 35

def failure_message_for_should_not
  "Expected #{@klass.inspect} to not #{description}; instead got #{@positive_result_message}"
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/matchers/validations.rb', line 11

def matches?(actual)
  @klass = actual.is_a?(Class) ? actual : actual.class

  @validator = @klass.validators_on(@field).detect do |v|
    (v.kind.to_s == @type) && (!v.options[:on] || on_options_matches?(v))
  end

  if @validator
    @negative_result_message = "#{@type.inspect} validator on #{@field.inspect}"
    @positive_result_message = "#{@type.inspect} validator on #{@field.inspect}"
  else
    @negative_result_message = "no #{@type.inspect} validator on #{@field.inspect}"
    return false
  end
  @result = true
  check_on if @options[:on]
  check_expected_message if @expected_message
  @result
end

#on(*on_method) ⇒ Object



50
51
52
53
# File 'lib/matchers/validations.rb', line 50

def on(*on_method)
  @options[:on] = on_method.flatten
  self
end

#with_message(message) ⇒ Object



55
56
57
58
# File 'lib/matchers/validations.rb', line 55

def with_message(message)
  @expected_message = message
  self
end