Class: Shoulda::Matchers::ActiveModel::ValidationMatcher

Inherits:
Object
  • Object
show all
Includes:
Qualifiers::IgnoringInterferenceByWriter
Defined in:
lib/shoulda/matchers/active_model/validation_matcher.rb,
lib/shoulda/matchers/active_model/validation_matcher/build_description.rb

Defined Under Namespace

Classes: BuildDescription

Instance Attribute Summary

Attributes included from Qualifiers::IgnoringInterferenceByWriter

#ignore_interference_by_writer

Instance Method Summary collapse

Methods included from Qualifiers::IgnoringInterferenceByWriter

#ignoring_interference_by_writer

Constructor Details

#initialize(attribute) ⇒ ValidationMatcher

Returns a new instance of ValidationMatcher.



8
9
10
11
12
13
14
15
16
17
# File 'lib/shoulda/matchers/active_model/validation_matcher.rb', line 8

def initialize(attribute)
  super
  @attribute = attribute
  @options = {}
  @expects_strict = false
  @subject = nil
  @last_submatcher_run = nil
  @expected_message = nil
  @expects_custom_validation_message = false
end

Instance Method Details

#allow_blankObject



28
29
30
31
# File 'lib/shoulda/matchers/active_model/validation_matcher.rb', line 28

def allow_blank
  options[:allow_blank] = true
  self
end

#descriptionObject



19
20
21
# File 'lib/shoulda/matchers/active_model/validation_matcher.rb', line 19

def description
  ValidationMatcher::BuildDescription.call(self, simple_description)
end

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/shoulda/matchers/active_model/validation_matcher.rb', line 60

def does_not_match?(subject)
  @subject = subject
  true
end

#expects_custom_validation_message?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/shoulda/matchers/active_model/validation_matcher.rb', line 51

def expects_custom_validation_message?
  @expects_custom_validation_message
end

#expects_strict?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/shoulda/matchers/active_model/validation_matcher.rb', line 38

def expects_strict?
  @expects_strict
end

#failure_messageObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/shoulda/matchers/active_model/validation_matcher.rb', line 65

def failure_message
  overall_failure_message.dup.tap do |message|
    if failure_reason.present?
      message << "\n"
      message << Shoulda::Matchers.word_wrap(
        failure_reason,
        indent: 2,
      )
    end
  end
end

#failure_message_when_negatedObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/shoulda/matchers/active_model/validation_matcher.rb', line 77

def failure_message_when_negated
  overall_failure_message_when_negated.dup.tap do |message|
    if failure_reason.present?
      message << "\n"
      message << Shoulda::Matchers.word_wrap(
        failure_reason,
        indent: 2,
      )
    end
  end
end

#failure_reasonObject



89
90
91
# File 'lib/shoulda/matchers/active_model/validation_matcher.rb', line 89

def failure_reason
  last_submatcher_run.try(:failure_message)
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/shoulda/matchers/active_model/validation_matcher.rb', line 55

def matches?(subject)
  @subject = subject
  false
end

#on(context) ⇒ Object



23
24
25
26
# File 'lib/shoulda/matchers/active_model/validation_matcher.rb', line 23

def on(context)
  @context = context
  self
end

#strictObject



33
34
35
36
# File 'lib/shoulda/matchers/active_model/validation_matcher.rb', line 33

def strict
  @expects_strict = true
  self
end

#with_message(expected_message) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/shoulda/matchers/active_model/validation_matcher.rb', line 42

def with_message(expected_message)
  if expected_message
    @expects_custom_validation_message = true
    @expected_message = expected_message
  end

  self
end