Class: DataImport::BaseParser

Inherits:
Object
  • Object
show all
Defined in:
app/services/data_import/base_parser.rb

Overview

Service object: base parser.

Direct Known Subclasses

CsvParser, XlsxParser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, file_name, options = {}) ⇒ BaseParser

Returns a new instance of BaseParser.

Parameters:

  • file_path (String)

    path to the import file

  • file_name (String)

    original file name (used for format detection)

  • options (Hash) (defaults to: {})

    parser options

Options Hash (options):

  • first_row_is_header (Boolean)

    treat the first row as column headers



10
11
12
13
14
15
16
# File 'app/services/data_import/base_parser.rb', line 10

def initialize(file_path, file_name, options = {})
  raise "File not found" unless file_path && File.exist?(file_path)

  @file_path = file_path
  @options = options
  @file_name = file_name
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



4
5
6
# File 'app/services/data_import/base_parser.rb', line 4

def file_name
  @file_name
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



4
5
6
# File 'app/services/data_import/base_parser.rb', line 4

def file_path
  @file_path
end

#formatObject (readonly)

Returns the value of attribute format.



4
5
6
# File 'app/services/data_import/base_parser.rb', line 4

def format
  @format
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'app/services/data_import/base_parser.rb', line 4

def options
  @options
end

Class Method Details

.instantiate(file_path, file_name, options = {}) ⇒ DataImport::BaseParser

Returns the parser for the detected format.

Parameters:

  • file_path (String)

    path to the import file

  • file_name (String)

    original file name (used for format detection)

  • options (Hash) (defaults to: {})

    parser options

Options Hash (options):

  • format (Symbol, nil)

    parser format override (defaults to the file extension)

Returns:



31
32
33
34
35
36
# File 'app/services/data_import/base_parser.rb', line 31

def self.instantiate(file_path, file_name, options = {})
  format = options.delete(:format)
  format ||= File.extname(file_name)[1..].to_sym
  parserKlass = "DataImport::#{format.to_s.titleize}Parser".constantize
  parserKlass.new(file_path, file_name, options)
end

Instance Method Details

#column_namesObject



22
23
24
# File 'app/services/data_import/base_parser.rb', line 22

def column_names
  raise "Not Implemented"
end

#each_with_indexObject



18
19
20
# File 'app/services/data_import/base_parser.rb', line 18

def each_with_index
  raise "Not Implemented"
end

#first_row_is_header?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/services/data_import/base_parser.rb', line 38

def first_row_is_header?
  options[:first_row_is_header]
end

#sizeObject



46
47
48
# File 'app/services/data_import/base_parser.rb', line 46

def size
  raise "Not Implemented"
end

#validateObject



42
43
44
# File 'app/services/data_import/base_parser.rb', line 42

def validate
  raise "Not Implemented"
end