Module: Populator

Overview

Note:

As noted in the example the extended module defines the logic to process a single row.

Populator includes helper methods to ease parsing data files. Assigning a header and iterating over rows is handled by the module via a simple configuration.

Examples:

Define a module and extend it with the populator module.

A Custom Populator.

module CustomPopulator
  extend Populator
  populates 'CustomFile'

  def process(row)
    ...
  end
end

Constant Summary collapse

DIRECTORY =

Default directory location for data files.

'files'
TYPE =

Default data file type is CSV.

'csv'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#beforeObject

Attribute accessors for the directory, file name, header, count, rows and before hook.



33
34
35
# File 'lib/populator.rb', line 33

def before
  @before
end

#countObject

Attribute accessors for the directory, file name, header, count, rows and before hook.



33
34
35
# File 'lib/populator.rb', line 33

def count
  @count
end

#directoryObject

Attribute accessors for the directory, file name, header, count, rows and before hook.



33
34
35
# File 'lib/populator.rb', line 33

def directory
  @directory
end

#fileObject

Attribute accessors for the directory, file name, header, count, rows and before hook.



33
34
35
# File 'lib/populator.rb', line 33

def file
  @file
end

#headerObject

Attribute accessors for the directory, file name, header, count, rows and before hook.



33
34
35
# File 'lib/populator.rb', line 33

def header
  @header
end

#loggerObject

Attribute accessors for the directory, file name, header, count, rows and before hook.



33
34
35
# File 'lib/populator.rb', line 33

def logger
  @logger
end

#rowsObject

Attribute accessors for the directory, file name, header, count, rows and before hook.



33
34
35
# File 'lib/populator.rb', line 33

def rows
  @rows
end

#typeObject

Attribute accessors for the directory, file name, header, count, rows and before hook.



33
34
35
# File 'lib/populator.rb', line 33

def type
  @type
end

Instance Method Details

#populates(file, options = {}) ⇒ Object

Configuration helper.

Parameters:

  • file (String)

    The data file name.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :directory (String) — default: 'db/populate/data_files'

    The location of the data file.

  • :type (String) — default: 'csv'

    The data file type.

  • :header (Boolean)

    Set true if the file has a header.

  • :before (String)

    The method to call before the run.



42
43
44
45
46
47
48
49
50
51
# File 'lib/populator.rb', line 42

def populates(file, options = {})
  # Setup the logger to log populator warnings and messages.
  self.logger = PopulateLogger.setup

  self.directory = options[:directory] || DIRECTORY
  self.file = file
  self.type = options[:type] || TYPE
  self.header = options[:header]
  self.before = options[:before]
end

#process(row = nil) ⇒ Object

Stub method to be defined in the extended module.

Raises:

  • (Exception)

    Raises an exception if the extended module does not override the method.



76
77
78
# File 'lib/populator.rb', line 76

def process(row = nil)
  raise MethodNotOverridden
end

#runBoolean

Parses the data file content and processes each row.

Returns:

  • (Boolean)

    Returns true if all rows are processed successfully.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/populator.rb', line 56

def run

  # Call the before hook if defined.
  #
  # @usage
  #   populates 'TestFile', :before => :inactivate
  send(before) if before

  rows = parser.parse(read)
  self.count = header ? rows.count - 1 : rows.count
  self.header = rows.shift if header
  count.times { process(rows.shift) }

  # Return true when all rows are processed.
  return true
end