Class: StrongerParameters::StringConstraint
- Inherits:
-
Constraint
- Object
- Constraint
- StrongerParameters::StringConstraint
- Defined in:
- lib/stronger_parameters/constraints/string_constraint.rb
Instance Attribute Summary collapse
-
#maximum_length ⇒ Object
readonly
Returns the value of attribute maximum_length.
-
#minimum_length ⇒ Object
readonly
Returns the value of attribute minimum_length.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(options = {}) ⇒ StringConstraint
constructor
A new instance of StringConstraint.
- #value(v) ⇒ Object
Methods inherited from Constraint
#&, #required, #required?, #|
Constructor Details
#initialize(options = {}) ⇒ StringConstraint
Returns a new instance of StringConstraint.
9 10 11 12 |
# File 'lib/stronger_parameters/constraints/string_constraint.rb', line 9 def initialize( = {}) @maximum_length = [:maximum_length] || [:max_length] @minimum_length = [:minimum_length] || [:min_length] end |
Instance Attribute Details
#maximum_length ⇒ Object (readonly)
Returns the value of attribute maximum_length.
7 8 9 |
# File 'lib/stronger_parameters/constraints/string_constraint.rb', line 7 def maximum_length @maximum_length end |
#minimum_length ⇒ Object (readonly)
Returns the value of attribute minimum_length.
7 8 9 |
# File 'lib/stronger_parameters/constraints/string_constraint.rb', line 7 def minimum_length @minimum_length end |
Instance Method Details
#==(other) ⇒ Object
30 31 32 |
# File 'lib/stronger_parameters/constraints/string_constraint.rb', line 30 def ==(other) super && maximum_length == other.maximum_length && minimum_length == other.minimum_length end |
#value(v) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/stronger_parameters/constraints/string_constraint.rb', line 14 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 |