Class: Topographer::Importer::Mapper::ValidationFieldMapping

Inherits:
FieldMapping
  • Object
show all
Defined in:
lib/topographer/importer/mapper/validation_field_mapping.rb

Instance Attribute Summary collapse

Attributes inherited from FieldMapping

#input_columns, #output_field

Instance Method Summary collapse

Constructor Details

#initialize(name, input_columns, &validation_block) ⇒ ValidationFieldMapping

Returns a new instance of ValidationFieldMapping.



4
5
6
7
8
9
10
11
12
# File 'lib/topographer/importer/mapper/validation_field_mapping.rb', line 4

def initialize(name, input_columns, &validation_block)
  unless block_given?
    raise Topographer::InvalidMappingError, 'Validation fields must have a behavior block'
  end
  @name = name
  @input_columns = Array(input_columns)
  @validation_block = validation_block
  @output_field = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'lib/topographer/importer/mapper/validation_field_mapping.rb', line 2

def name
  @name
end

Instance Method Details

#process_input(input, result) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/topographer/importer/mapper/validation_field_mapping.rb', line 14

def process_input(input, result)
  mapping_input = input.slice(*input_columns)
  @invalid_keys = get_invalid_keys(mapping_input)
  if @invalid_keys.blank?
    @validation_block.(mapping_input)
  else
    result.add_error(name, invalid_input_error)
  end

rescue => exception
  result.add_error(name, exception.message)

end

#required?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/topographer/importer/mapper/validation_field_mapping.rb', line 28

def required?
  true
end