Class: Presenters::QcThresholdPresenter::Threshold

Inherits:
Object
  • Object
show all
Defined in:
app/models/presenters/qc_threshold_presenter.rb

Overview

A threshold is a single QC attribute, and the configuration for the default range, the units used, and whether there are any defaults

Constant Summary collapse

RANGE_EXPANSION =

The range slider, unless otherwise configured, will set its range based on the maximum and minimum values observed. It will expand the range by the percentage indicated here to ensure the maximum and minimum values don’t butt right up against the range limits

5
DEFAULT_DECIMAL_PLACES =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, results, configuration) ⇒ Threshold

Returns a new instance of Threshold.



19
20
21
22
23
24
# File 'app/models/presenters/qc_threshold_presenter.rb', line 19

def initialize(key, results, configuration)
  @key = key
  @name = configuration.fetch(:name, key)
  @results = results
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



17
18
19
# File 'app/models/presenters/qc_threshold_presenter.rb', line 17

def configuration
  @configuration
end

#keyObject (readonly)

Returns the value of attribute key.



17
18
19
# File 'app/models/presenters/qc_threshold_presenter.rb', line 17

def key
  @key
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'app/models/presenters/qc_threshold_presenter.rb', line 17

def name
  @name
end

#resultsObject (readonly)

Returns the value of attribute results.



17
18
19
# File 'app/models/presenters/qc_threshold_presenter.rb', line 17

def results
  @results
end

Instance Method Details

#defaultFloat, Integer

The default position for the slider. If not set in the thresholds section of the purpose configuration is set to zero.

Returns:

  • (Float, Integer)

    The default slider position



67
68
69
# File 'app/models/presenters/qc_threshold_presenter.rb', line 67

def default
  configuration.fetch(:default_threshold, 0)
end

#enabled?Boolean

Indicates if the field should be enabled. Returns false if there are no QC results for thresholds to work with or the units used for the wells can’t be directly converted.

Returns:

  • (Boolean)


78
79
80
# File 'app/models/presenters/qc_threshold_presenter.rb', line 78

def enabled?
  results? && compatible_units?
end

#errorString

UI ready text to display to the user if the field is not enabled.

Returns:

  • (String)


87
88
89
90
91
92
93
# File 'app/models/presenters/qc_threshold_presenter.rb', line 87

def error
  return 'There are no QC results of this type to apply a threshold.' unless results?
  return if compatible_units?

  units = unique_units.map(&:units).join(', ')
  "Incompatible units #{units}. Automatic thresholds disabled."
end

#maxFloat

The maximum value for the slider. If not set in the thresholds section of the purpose configuration is calibrated based on the maximum observed value. (Or 100 if it’s a percentage)

Returns:

  • (Float)

    The maximum value to use for the range



33
34
35
# File 'app/models/presenters/qc_threshold_presenter.rb', line 33

def max
  @max ||= configuration.fetch(:max) { percentage? ? 100 : max_result + range_buffer }.ceil(decimal_places)
end

#minFloat

The minimum value for the slider. If not set in the thresholds section of the purpose configuration is calibrated based on the minimum observed value. (Or 0 if it’s a percentage)

Returns:

  • (Float)

    The minimum value to use for the range



44
45
46
# File 'app/models/presenters/qc_threshold_presenter.rb', line 44

def min
  @min ||= configuration.fetch(:min) { percentage? ? 0 : min_result - range_buffer }.floor(decimal_places)
end

#options {|configured_default, "#{configured_default} #{units}"| ... } ⇒ Object

Yields:

  • (configured_default, "#{configured_default} #{units}")


101
102
103
104
105
106
# File 'app/models/presenters/qc_threshold_presenter.rb', line 101

def options
  configured_default = configuration[:default_threshold]
  return unless configured_default

  yield configured_default, "#{configured_default} #{units}"
end

#stepObject



108
109
110
# File 'app/models/presenters/qc_threshold_presenter.rb', line 108

def step
  (10**-decimal_places).to_f
end

#unitsString

The units to use for the threshold. If not set in the thresholds section of the purpose configuration is calibrated based on the most sensitive observed units

Returns:

  • (String)

    The units to use for the threshold



55
56
57
58
59
# File 'app/models/presenters/qc_threshold_presenter.rb', line 55

def units
  @units ||= configuration.fetch(:units) { unique_units.min.units }
rescue ArgumentError
  unique_units.first
end

#value_for(qc_result) ⇒ Object



95
96
97
98
99
# File 'app/models/presenters/qc_threshold_presenter.rb', line 95

def value_for(qc_result)
  qc_result.unit_value.convert_to(units).scalar.to_f
rescue ArgumentError
  nil
end