Class: Topographer::Importer::Mapper

Inherits:
Object
  • Object
show all
Includes:
MappingColumns
Defined in:
lib/topographer/importer/mapper.rb

Defined Under Namespace

Modules: MappingColumns, MappingValidator Classes: DefaultFieldMapping, FieldMapping, IgnoredFieldMapping, MapperBuilder, Result, ValidationFieldMapping

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MappingColumns

#default_fields, #ignored_mapping_columns, #input_columns, #optional_mapping_columns, #output_fields, #required_input_columns, #required_mapping_columns, #validation_mapping_columns

Constructor Details

#initialize(mapper_builder, model_class) ⇒ Mapper

Returns a new instance of Mapper.



23
24
25
26
27
28
29
30
31
# File 'lib/topographer/importer/mapper.rb', line 23

def initialize(mapper_builder, model_class)
  @required_mappings = mapper_builder.required_mappings
  @optional_mappings = mapper_builder.optional_mappings
  @ignored_mappings = mapper_builder.ignored_mappings
  @validation_mappings = mapper_builder.validation_mappings
  @default_values = mapper_builder.default_values
  @key_fields = mapper_builder.key_fields
  @model_class = model_class
end

Instance Attribute Details

#bad_columnsObject (readonly)

Returns the value of attribute bad_columns.



13
14
15
# File 'lib/topographer/importer/mapper.rb', line 13

def bad_columns
  @bad_columns
end

#default_valuesObject (readonly)

Returns the value of attribute default_values.



13
14
15
# File 'lib/topographer/importer/mapper.rb', line 13

def default_values
  @default_values
end

#ignored_mappingsObject (readonly)

Returns the value of attribute ignored_mappings.



13
14
15
# File 'lib/topographer/importer/mapper.rb', line 13

def ignored_mappings
  @ignored_mappings
end

#key_fieldsObject (readonly)

Returns the value of attribute key_fields.



13
14
15
# File 'lib/topographer/importer/mapper.rb', line 13

def key_fields
  @key_fields
end

#missing_columnsObject (readonly)

Returns the value of attribute missing_columns.



13
14
15
# File 'lib/topographer/importer/mapper.rb', line 13

def missing_columns
  @missing_columns
end

#model_classObject (readonly)

Returns the value of attribute model_class.



13
14
15
# File 'lib/topographer/importer/mapper.rb', line 13

def model_class
  @model_class
end

#optional_mappingsObject (readonly)

Returns the value of attribute optional_mappings.



13
14
15
# File 'lib/topographer/importer/mapper.rb', line 13

def optional_mappings
  @optional_mappings
end

#required_mappingsObject (readonly)

Returns the value of attribute required_mappings.



13
14
15
# File 'lib/topographer/importer/mapper.rb', line 13

def required_mappings
  @required_mappings
end

#validation_mappingsObject (readonly)

Returns the value of attribute validation_mappings.



13
14
15
# File 'lib/topographer/importer/mapper.rb', line 13

def validation_mappings
  @validation_mappings
end

Class Method Details

.build_mapper(model_class) {|mapper_builder| ... } ⇒ Object

Yields:

  • (mapper_builder)


16
17
18
19
20
21
# File 'lib/topographer/importer/mapper.rb', line 16

def self.build_mapper(model_class)
  mapper_builder = MapperBuilder.new()
  yield mapper_builder

  new(mapper_builder, model_class)
end

Instance Method Details

#input_structure_valid?(input_columns, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/topographer/importer/mapper.rb', line 33

def input_structure_valid?(input_columns, options={})
  ignore_unmapped_columns = options.fetch(:ignore_unmapped_columns, false)
  @bad_columns ||= input_columns - mapped_input_columns
  @missing_columns ||= required_input_columns - input_columns

  if ignore_unmapped_columns
    @missing_columns.empty?
  else
    @bad_columns.empty? && @missing_columns.empty?
  end
end

#map_input(source_data) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/topographer/importer/mapper.rb', line 45

def map_input(source_data)
  mapping_result = Result.new(source_data.source_identifier)

  if source_data.empty?
    handle_no_data(mapping_result)
  else
    @validation_mappings.values.each do |validation_field_mapping|
      validation_field_mapping.process_input(source_data.data, mapping_result)
    end

    output_fields.each do |output_field|
      field_mapping = mappings[output_field]
      field_mapping.process_input(source_data.data, mapping_result)
    end
  end

  mapping_result
end