Class: QcReport::File

Inherits:
Object
  • Object
show all
Defined in:
app/models/qc_report/file.rb

Constant Summary collapse

ACCEPTED_MIMETYPE =
'text/csv'
ACCEPTED_EXTENSTION =
'csv'
FILE_VERSION_KEY =
'Sequencescape QC Report'
REPORT_ID_KEY =
'Report Identifier'
MAXIMUM_HEADER_SIZE =

We set a maximum header size to stop really large documents from getting read into the header hash.

40
GROUP_SIZE =

The number of lines processed at a time.

400
DataError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, set_decision, filename = nil, mime_type = ACCEPTED_MIMETYPE) ⇒ File

Returns a new instance of File


18
19
20
21
22
23
24
# File 'app/models/qc_report/file.rb', line 18

def initialize(file, set_decision, filename = nil, mime_type = ACCEPTED_MIMETYPE)
  @file = file.to_io
  @filename = filename || File.basename(file.path)
  @mime_type = mime_type
  @errors = []
  @set_decision = set_decision
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors


16
17
18
# File 'app/models/qc_report/file.rb', line 16

def errors
  @errors
end

#filenameObject (readonly)

Returns the value of attribute filename


16
17
18
# File 'app/models/qc_report/file.rb', line 16

def filename
  @filename
end

#mime_typeObject (readonly)

Returns the value of attribute mime_type


16
17
18
# File 'app/models/qc_report/file.rb', line 16

def mime_type
  @mime_type
end

Instance Method Details

#headersObject

A hash of the header section


59
60
61
# File 'app/models/qc_report/file.rb', line 59

def headers
  @headers || parse_headers
end

#processObject

Generate a report


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/qc_report/file.rb', line 27

def process
  @valid = true
  return false unless valid?

  ActiveRecord::Base.transaction do
    begin
      each_group_of_decisions do |group|
        process_group(group)
      end
    rescue DataError, QcMetric::InvalidValue => e
      invalid(e.message)
      raise ActiveRecord::Rollback
    end
  end
  qc_report.proceed_decision! if @valid
  @valid
end

#qc_reportObject

The report to which the file corresponds


54
55
56
# File 'app/models/qc_report/file.rb', line 54

def qc_report
  @qc_report ||= QcReport.find_by(report_identifier: report_identifier)
end

#report_identifierObject

The report identifier in the header section


64
65
66
# File 'app/models/qc_report/file.rb', line 64

def report_identifier
  headers[REPORT_ID_KEY]
end

#valid?Boolean

Returns:

  • (Boolean)

45
46
47
48
49
50
51
# File 'app/models/qc_report/file.rb', line 45

def valid?
  return invalid("#{filename} was not a csv file") unless is_a_csv?
  return invalid("#{filename} does not appear to be a qc report file. Make sure the #{FILE_VERSION_KEY} line has not been removed.") unless is_a_report?
  return invalid("Couldn't find the report #{report_identifier}. Check that the report identifier has not been modified.") unless qc_report

  true
end