Class: Shoulda::Matchers::ActiveModel::ValidateExclusionOfMatcher

Inherits:
ValidationMatcher
  • Object
show all
Defined in:
lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb

Instance Attribute Summary

Attributes included from Qualifiers::IgnoringInterferenceByWriter

#ignore_interference_by_writer

Instance Method Summary collapse

Methods inherited from ValidationMatcher

#allow_blank, #description, #expects_custom_validation_message?, #expects_strict?, #failure_message, #failure_message_when_negated, #on, #strict, #with_message

Methods included from Qualifiers::IgnoringInterferenceByWriter

#ignoring_interference_by_writer

Constructor Details

#initialize(attribute) ⇒ ValidateExclusionOfMatcher

Returns a new instance of ValidateExclusionOfMatcher.



123
124
125
126
127
128
# File 'lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb', line 123

def initialize(attribute)
  super(attribute)
  @expected_message = :exclusion
  @array = nil
  @range = nil
end

Instance Method Details

#does_not_match?(subject) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb', line 173

def does_not_match?(subject)
  super(subject)

  if @range
    disallows_lower_value ||
      allows_minimum_value ||
      allows_maximum_value ||
      disallows_higher_value
  elsif @array
    allows_any_values_in_array?
  end
end

#in_array(array) ⇒ Object



130
131
132
133
# File 'lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb', line 130

def in_array(array)
  @array = array
  self
end

#in_range(range) ⇒ Object



135
136
137
138
139
140
# File 'lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb', line 135

def in_range(range)
  @range = range
  @minimum = range.first
  @maximum = range.max
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb', line 160

def matches?(subject)
  super(subject)

  if @range
    allows_lower_value &&
      disallows_minimum_value &&
      disallows_maximum_value &&
      allows_higher_value
  elsif @array
    disallows_all_values_in_array?
  end
end

#simple_descriptionObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb', line 142

def simple_description
  if @range
    "validate that :#{@attribute} lies outside the range " +
      Shoulda::Matchers::Util.inspect_range(@range)
  else
    description = "validate that :#{@attribute}"

    description <<
      if @array.many?
        " is neither #{inspected_array}"
      else
        " is not #{inspected_array}"
      end

    description
  end
end