Class: Ticket::Replicator::FileLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/ticket/replicator/file_loader.rb

Defined Under Namespace

Classes: LoadError

Constant Summary collapse

FIRST_DATA_ROW_LINE_NUMBER =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jira_project, extracted_path) ⇒ FileLoader

Returns a new instance of FileLoader.



20
21
22
23
# File 'lib/ticket/replicator/file_loader.rb', line 20

def initialize(jira_project, extracted_path)
  @jira_project = jira_project
  @extracted_path = extracted_path
end

Instance Attribute Details

#extracted_pathObject (readonly)

Returns the value of attribute extracted_path.



18
19
20
# File 'lib/ticket/replicator/file_loader.rb', line 18

def extracted_path
  @extracted_path
end

#jira_projectObject (readonly)

Returns the value of attribute jira_project.



18
19
20
# File 'lib/ticket/replicator/file_loader.rb', line 18

def jira_project
  @jira_project
end

Class Method Details

.run_on(jira_project, file_path) ⇒ Object



10
11
12
13
14
# File 'lib/ticket/replicator/file_loader.rb', line 10

def self.run_on(jira_project, file_path)
  log.debug { "Loading #{file_path} into #{jira_project.project_key}..." }

  new(jira_project, file_path).run
end

Instance Method Details

#rowsObject



41
42
43
# File 'lib/ticket/replicator/file_loader.rb', line 41

def rows
  CSV.read(extracted_path, headers: true, header_converters: :symbol)
end

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ticket/replicator/file_loader.rb', line 26

def run
  rows.each_with_index do |row, index|
    line_number = index + FIRST_DATA_ROW_LINE_NUMBER
    RowLoader.run_on(jira_project, row)
  rescue StandardError => e
    message = <<~EOERROR
      #{extracted_path}:#{line_number}: error while loading row:
      #{e.message}:
      #{row.inspect}
    EOERROR

    raise LoadError, message, e.backtrace
  end
end