Module: Populator
- Defined in:
- lib/populator.rb
Overview
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.
Constant Summary collapse
- DIRECTORY =
Default directory location for data files.
'files'
- TYPE =
Default data file type is CSV.
'csv'
Instance Attribute Summary collapse
-
#before ⇒ Object
Attribute accessors for the directory, file name, header, count, rows and before hook.
-
#count ⇒ Object
Attribute accessors for the directory, file name, header, count, rows and before hook.
-
#directory ⇒ Object
Attribute accessors for the directory, file name, header, count, rows and before hook.
-
#file ⇒ Object
Attribute accessors for the directory, file name, header, count, rows and before hook.
-
#header ⇒ Object
Attribute accessors for the directory, file name, header, count, rows and before hook.
-
#logger ⇒ Object
Attribute accessors for the directory, file name, header, count, rows and before hook.
-
#rows ⇒ Object
Attribute accessors for the directory, file name, header, count, rows and before hook.
-
#type ⇒ Object
Attribute accessors for the directory, file name, header, count, rows and before hook.
Instance Method Summary collapse
-
#populates(file, options = {}) ⇒ Object
Configuration helper.
-
#process(row = nil) ⇒ Object
Stub method to be defined in the extended module.
-
#run ⇒ Boolean
Parses the data file content and processes each row.
Instance Attribute Details
#before ⇒ Object
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 |
#count ⇒ Object
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 |
#directory ⇒ Object
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 |
#file ⇒ Object
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 |
#header ⇒ Object
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 |
#logger ⇒ Object
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 |
#rows ⇒ Object
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 |
#type ⇒ Object
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.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/populator.rb', line 42 def populates(file, = {}) # Setup the logger to log populator warnings and messages. self.logger = PopulateLogger.setup self.directory = [:directory] || DIRECTORY self.file = file self.type = [:type] || TYPE self.header = [:header] self.before = [:before] end |
#process(row = nil) ⇒ Object
Stub method to be defined in the extended module.
76 77 78 |
# File 'lib/populator.rb', line 76 def process(row = nil) raise MethodNotOverridden end |
#run ⇒ Boolean
Parses the data file content and processes each row.
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 |