Module: Smilodon::Populator
- Included in:
- FakePopulator, FakePopulatorWithMultipleFiles, FakePopulatorWithOnlyDirectory, FakePopulatorWithOverriddenDirectory, FakePopulatorWithOverriddenType
- Defined in:
- lib/smilodon.rb
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, rows and before hook.
-
#directory ⇒ Object
Attribute accessors for the directory, file name, header, rows and before hook.
-
#files ⇒ Object
Attribute accessors for the directory, file name, header, rows and before hook.
-
#header ⇒ Object
Attribute accessors for the directory, file name, header, rows and before hook.
-
#logger ⇒ Object
Attribute accessors for the directory, file name, header, rows and before hook.
-
#rows ⇒ Object
Attribute accessors for the directory, file name, header, rows and before hook.
-
#type ⇒ Object
Attribute accessors for the directory, file name, header, rows and before hook.
Instance Method Summary collapse
-
#populates(*args) ⇒ 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, rows and before hook.
36 37 38 |
# File 'lib/smilodon.rb', line 36 def before @before end |
#directory ⇒ Object
Attribute accessors for the directory, file name, header, rows and before hook.
36 37 38 |
# File 'lib/smilodon.rb', line 36 def directory @directory end |
#files ⇒ Object
Attribute accessors for the directory, file name, header, rows and before hook.
36 37 38 |
# File 'lib/smilodon.rb', line 36 def files @files end |
#header ⇒ Object
Attribute accessors for the directory, file name, header, rows and before hook.
36 37 38 |
# File 'lib/smilodon.rb', line 36 def header @header end |
#logger ⇒ Object
Attribute accessors for the directory, file name, header, rows and before hook.
36 37 38 |
# File 'lib/smilodon.rb', line 36 def logger @logger end |
#rows ⇒ Object
Attribute accessors for the directory, file name, header, rows and before hook.
36 37 38 |
# File 'lib/smilodon.rb', line 36 def rows @rows end |
#type ⇒ Object
Attribute accessors for the directory, file name, header, rows and before hook.
36 37 38 |
# File 'lib/smilodon.rb', line 36 def type @type end |
Instance Method Details
#populates(*args) ⇒ Object
Configuration helper.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/smilodon.rb', line 45 def populates(*args) = args.last.is_a?(Hash) ? args.pop : {} self.directory = if defined?(Rails) "#{Rails.root}/#{[:directory] || DIRECTORY}" else [:directory] || DIRECTORY end self.type = [:type] || TYPE self.header = [:header] self.before = [:before] self.files = args.empty? ? grab_files : args end |
#process(row = nil) ⇒ Object
Stub method to be defined in the extended module.
87 88 89 |
# File 'lib/smilodon.rb', line 87 def process(row = nil) raise MethodNotOverridden end |
#run ⇒ Boolean
Parses the data file content and processes each row.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/smilodon.rb', line 63 def run files.each do |f| # Call the before hook if defined. # # @usage # populates 'TestFile', :before => :inactivate send(before) if before rows = parser.parse(read(f)) rows.each_with_index do |row, index| if index == 0 && header self.header = row else process(row) end end end # Return true when all rows are processed. true end |