class StrongerParameters::StringConstraint

Attributes

maximum_length[R]
minimum_length[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/stronger_parameters/constraints/string_constraint.rb, line 8
def initialize(options = {})
  @maximum_length = options[:maximum_length] || options[:max_length]
  @minimum_length = options[:minimum_length] || options[:min_length]
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method StrongerParameters::Constraint#==
# File lib/stronger_parameters/constraints/string_constraint.rb, line 29
def ==(other)
  super && maximum_length == other.maximum_length && minimum_length == other.minimum_length
end
value(v) click to toggle source
# File lib/stronger_parameters/constraints/string_constraint.rb, line 13
def value(v)
  if v.is_a?(String)
    if maximum_length && v.bytesize > maximum_length
      return InvalidValue.new(v, "can not be longer than #{maximum_length} bytes")
    elsif minimum_length && v.bytesize < minimum_length
      return InvalidValue.new(v, "can not be shorter than #{minimum_length} bytes")
    elsif !v.valid_encoding?
      return InvalidValue.new(v, 'must have valid encoding')
    end

    return v
  end

  InvalidValue.new(v, 'must be a string')
end