class Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher
@private
Constants
- ARBITRARY_OUTSIDE_DECIMAL
- ARBITRARY_OUTSIDE_FIXNUM
- ARBITRARY_OUTSIDE_STRING
- 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 265 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 288 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 293 def allow_nil(allow_nil = true) @options[:allow_nil] = allow_nil self end
description()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 316 def description "ensure inclusion of #{@attribute} in #{inspect_message}" end
in_array(array)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 276 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 281 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 320 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
with_high_message(message)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 311 def with_high_message(message) @high_message = message if message self end
with_low_message(message)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 306 def with_low_message(message) @low_message = message if message self end
with_message(message)
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 298 def with_message(message) if message @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 374 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 353 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 392 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 388 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 362 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 462 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 470 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 454 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 438 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 476 def column_type_to_attribute_type(type) case type when :boolean, :decimal then type when :integer, :float then :fixnum else :default end end
disallows_higher_value()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 384 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 380 def disallows_lower_value @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 396 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 370 def inspect_message @range.nil? ? @array.inspect : @range.inspect end
matches_for_array?()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 346 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 339 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 425 def outside_values case attribute_type when :boolean boolean_outside_values when :fixnum [ARBITRARY_OUTSIDE_FIXNUM] when :decimal [ARBITRARY_OUTSIDE_DECIMAL] 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 484 def value_to_attribute_type(value) case value when true, false then :boolean when BigDecimal then :decimal when Fixnum then :fixnum else :default end end
values_outside_of_array()
click to toggle source
# File lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb, line 417 def values_outside_of_array if !(@array & outside_values).empty? raise CouldNotDetermineValueOutsideOfArray else outside_values end end