module Populator
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.
@example
Define a module and extend it with the populator module.
@example A Custom Populator
.
module CustomPopulator extend Populator populates 'CustomFile' def process(row) ... end end
@note As noted in the example the extended module defines the
logic to process a single row.
Constants
- DIRECTORY
Default directory location for data files.
- TYPE
Default data file type is CSV.
Attributes
Attribute accessors for the directory, file name, header, count, rows and before hook.
Attribute accessors for the directory, file name, header, count, rows and before hook.
Attribute accessors for the directory, file name, header, count, rows and before hook.
Attribute accessors for the directory, file name, header, count, rows and before hook.
Attribute accessors for the directory, file name, header, count, rows and before hook.
Attribute accessors for the directory, file name, header, count, rows and before hook.
Attribute accessors for the directory, file name, header, count, rows and before hook.
Attribute accessors for the directory, file name, header, count, rows and before hook.
Public Instance Methods
Configuration helper.
@param [String] file The data file name. @option options [String] :directory ('db/populate/data_files') The location of the data file. @option options [String] :type ('csv') The data file type. @option options [Boolean] :header Set true if the file has a header. @option options [String] :before The method to call before the run.
# 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
Stub method to be defined in the extended module.
@raise [Exception] Raises an exception if the extended module does not override the method.
# File lib/populator.rb, line 76 def process(row = nil) raise MethodNotOverridden end
Parses the data file content and processes each row.
@return [Boolean] Returns true if all rows are processed successfully.
# 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
Private Instance Methods
The parser to use based on the type of data file.
@return [Parser, parse] Returns the parser class to use.
# File lib/populator.rb, line 85 def parser if type == 'csv' require 'csv' return CSV::Reader end end
Absolute path for the data file.
@return [String] The absolute path.
# File lib/populator.rb, line 95 def path "#{Rails.root}/#{directory}/#{file}.#{type}" end
Reads the data file.
@return [File] The data file contents. @raise [DataFileNotConfigured] Raises an exception when the file is not configured. @raise [MissingDataFile] Raises an exception when the configured file is missing.
# File lib/populator.rb, line 104 def read raise DataFileNotConfigured unless file raise MissingDataFile unless File.exist?(path) File.read path end