Class: Tabledata::ColumnDefinition

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

Defined Under Namespace

Classes: BinaryProcessor, BooleanProcessor, DateProcessor, DateTimeProcessor, FloatProcessor, IntegerProcessor, StringProcessor

Constant Summary collapse

Processors =
{
  string:   StringProcessor,
  integer:  IntegerProcessor,
  float:    FloatProcessor,
  datetime: DateTimeProcessor,
  date:     DateProcessor,
  boolean:  BooleanProcessor,
  binary:   BinaryProcessor,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, accessor, header, type, allow_nil, default, strip, empty_string_is_nil, pre_validator, adaptor, validator, options) ⇒ ColumnDefinition


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/tabledata/column_definition.rb', line 188

def initialize(index, accessor, header, type, allow_nil, default, strip, empty_string_is_nil, pre_validator, adaptor, validator, options)
  @index               = index
  @accessor            = accessor
  @header              = header
  @type                = type
  @allow_nil           = allow_nil
  @default             = default
  @strip               = strip
  @empty_string_is_nil = empty_string_is_nil
  @pre_validator       = pre_validator
  @adaptor             = adaptor
  @validator           = validator
  @options             = options
  @processor           = Processors.fetch(type).new(options)
end

Instance Attribute Details

#accessorObject (readonly)

Returns the value of attribute accessor


8
9
10
# File 'lib/tabledata/column_definition.rb', line 8

def accessor
  @accessor
end

#adaptorObject (readonly)

Returns the value of attribute adaptor


15
16
17
# File 'lib/tabledata/column_definition.rb', line 15

def adaptor
  @adaptor
end

#allow_nilObject (readonly)

Returns the value of attribute allow_nil


11
12
13
# File 'lib/tabledata/column_definition.rb', line 11

def allow_nil
  @allow_nil
end

#defaultObject (readonly)

Returns the value of attribute default


12
13
14
# File 'lib/tabledata/column_definition.rb', line 12

def default
  @default
end

#headerObject (readonly)

Returns the value of attribute header


9
10
11
# File 'lib/tabledata/column_definition.rb', line 9

def header
  @header
end

#indexObject (readonly)

Returns the value of attribute index


7
8
9
# File 'lib/tabledata/column_definition.rb', line 7

def index
  @index
end

#optionsObject (readonly)

Returns the value of attribute options


17
18
19
# File 'lib/tabledata/column_definition.rb', line 17

def options
  @options
end

#pre_validatorObject (readonly)

Returns the value of attribute pre_validator


14
15
16
# File 'lib/tabledata/column_definition.rb', line 14

def pre_validator
  @pre_validator
end

#stripObject (readonly)

Returns the value of attribute strip


13
14
15
# File 'lib/tabledata/column_definition.rb', line 13

def strip
  @strip
end

#typeObject (readonly)

Returns the value of attribute type


10
11
12
# File 'lib/tabledata/column_definition.rb', line 10

def type
  @type
end

#validatorObject (readonly)

Returns the value of attribute validator


16
17
18
# File 'lib/tabledata/column_definition.rb', line 16

def validator
  @validator
end

Instance Method Details

#coerce(value) ⇒ Object


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/tabledata/column_definition.rb', line 204

def coerce(value)
  errors  = []
  adapted = nil

  if @pre_validator.nil? || @pre_validator.call(value)
    begin
      value   = value.strip if @strip && value.is_a?(String)
      value   = nil if @empty_string_is_nil && value.is_a?(String) && value.empty?
      adapted = @adaptor ? @adaptor.call(value) : value
      adapted = @default if adapted.nil?
    rescue => exception
      errors << [:exception, exception]
    else
      adapted = @processor.call(adapted, errors) if @processor && !adapted.nil?
      errors << [:invalid_value] if @validator && !adapted.nil? && !@validator.call(adapted)
      errors << [:invalid_nil_value] if adapted.nil? && !@allow_nil
    end
  else
    errors << [:invalid_input]
  end

  [adapted, errors]
end