class Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher
@private
Constants
- ARBITRARY_OUTSIDE_DECIMAL
- ARBITRARY_OUTSIDE_FIXNUM
- ARBITRARY_OUTSIDE_STRING
- BOOLEAN_ALLOWS_BOOLEAN_MESSAGE
- BOOLEAN_ALLOWS_NIL_MESSAGE
- BOOLEAN_ALLOWS_NIL_WITH_NOT_NULL_MESSAGE
Public Class Methods
new(attribute)
click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb, line 240 def initialize(attribute) super(attribute) @options = {} end
Public Instance Methods
allow_blank(allow_blank = true)
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb, line 257 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/ensure_inclusion_of_matcher.rb, line 262 def allow_nil(allow_nil = true) @options[:allow_nil] = allow_nil self end
description()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb, line 285 def description "ensure inclusion of #{@attribute} in #{inspect_message}" end
in_array(array)
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb, line 245 def in_array(array) @array = array self end
in_range(range)
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb, line 250 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/ensure_inclusion_of_matcher.rb, line 289 def matches?(subject) super(subject) if @range @low_message ||= :inclusion @high_message ||= :inclusion disallows_lower_value && allows_minimum_value && disallows_higher_value && allows_maximum_value elsif @array if allows_all_values_in_array? && allows_blank_value? && allows_nil_value? && disallows_value_outside_of_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/ensure_inclusion_of_matcher.rb, line 280 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/ensure_inclusion_of_matcher.rb, line 275 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/ensure_inclusion_of_matcher.rb, line 267 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/ensure_inclusion_of_matcher.rb, line 333 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/ensure_inclusion_of_matcher.rb, line 312 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/ensure_inclusion_of_matcher.rb, line 351 def allows_maximum_value allows_value_of(@maximum, @high_message) end
allows_minimum_value()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb, line 347 def allows_minimum_value allows_value_of(@minimum, @low_message) end
allows_nil_value?()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb, line 321 def allows_nil_value? if @options.key?(:allow_nil) @options[:allow_nil] == allows_value_of(nil) else true end end
attribute_column()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb, line 411 def attribute_column @subject.class.columns_hash[@attribute.to_s] end
boolean_outside_values()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb, line 395 def boolean_outside_values values = [] values << case @array when [true] then false when [false] then true else raise CouldNotDetermineValueOutsideOfArray end if attribute_column.null values << nil end values end
disallows_higher_value()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb, line 343 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/ensure_inclusion_of_matcher.rb, line 339 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/ensure_inclusion_of_matcher.rb, line 355 def disallows_value_outside_of_array? if attribute_column.type == :boolean case @array when [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, BOOLEAN_ALLOWS_NIL_WITH_NOT_NULL_MESSAGE end end end !allows_value_of(*values_outside_of_array) end
inspect_message()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb, line 329 def inspect_message @range.nil? ? @array.inspect : @range.inspect end
outside_values()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb, line 382 def outside_values case attribute_column.type when :boolean boolean_outside_values when :integer, :float [ARBITRARY_OUTSIDE_FIXNUM] when :decimal [ARBITRARY_OUTSIDE_DECIMAL] else [ARBITRARY_OUTSIDE_STRING] end end
values_outside_of_array()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb, line 374 def values_outside_of_array if !(@array & outside_values).empty? raise CouldNotDetermineValueOutsideOfArray else outside_values end end