Class: Stellar::Thresholds

Inherits:
Object
  • Object
show all
Defined in:
lib/stellar/thresholds.rb

Constant Summary collapse

COMPONENTS =
[:master_weight, :low, :medium, :high]
VALID_RANGE =
0..255

Class Method Summary collapse

Class Method Details

.make(thresholds = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/stellar/thresholds.rb', line 6

def make(thresholds = {})
  # error if any of the needed components are not provided
  if COMPONENTS.any? { |c| thresholds[c].blank? }
    raise ArgumentError, "invalid thresholds hash, must have #{COMPONENTS.inspect} keys, had: #{thresholds.keys.inspect}"
  end

  # error if any of the needed components are not numbers 0 <= N <= 255
  COMPONENTS.each do |c|
    good = true

    good &&= thresholds[c].is_a?(Integer)
    good &&= VALID_RANGE.include? thresholds[c]

    unless good
      raise ArgumentError, "invalid #{c.inspect}, must be number in (0..255), got #{thresholds[c].inspect}"
    end
  end

  thresholds.values_at(*COMPONENTS).pack("C*")
end

.parse(combined) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/stellar/thresholds.rb', line 27

def parse(combined)
  master_weight, low, medium, high = combined.unpack("C*")
  {
    master_weight: master_weight,
    low: low,
    medium: medium,
    high: high
  }
end