Class: NdrImport::Table

Inherits:
Object
  • Object
show all
Includes:
Mapper
Defined in:
lib/ndr_import/table.rb

Overview

This class maintains the state of a table mapping and encapsulates the logic required to transform a table of data into “records”. Particular attention has been made to use enumerables throughout to help with the transformation of large quantities of data.

Direct Known Subclasses

NonTabular::Table

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Table

Returns a new instance of Table.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ndr_import/table.rb', line 23

def initialize(options = {})
  options.stringify_keys! if options.is_a?(Hash)
  validate_options(options)

  all_valid_options.each do |key|
    # This pattern is used to only set attributes if option specified,
    # which makes for more concise YAML serialization.
    options[key] && instance_variable_set("@#{key}", options[key])
  end

  @row_index = 0
end

Instance Attribute Details

#notifier=(value) ⇒ Object (writeonly)

Sets the attribute notifier

Parameters:

  • value

    the value to set the attribute notifier to.



21
22
23
# File 'lib/ndr_import/table.rb', line 21

def notifier=(value)
  @notifier = value
end

Class Method Details

.all_valid_optionsObject



12
13
14
# File 'lib/ndr_import/table.rb', line 12

def self.all_valid_options
  %w(canonical_name filename_pattern tablename_pattern header_lines footer_lines format klass columns)
end

Instance Method Details

#all_valid_optionsObject



16
17
18
# File 'lib/ndr_import/table.rb', line 16

def all_valid_options
  self.class.all_valid_options
end

#header_valid?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/ndr_import/table.rb', line 86

def header_valid?
  @header_valid == true
end

#match(filename, tablename) ⇒ Object



36
37
38
39
# File 'lib/ndr_import/table.rb', line 36

def match(filename, tablename)
  ::File.basename(filename) =~ (filename_pattern || /\A.*\z/) &&
    (tablename.nil? || tablename =~ (tablename_pattern || /\A.*\z/))
end

#process_line(line, &block) ⇒ Object

This method process a line of data, If it is a header line it validates it, otherwise transforms it. It also increments and row index and notifies the amount of lines processed.



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ndr_import/table.rb', line 60

def process_line(line, &block)
  return enum_for(:process_line, line) unless block

  if @row_index < header_lines
    validate_header(line, @columns)
  else
    fail 'Header is not valid' if header_lines > 0 && !header_valid?
    transform_line(line, @row_index, &block)
  end

  @row_index += 1
  @notifier.try(:processed, @row_index)
end

#transform(lines, &block) ⇒ Object

This method transforms a table of data, given a line array/enumerator and yields klass, fields and index (input row number) for each record that it would create as a result of the transformation process.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ndr_import/table.rb', line 44

def transform(lines, &block)
  return enum_for(:transform, lines) unless block

  @row_index = 0
  @header_valid = false
  @notifier.try(:started)

  skip_footer_lines(lines, footer_lines).each do |line|
    process_line(line, &block)
  end

  @notifier.try(:finished)
end

#transform_line(line, index) ⇒ Object

This method transforms an incoming line of data by applying each of the klass masked mappings to the line and yielding the klass and fields for each mapped klass.



76
77
78
79
80
81
82
83
84
# File 'lib/ndr_import/table.rb', line 76

def transform_line(line, index)
  return enum_for(:transform_line, line, index) unless block_given?

  masked_mappings.each do |klass, klass_mappings|
    fields = mapped_line(line, klass_mappings)
    next if fields[:skip].to_s == 'true'
    yield(klass, fields, index)
  end
end