Class: QcReport::File

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

Overview

rubocop:todo Metrics/ClassLength

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.



21
22
23
24
25
26
27
# File 'app/models/qc_report/file.rb', line 21

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.



19
20
21
# File 'app/models/qc_report/file.rb', line 19

def errors
  @errors
end

#filenameObject (readonly)

Returns the value of attribute filename.



19
20
21
# File 'app/models/qc_report/file.rb', line 19

def filename
  @filename
end

#mime_typeObject (readonly)

Returns the value of attribute mime_type.



19
20
21
# File 'app/models/qc_report/file.rb', line 19

def mime_type
  @mime_type
end

Instance Method Details

#headersObject

A hash of the header section



72
73
74
# File 'app/models/qc_report/file.rb', line 72

def headers
  @headers || parse_headers
end

#processObject

Generate a report



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

def process
  @valid = true
  return false unless valid?

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

#qc_reportObject

The report to which the file corresponds



67
68
69
# File 'app/models/qc_report/file.rb', line 67

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

#report_identifierObject

The report identifier in the header section



77
78
79
# File 'app/models/qc_report/file.rb', line 77

def report_identifier
  headers[REPORT_ID_KEY]
end

#valid?Boolean

rubocop:todo Metrics/MethodLength

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/qc_report/file.rb', line 44

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

  true
end