class Shoulda::Matchers::ActiveModel::EnsureLengthOfMatcher
@private
Public Class Methods
new(attribute)
click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 194 def initialize(attribute) super(attribute) @options = {} end
Public Instance Methods
description()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 241 def description description = "ensure #{@attribute} has a length " if @options.key?(:minimum) && @options.key?(:maximum) if @options[:minimum] == @options[:maximum] description << "of exactly #{@options[:minimum]}" else description << "between #{@options[:minimum]} and #{@options[:maximum]}" end else description << "of at least #{@options[:minimum]}" if @options[:minimum] description << "of at most #{@options[:maximum]}" if @options[:maximum] end description end
is_at_least(length)
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 199 def is_at_least(length) @options[:minimum] = length @short_message ||= :too_short self end
is_at_most(length)
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 205 def is_at_most(length) @options[:maximum] = length @long_message ||= :too_long self end
is_equal_to(length)
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 211 def is_equal_to(length) @options[:minimum] = length @options[:maximum] = length @short_message ||= :wrong_length @long_message ||= :wrong_length self end
matches?(subject)
click to toggle source
Calls superclass method
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 256 def matches?(subject) super(subject) translate_messages! lower_bound_matches? && upper_bound_matches? end
with_long_message(message)
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 226 def with_long_message(message) if message @long_message = message end self end
with_message(message)
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 233 def with_message(message) if message @short_message = message @long_message = message end self end
with_short_message(message)
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 219 def with_short_message(message) if message @short_message = message end self end
Private Instance Methods
allows_length_of?(length, message)
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 323 def allows_length_of?(length, message) allows_value_of(string_of_length(length), message) end
allows_maximum_length?()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 315 def allows_maximum_length? if @options.key?(:maximum) allows_length_of?(@options[:maximum], @long_message) else true end end
allows_minimum_length?()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 307 def allows_minimum_length? if @options.key?(:minimum) allows_length_of?(@options[:minimum], @short_message) else true end end
disallows_higher_length?()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 299 def disallows_higher_length? if @options.key?(:maximum) disallows_length_of?(@options[:maximum] + 1, @long_message) else true end end
disallows_length_of?(length, message)
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 327 def disallows_length_of?(length, message) disallows_value_of(string_of_length(length), message) end
disallows_lower_length?()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 290 def disallows_lower_length? if @options.key?(:minimum) @options[:minimum] == 0 || disallows_length_of?(@options[:minimum] - 1, @short_message) else true end end
lower_bound_matches?()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 282 def lower_bound_matches? disallows_lower_length? && allows_minimum_length? end
string_of_length(length)
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 331 def string_of_length(length) 'x' * length end
translate_messages!()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 264 def translate_messages! if Symbol === @short_message @short_message = default_error_message(@short_message, model_name: @subject.class.to_s.underscore, instance: @subject, attribute: @attribute, count: @options[:minimum]) end if Symbol === @long_message @long_message = default_error_message(@long_message, model_name: @subject.class.to_s.underscore, instance: @subject, attribute: @attribute, count: @options[:maximum]) end end
upper_bound_matches?()
click to toggle source
# File lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb, line 286 def upper_bound_matches? disallows_higher_length? && allows_maximum_length? end