class BareTypes::Enum

Public Class Methods

new(source) click to toggle source
# File lib/types.rb, line 602
def initialize(source)
  @intToVal = {}
  @valToInt = {}
  raise BareException.new("Enum must initialized with a hash from integers to anything") if !source.is_a?(Hash)
  raise BareException.new("Enum must have unique positive integer assignments") if Set.new(source.keys).size != source.keys.size
  raise EnumValueError.new("Enum must have unique values") if source.values.to_set.size != source.values.size
  source.each do |k, v|
    raise("Enum keys must be positive integers") if k < 0
    @intToVal[k.to_i] = v
    @valToInt[v] = k.to_i
  end
end

Public Instance Methods

==(otherType) click to toggle source
# File lib/types.rb, line 586
def ==(otherType)
  return false unless otherType.class == BareTypes::Enum
  @intToVal.each do |int, val|
    return false unless otherType.intToVal[int] == val
  end
  return true
end
decode(msg) click to toggle source
# File lib/types.rb, line 622
def decode(msg)
  value, rest = BareTypes::Uint.new.decode(msg)
  return @intToVal[value], rest
end
encode(msg) click to toggle source
# File lib/types.rb, line 615
def encode(msg)
  raise SchemaMismatch.new("#{msg.inspect} is not part of this enum: #{@intToVal}") if !@valToInt.keys.include?(msg)
  integerRep = @valToInt[msg]
  encoded = BareTypes::Uint.new.encode(integerRep)
  return encoded
end
finalize_references(schema) click to toggle source
# File lib/types.rb, line 595
def finalize_references(schema)
end
intToVal() click to toggle source
# File lib/types.rb, line 598
def intToVal
  @intToVal
end