Class: NdrImport::NonTabular::ColumnMapping

Inherits:
Object
  • Object
show all
Defined in:
lib/ndr_import/non_tabular/column_mapping.rb

Overview

This class stores the mapping for an individual non-tabular column, encapsulating the logic associated with finding matching lines of source data and subsequently capturing arrays of values within them.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column_mapping) ⇒ ColumnMapping

Returns a new instance of ColumnMapping.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 11

def initialize(column_mapping)
  @name         = column_mapping['rawtext_name'] ||
                  column_mapping['column'] ||
                  column_mapping['standard_mapping']
  @cell_mapping = column_mapping['non_tabular_cell']

  validate_cell_mapping

  @lines = @cell_mapping['lines']
  @join  = @cell_mapping['join']
end

Instance Attribute Details

#captureObject

Returns the value of attribute capture.



9
10
11
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 9

def capture
  @capture
end

#cell_mappingObject

Returns the value of attribute cell_mapping.



9
10
11
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 9

def cell_mapping
  @cell_mapping
end

#joinObject

Returns the value of attribute join.



9
10
11
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 9

def join
  @join
end

#linesObject

Returns the value of attribute lines.



9
10
11
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 9

def lines
  @lines
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 9

def name
  @name
end

Instance Method Details

#capture_value(line) ⇒ Object

capture the required part of the line by replacing (recusively) the line, with the first captured regular expression group. This is hardcoded in an attempt to preserve the rawtext as much as possible



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 36

def capture_value(line)
  value = line.dup
  [@cell_mapping['capture']].flatten.each do |pattern|
    if matchdata = value.to_s.match(pattern)
      value = matchdata[1]
    else
      value = nil
    end
  end
  value
end

#matching_lines(text) ⇒ Object

This method returns the range of matching source data lines. If the range is a RegexpRange then it will calculate it for the text provided.



25
26
27
28
29
30
31
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 25

def matching_lines(text)
  if @lines.is_a?(RegexpRange)
    @lines.to_range(text)
  else
    @lines
  end
end

#validate_cell_mappingObject



48
49
50
51
52
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 48

def validate_cell_mapping
  validate_presence_of_non_tabular_cell
  validate_presence_of_non_tabular_cell_lines
  validate_presence_of_non_tabular_cell_capture
end

#validate_presence_of_non_tabular_cellObject



54
55
56
57
58
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 54

def validate_presence_of_non_tabular_cell
  return if @cell_mapping
  fail NdrImport::MappingError,
       I18n.t('mapping.errors.missing_non_tabular_cell', :name => @name)
end

#validate_presence_of_non_tabular_cell_captureObject



66
67
68
69
70
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 66

def validate_presence_of_non_tabular_cell_capture
  return if @cell_mapping['capture']
  fail NdrImport::MappingError,
       I18n.t('mapping.errors.missing_non_tabular_cell_capture', :name => @name)
end

#validate_presence_of_non_tabular_cell_linesObject



60
61
62
63
64
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 60

def validate_presence_of_non_tabular_cell_lines
  return if @cell_mapping['lines']
  fail NdrImport::MappingError,
       I18n.t('mapping.errors.missing_non_tabular_cell_lines', :name => @name)
end