Class: Qti::V1::Models::Numerics::Precision

Inherits:
ScoringBase
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper
Defined in:
lib/qti/v1/models/numerics/precision.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ScoringBase

#equal_node, #gt_node, #gte_node, #initialize, #lte_node

Constructor Details

This class inherits a constructor from Qti::V1::Models::Numerics::ScoringBase

Class Method Details

.significant_digits(number_s) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/qti/v1/models/numerics/precision.rb', line 46

def self.significant_digits(number_s)
  sig = 0
  zeros = 0
  number_s.each_char do |c|
    next if (sig.zero? && c == '0') || c == '.'
    if c == '0'
      zeros += 1
    else
      sig += zeros + 1
      zeros = 0
    end
  end
  # this is not exactly significant digits
  # trailing zeros are ignored
  # because gt_node and lte_node don't expect trailing zeros
  sig
end

Instance Method Details

#precisionObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/qti/v1/models/numerics/precision.rb', line 30

def precision
  overridden_precision = equal_node.attributes['precision']&.value

  return overridden_precision.to_i if overridden_precision.present?

  sig = [
    Precision.significant_digits(gt_node.content),
    Precision.significant_digits(lte_node.content)
  ].max
  sig - 1
end

#precision_typeObject



42
43
44
# File 'lib/qti/v1/models/numerics/precision.rb', line 42

def precision_type
  equal_node.attributes['precisiontype']&.value || 'significantDigits'
end

#scoring_dataObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/qti/v1/models/numerics/precision.rb', line 8

def scoring_data
  return unless equal_node && gt_node && lte_node
  Struct.new(
    :id,
    :type,
    :value,
    :precision,
    :precision_type
  ).new(
    equal_node.attributes['respident']&.value,
    'preciseResponse',
    value,
    precision.to_s,
    precision_type
  )
end

#valueObject



25
26
27
28
# File 'lib/qti/v1/models/numerics/precision.rb', line 25

def value
  fval = equal_node.content.to_f
  number_with_precision(fval, precision: precision, significant: precision_type == 'significantDigits')
end