Class: Tabledata::ColumnDefinition::StringProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/tabledata/column_definition.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ StringProcessor


20
21
22
23
24
25
26
27
28
29
# File 'lib/tabledata/column_definition.rb', line 20

def initialize(options)
  options      = options.dup
  length_range = options.delete(:length)
  length_range = length_range..length_range if length_range.is_a?(Integer)
  @min_length  = options.delete(:min_length) || (length_range && length_range.begin) || nil
  @max_length  = options.delete(:max_length) || (length_range && length_range.end) || nil
  @pattern     = options.delete(:pattern)

  InvalidOptions.verify!(__method__, options, [:size, :min_length, :max_length, :pattern])
end

Instance Method Details

#call(value, errors) ⇒ Object


31
32
33
34
35
36
37
38
# File 'lib/tabledata/column_definition.rb', line 31

def call(value, errors)
  value = value.to_s unless value.is_a?(String)
  errors << [:too_short, {min_length: @min_length, actual: value.length}] if @min_length && value.length < @min_length
  errors << [:too_long, {max_length: @max_length, actual: value.length}] if @max_length && value.length > @max_length
  errors << [:invalid_format] if @pattern && value !~ @pattern

  value
end