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.



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

def before
  @before
end

#directoryObject

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

#filesObject

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

#headerObject

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

#loggerObject

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

#rowsObject

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

#typeObject

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.

Parameters:

  • args (Array)

    an array of strings with an optional options hash

  • options (Hash)

    a customizable set of options



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

def populates(*args)
  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.



87
88
89
# File 'lib/smilodon.rb', line 87

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.



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