class Quby::TableBackend::RangeTree

Public Class Methods

new(levels:, tree:) click to toggle source

@param tree [Hash<>] hash of hashes leading from parameter values/ranges to a result. @params levels [Array<String>] the argument name for each level of the tree.

# File lib/quby/table_backend/range_tree.rb, line 32
def initialize(levels:, tree:)
  @levels = levels
  @tree = tree
end

Public Instance Methods

lookup(parameters) click to toggle source

Given a parameters hash that contains a value or range for every level in the tree, find and return the normscore. ie. `lookup({age: 10, raw: 5, scale: 'Inhibitie', gender: 'male'})` => 39

# File lib/quby/table_backend/range_tree.rb, line 40
def lookup(parameters)
  validate_parameters(parameters)
  lookup_score(parameters)
end

Private Instance Methods

lookup_score(parameters) click to toggle source

Reduce the tree (a hash) to a normscore by looking up the correct values/ranges for each column in @levels. Returns a single normscore when found. Returns nil and reports to AppSignal when normscore could not be found.

# File lib/quby/table_backend/range_tree.rb, line 58
def lookup_score(parameters)
  @levels[0...-1].reduce(@tree) do |node, level|
    value = parameters[level.to_sym]
    # binding.pry
    case node.first.first # all keys for one level are the same type.
    when String
      node[value.to_s] # from csv it's always a string, but should also allow symbol param.
    when Float
      node[value.to_f] # from csv it's always a float, but should also allow int param.
    when Enumerable
      node.find { |k, _v| k.include? value }.last
    else
      node[value]
    end
  end
rescue StandardError => exception
  # Normscore could not be found for the given parameters.
  if defined? Roqua::Support::Errors
    Roqua::Support::Errors.report(exception, parameters: parameters)
  end
  nil
end
validate_parameters(parameters) click to toggle source

All parameters must be present to do a lookup but the order does not matter.

# File lib/quby/table_backend/range_tree.rb, line 48
def validate_parameters(parameters)
  if @levels[0...-1].sort != parameters.keys.map(&:to_s).sort
    fail 'Incompatible score parameters found'
  end
end