Class: Topographer::Importer::Mapper
- Inherits:
-
Object
- Object
- Topographer::Importer::Mapper
- 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
-
#bad_columns ⇒ Object
readonly
Returns the value of attribute bad_columns.
-
#default_values ⇒ Object
readonly
Returns the value of attribute default_values.
-
#ignored_mappings ⇒ Object
readonly
Returns the value of attribute ignored_mappings.
-
#key_fields ⇒ Object
readonly
Returns the value of attribute key_fields.
-
#missing_columns ⇒ Object
readonly
Returns the value of attribute missing_columns.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#optional_mappings ⇒ Object
readonly
Returns the value of attribute optional_mappings.
-
#required_mappings ⇒ Object
readonly
Returns the value of attribute required_mappings.
-
#validation_mappings ⇒ Object
readonly
Returns the value of attribute validation_mappings.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(mapper_builder, model_class) ⇒ Mapper
constructor
A new instance of Mapper.
- #input_structure_valid?(input_columns, options = {}) ⇒ Boolean
- #map_input(source_data) ⇒ Object
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_columns ⇒ Object (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_values ⇒ Object (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_mappings ⇒ Object (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_fields ⇒ Object (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_columns ⇒ Object (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_class ⇒ Object (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_mappings ⇒ Object (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_mappings ⇒ Object (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_mappings ⇒ Object (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
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
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, ={}) ignore_unmapped_columns = .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 |