Sha256: c2a8ccba3c91bed671e238efce5efc3edbe8ae0cfb1b9ea98ac958ed322030bb

Contents?: true

Size: 1.06 KB

Versions: 27

Compression:

Stored size: 1.06 KB

Contents

module Stellar
  class << Thresholds
    COMPONENTS = [:master_weight, :low, :medium, :high]
    VALID_RANGE = 0..255

    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?(Fixnum)
        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

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

Version data entries

27 entries across 27 versions & 3 rubygems

Version Path
stellar-base-0.11.0 lib/stellar/thresholds.rb
auction-ruby-base-0.1.3 lib/stellar/thresholds.rb
auction-ruby-base-0.1.2 lib/stellar/thresholds.rb
auction-ruby-base-0.1.1 lib/stellar/thresholds.rb
stellar-base-0.10.0 lib/stellar/thresholds.rb
stellar-base-0.9.0 lib/stellar/thresholds.rb
stellar-base-0.8.0 lib/stellar/thresholds.rb
stellar-base-0.7.0 lib/stellar/thresholds.rb
stellar-base-0.6.1 lib/stellar/thresholds.rb
stellar-base-0.6.0 lib/stellar/thresholds.rb
stellar-base-0.5.0 lib/stellar/thresholds.rb
stellar-base-0.4.0 lib/stellar/thresholds.rb
open-core-0.3.0 lib/stellar/thresholds.rb
stellar-base-0.3.0 lib/stellar/thresholds.rb
stellar-base-0.2.0 lib/stellar/thresholds.rb
stellar-base-0.1.4 lib/stellar/thresholds.rb
stellar-base-0.1.3 lib/stellar/thresholds.rb
stellar-base-0.1.2 lib/stellar/thresholds.rb
stellar-base-0.1.1 lib/stellar/thresholds.rb
stellar-base-0.1.0 lib/stellar/thresholds.rb