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.



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

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

Instance Method Details

#check_onObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/matchers/validations.rb', line 51

def check_on
  validator_on_methods = [@validator.options[:on]].flatten

  if validator_on_methods.any?
    message = " on methods: #{validator_on_methods}"

    if on_options_covered_by?( @validator )
      @positive_result_message << message
    else
      @negative_result_message << message
      @result = false
    end
  end
end

#descriptionObject



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

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

#failure_message_for_shouldObject



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

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

#failure_message_for_should_notObject



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

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

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  @validator = @klass.validators_on(@field).detect{ |v|
    v.kind.to_s == @type and (!v.options[:on] or on_options_matches?(v))
  }

  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]
  @result
end

#on(*on_method) ⇒ Object



46
47
48
49
# File 'lib/matchers/validations.rb', line 46

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