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

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

Instance Method Summary collapse

Methods included from Helpers

#class_of, #to_sentence

Constructor Details

#initialize(field, validation_type) ⇒ HaveValidationMatcher

Returns a new instance of HaveValidationMatcher.



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

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

Instance Method Details

#descriptionObject



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

def description
  desc = "validate #{@type.inspect} of #{@field.inspect}"
  desc << " with message: #{@expected_message.inspect}" if @expected_message
  desc << " on #{@expected_on.empty? ? 'all actions' : to_sentence(@expected_on)}" if @expected_on

  desc
end

#failure_messageObject



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

def failure_message
  "#{@klass} to #{description}; instead got #{@negative_message}"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/matchers/validations/validations.rb', line 20

def matches?(subject)
  @klass     = class_of(subject)
  @validator = detect_validator
  @result    = true

  check_validator
  check_message if @expected_message
  check_on if @expected_on

  @result
end

#negative_failure_messageObject



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

def negative_failure_message
  "#{@klass} to not #{description}; instead got #{@positive_message}"
end

#on(*contexts) ⇒ Object



15
16
17
18
# File 'lib/matchers/validations/validations.rb', line 15

def on(*contexts)
  @expected_on = clean_contexts(contexts)
  self
end

#with_message(expected_message) ⇒ Object



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

def with_message(expected_message)
  @expected_message = expected_message
  self
end