Module: Smilodon::Populator

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, rows and before hook.



35
36
37
# File 'lib/smilodon.rb', line 35

def before
  @before
end

#directoryObject

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



35
36
37
# File 'lib/smilodon.rb', line 35

def directory
  @directory
end

#filesObject

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



35
36
37
# File 'lib/smilodon.rb', line 35

def files
  @files
end

#headerObject

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



35
36
37
# File 'lib/smilodon.rb', line 35

def header
  @header
end

#loggerObject

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



35
36
37
# File 'lib/smilodon.rb', line 35

def logger
  @logger
end

#rowsObject

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



35
36
37
# File 'lib/smilodon.rb', line 35

def rows
  @rows
end

#typeObject

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



35
36
37
# File 'lib/smilodon.rb', line 35

def type
  @type
end

Instance Method Details

#populates(*args) ⇒ Object

Configuration helper.

Parameters:

  • args (Array)

    an array of strings with an optional options hash

  • options (Hash)

    a customizable set of options



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/smilodon.rb', line 44

def populates(*args)
  # Setup the logger to log populator warnings and messages.
  self.logger = PopulateLogger.setup

  options = args.last.is_a?(Hash) ? args.pop : {}

  self.directory = if defined?(Rails) 
    "#{Rails.root}/#{options[:directory] || DIRECTORY}"
  else
    options[:directory] || DIRECTORY
  end

  self.type = options[:type] || TYPE
  self.header = options[:header]
  self.before = options[:before]
  self.files = args.empty? ? grab_files : args
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.



89
90
91
# File 'lib/smilodon.rb', line 89

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.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/smilodon.rb', line 65

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