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
- 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 290 def initialize(attribute) super(attribute) @options = {} @array = nil @range = nil @minimum = nil @maximum = nil @low_message = nil @high_message = nil end
Public Instance Methods
allow_blank(allow_blank = true)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 313 def allow_blank(allow_blank = true) @options[:allow_blank] = allow_blank self end
allow_nil(allow_nil = true)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 322 def allow_nil(allow_nil = true) @options[:allow_nil] = allow_nil self end
expects_to_allow_blank?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 318 def expects_to_allow_blank? @options[:allow_blank] end
expects_to_allow_nil?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 327 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 301 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 306 def in_range(range) @range = range @minimum = range.first @maximum = range.max self end
matches?(subject)
click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 375 def matches?(subject) super(subject) if @range @low_message ||= :inclusion @high_message ||= :inclusion 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 358 def simple_description if @range "validate that :#{@attribute} lies inside the range " + Shoulda::Matchers::Util.inspect_range(@range) else description = "validate that :#{@attribute}" if @array.many? description << " is either #{inspected_array}" else description << " 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 350 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 341 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 331 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 429 def allows_all_values_in_array? @array.all? do |value| allows_value_of(value, @low_message) end end
allows_blank_value?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 408 def allows_blank_value? if @options.key?(:allow_blank) blank_values = ['', ' ', "\n", "\r", "\t", "\f"] @options[:allow_blank] == blank_values.all? { |value| allows_value_of(value) } else true end end
allows_maximum_value()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 449 def allows_maximum_value 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 445 def allows_minimum_value 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 417 def allows_nil_value? if @options.key?(:allow_nil) @options[:allow_nil] == allows_value_of(nil) else true end end
attribute_allows_nil?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 525 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 533 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 517 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 501 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 539 def column_type_to_attribute_type(type) case type when :float then :integer when :timestamp then :datetime else type end end
disallows_higher_value()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 441 def disallows_higher_value 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 435 def disallows_lower_value @minimum.nil? || @minimum == 0 || disallows_value_of(@minimum - 1, @low_message) end
disallows_value_outside_of_array?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 453 def disallows_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
inspect_message()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 425 def inspect_message @range.nil? ? @array.inspect : @range.inspect end
inspected_array()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 559 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 401 def matches_for_array? allows_all_values_in_array? && allows_blank_value? && allows_nil_value? && disallows_value_outside_of_array? end
matches_for_range?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 394 def matches_for_range? disallows_lower_value && allows_minimum_value && disallows_higher_value && allows_maximum_value end
outside_values()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 482 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 547 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 474 def values_outside_of_array if !(@array & outside_values).empty? raise CouldNotDetermineValueOutsideOfArray else outside_values end end