Class: LIVR::Rules::String::LengthBetween

Inherits:
LIVR::Rule
  • Object
show all
Defined in:
lib/livr/rules/string.rb

Instance Method Summary collapse

Constructor Details

#initialize(min_length, max_length) ⇒ LengthBetween

Returns a new instance of LengthBetween.


106
107
108
109
# File 'lib/livr/rules/string.rb', line 106

def initialize(min_length, max_length)
  @min_length = min_length
  @max_length = max_length
end

Instance Method Details

#call(value, user_data, field_results) ⇒ Object


111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/livr/rules/string.rb', line 111

def call(value, user_data, field_results)
  return if value.in? [nil, ""]
  return 'FORMAT_ERROR' unless is_primitive(value)

  value = value.to_s
  return 'TOO_SHORT' if value.length < @min_length
  return 'TOO_LONG'  if value.length > @max_length

  field_results << value

  return
end