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
22
# 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']
  @preserve_blank_lines = @cell_mapping['preserve_blank_lines']
end

Instance Attribute Details

#capture ⇒ Object

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_mapping ⇒ Object

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

#join ⇒ Object

Returns the value of attribute join.



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

def join
  @join
end

#lines ⇒ Object

Returns the value of attribute lines.



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

def lines
  @lines
end

#name ⇒ Object

Returns the value of attribute name.



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

def name
  @name
end

#preserve_blank_lines ⇒ Object

Returns the value of attribute preserve_blank_lines.



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

def preserve_blank_lines
  @preserve_blank_lines
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. The captured value is String#striped by default.



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

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.nil? ? value : value.strip
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.



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

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

#validate_cell_mapping ⇒ Object



50
51
52
53
54
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 50

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_cell ⇒ Object



56
57
58
59
60
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 56

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_capture ⇒ Object



68
69
70
71
72
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 68

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_lines ⇒ Object



62
63
64
65
66
# File 'lib/ndr_import/non_tabular/column_mapping.rb', line 62

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