Class: Kafo::DataTypes::String

Inherits:
Kafo::DataType show all
Defined in:
lib/kafo/data_types/string.rb

Instance Method Summary collapse

Methods inherited from Kafo::DataType

#condition_value, #dump_default, #multivalued?, new_from_string, parse_hash, register_type, split_arguments, #typecast, types, unregister_type

Constructor Details

#initialize(min = :default, max = :default) ⇒ String

Returns a new instance of String.



4
5
6
7
# File 'lib/kafo/data_types/string.rb', line 4

def initialize(min = :default, max = :default)
  @min = (min.to_s == 'default') ? 0 : min.to_i
  @max = (max.to_s == 'default') ? :infinite : max.to_i
end

Instance Method Details

#to_sObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/kafo/data_types/string.rb', line 9

def to_s
  if @min > 0 && @max == :infinite
    "string (at least #{@min} characters)"
  elsif @min == 0 && @max != :infinite
    "string (up to #{@max} characters)"
  elsif @min > 0 && @max != :infinite
    "string (between #{@min} and #{@max} characters)"
  else
    "string"
  end
end

#valid?(input, errors = []) ⇒ Boolean

Returns:



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kafo/data_types/string.rb', line 21

def valid?(input, errors = [])
  unless input.is_a?(::String)
    errors << "#{input.inspect} is not a valid string"
    return false
  end

  errors << "#{input} must be at least #{@min}" if input.size < @min
  errors << "#{input} must be up to #{@max}" if @max != :infinite && input.size > @max

  return errors.empty?
end