class NdrImport::NonTabular::ColumnMapping

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.

Attributes

capture[RW]
cell_mapping[RW]
join[RW]
lines[RW]
name[RW]
preserve_blank_lines[RW]

Public Class Methods

new(column_mapping) click to toggle source
# 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

Public Instance Methods

capture_value(line) click to toggle source

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#strip`ed by default.

# 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) click to toggle source

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.

# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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