class Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher
@private
Constants
- ARBITRARY_OUTSIDE_DATE
- ARBITRARY_OUTSIDE_DATETIME
- ARBITRARY_OUTSIDE_DECIMAL
- ARBITRARY_OUTSIDE_INTEGER
- ARBITRARY_OUTSIDE_STRING
- ARBITRARY_OUTSIDE_TIME
- BLANK_VALUES
- BOOLEAN_ALLOWS_BOOLEAN_MESSAGE
- BOOLEAN_ALLOWS_NIL_MESSAGE
Public Class Methods
new(attribute)
click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 294 def initialize(attribute) super(attribute) @options = {} @array = nil @range = nil @minimum = nil @maximum = nil @low_message = :inclusion @high_message = :inclusion end
Public Instance Methods
allow_nil()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 317 def allow_nil @options[:allow_nil] = true self end
does_not_match?(subject)
click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 386 def does_not_match?(subject) super(subject) if @range does_not_match_for_range? elsif @array if does_not_match_for_array? true else @failure_message = "#{@array} matches array in validation" false end end end
expects_to_allow_nil?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 322 def expects_to_allow_nil? @options[:allow_nil] end
in_array(array)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 305 def in_array(array) @array = array self end
in_range(range)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 310 def in_range(range) @range = range @minimum = minimum_range_value @maximum = maximum_range_value self end
matches?(subject)
click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 371 def matches?(subject) super(subject) if @range matches_for_range? elsif @array if matches_for_array? true else @failure_message = "#{@array} doesn't match array in validation" false end end end
simple_description()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 353 def simple_description if @range "validate that :#{@attribute} lies inside the range " + Shoulda::Matchers::Util.inspect_range(@range) else description = "validate that :#{@attribute}" description << if @array.count > 1 " is either #{inspected_array}" else " is #{inspected_array}" end description end end
with_high_message(message)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 345 def with_high_message(message) if message @high_message = message end self end
with_low_message(message)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 336 def with_low_message(message) if message @expects_custom_validation_message = true @low_message = message end self end
with_message(message)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 326 def with_message(message) if message @expects_custom_validation_message = true @low_message = message @high_message = message end self end
Private Instance Methods
allows_all_values_in_array?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 479 def allows_all_values_in_array? @array.all? do |value| allows_value_of(value, @low_message) end end
allows_any_value_outside_of_array?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 491 def allows_any_value_outside_of_array? if attribute_type == :boolean case @array when [false, true], [true, false] Shoulda::Matchers.warn BOOLEAN_ALLOWS_BOOLEAN_MESSAGE return true when [nil] if attribute_column.null Shoulda::Matchers.warn BOOLEAN_ALLOWS_NIL_MESSAGE return true else raise NonNullableBooleanError.create(@attribute) end end end values_outside_of_array.any? do |value| allows_value_of(value, @low_message) end end
allows_higher_value()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 471 def allows_higher_value @maximum.nil? || allows_value_of(@maximum + 1, @high_message) end
allows_lower_value()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 443 def allows_lower_value @minimum && @minimum != 0 && allows_value_of(@minimum - 1, @low_message) end
allows_maximum_value()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 463 def allows_maximum_value @maximum.nil? || allows_value_of(@maximum, @high_message) end
allows_minimum_value()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 455 def allows_minimum_value @minimum.nil? || allows_value_of(@minimum, @low_message) end
allows_nil_value?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 618 def allows_nil_value? @options[:allow_nil] != true || allows_value_of(nil) end
attribute_allows_nil?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 584 def attribute_allows_nil? if attribute_column attribute_column.null else true end end
attribute_column()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 592 def attribute_column if @subject.class.respond_to?(:columns_hash) @subject.class.columns_hash[@attribute.to_s] end end
attribute_type()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 576 def attribute_type if attribute_column column_type_to_attribute_type(attribute_column.type) else value_to_attribute_type(@subject.__send__(@attribute)) end end
boolean_outside_values()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 560 def boolean_outside_values values = [] values << case @array when [true] then false when [false] then true else raise CouldNotDetermineValueOutsideOfArray end if attribute_allows_nil? values << nil end values end
column_type_to_attribute_type(type)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 598 def column_type_to_attribute_type(type) case type when :float then :integer when :timestamp then :datetime else type end end
disallows_all_values_outside_of_array?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 512 def disallows_all_values_outside_of_array? if attribute_type == :boolean case @array when [false, true], [true, false] Shoulda::Matchers.warn BOOLEAN_ALLOWS_BOOLEAN_MESSAGE return true when [nil] if attribute_column.null Shoulda::Matchers.warn BOOLEAN_ALLOWS_NIL_MESSAGE return true else raise NonNullableBooleanError.create(@attribute) end end end values_outside_of_array.all? do |value| disallows_value_of(value, @low_message) end end
disallows_any_values_in_array?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 485 def disallows_any_values_in_array? @array.any? do |value| disallows_value_of(value, @low_message) end end
disallows_higher_value()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 475 def disallows_higher_value @maximum.nil? || disallows_value_of(@maximum + 1, @high_message) end
disallows_lower_value()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 449 def disallows_lower_value @minimum.nil? || @minimum == 0 || disallows_value_of(@minimum - 1, @low_message) end
disallows_maximum_value()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 467 def disallows_maximum_value @maximum.nil? || disallows_value_of(@maximum, @high_message) end
disallows_minimum_value()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 459 def disallows_minimum_value @minimum.nil? || disallows_value_of(@minimum, @low_message) end
disallows_nil_value?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 622 def disallows_nil_value? @options[:allow_nil] && disallows_value_of(nil) end
does_not_match_for_array?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 436 def does_not_match_for_array? disallows_any_values_in_array? || allows_any_value_outside_of_array? || disallows_nil_value? || allow_blank_does_not_match? end
does_not_match_for_range?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 422 def does_not_match_for_range? allows_lower_value || disallows_minimum_value || disallows_maximum_value || allows_higher_value end
inspected_array()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 626 def inspected_array Shoulda::Matchers::Util.inspect_values(@array).to_sentence( two_words_connector: ' or ', last_word_connector: ', or ', ) end
matches_for_array?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 429 def matches_for_array? allows_all_values_in_array? && disallows_all_values_outside_of_array? && allows_nil_value? && allow_blank_matches? end
matches_for_range?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 415 def matches_for_range? disallows_lower_value && allows_minimum_value && allows_maximum_value && disallows_higher_value end
maximum_range_value()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 407 def maximum_range_value if @range.exclude_end? @range.end ? (@range.end - 1) : nil else @range.end end end
minimum_range_value()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 403 def minimum_range_value @range.begin end
outside_values()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 541 def outside_values case attribute_type when :boolean boolean_outside_values when :integer [ARBITRARY_OUTSIDE_INTEGER] when :decimal [ARBITRARY_OUTSIDE_DECIMAL] when :date [ARBITRARY_OUTSIDE_DATE] when :datetime [ARBITRARY_OUTSIDE_DATETIME] when :time [ARBITRARY_OUTSIDE_TIME] else [ARBITRARY_OUTSIDE_STRING] end end
value_to_attribute_type(value)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 606 def value_to_attribute_type(value) case value when true, false then :boolean when BigDecimal then :decimal when Integer then :integer when Date then :date when DateTime then :datetime when Time then :time else :unknown end end
values_outside_of_array()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 533 def values_outside_of_array if !(@array & outside_values).empty? raise CouldNotDetermineValueOutsideOfArray else outside_values end end