Module: RobotMaster::Priority

Defined in:
lib/robot-master/priority.rb

Overview

Maps priority values into priority classes

Constant Summary collapse

PRIORITIES =

all possible priority classes

%w{critical high default low}.map(&:to_sym)

Class Method Summary collapse

Class Method Details

.has_priority_items?(priorities) ⇒ Boolean

Returns true if the results queue has any high or critical priority items

Examples:

has_priority_items?([0, -1, -1, 0])
=> false
has_priority_items?([0, 0, 1])
=> true
has_priority_items?([:default, :default, :high])
=> true

Parameters:

  • priorities (Array<Integer>, Array<Symbol>)

Returns:

  • (Boolean)

    true if the results queue has any high or critical priority items


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/robot-master/priority.rb', line 39

def self.has_priority_items?(priorities)
  priorities.each.any? do |priority|
    unless priority.is_a?(Symbol) or priority.is_a?(Numeric)
      raise ArgumentError, "Illegal priority value #{priority}" 
    end
    [:critical, :high].include?(
      priority.is_a?(Numeric) ?
        priority_class(priority.to_i) :
        priority
      )
  end
end

.priority_class(priority) ⇒ Symbol

Converts the given priority number into a priority class

  • `:critical` when priority > 100

  • `:high` when 0 < priority <= 100

  • `:default` when priority == 0

  • `:low` when priority < 0

Parameters:

  • priority (Integer)

Returns:

  • (Symbol)

    the priority class into which the given priority falls


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/robot-master/priority.rb', line 16

def self.priority_class(priority)
  if priority > 100
    :critical
  elsif priority > 0 and priority <= 100
    :high
  elsif priority < 0
    :low
  else
    :default
  end
end

.priority_classes(priorities) ⇒ Array<Symbol>

Converts all priority numbers into the possible priority classes.

Examples:

priority_classes([1000, 101, 100, -100, -1, 99, 150])
=> [:critical, :high, :low]

Parameters:

  • priorities (Array<Integer>)

Returns:

  • (Array<Symbol>)

    a unique array of priority classes into which the given priorities fall, in order of highest priority first.


60
61
62
63
64
65
# File 'lib/robot-master/priority.rb', line 60

def self.priority_classes(priorities)
  # iterate in high-to-low order on unique priorities
  priorities.sort.reverse.uniq.collect do |priority| 
    priority_class(priority)
  end.uniq
end