class BareTypes::Map

Public Class Methods

new(fromType, toType) click to toggle source
# File lib/types.rb, line 151
def initialize(fromType, toType)
  raise VoidUsedOutsideTaggedSet if fromType.class == BareTypes::Void or toType.class == BareTypes::Void
  if !fromType.class.ancestors.include?(BarePrimitive) ||
      fromType.is_a?(BareTypes::Data) ||
      fromType.is_a?(BareTypes::DataFixedLen)
    raise MapKeyError("Map keys must use a primitive type which is not data or data<length>.")
  end
  @from = fromType
  @to = toType
end

Public Instance Methods

==(otherType) click to toggle source
# File lib/types.rb, line 137
def ==(otherType)
  return otherType.class == BareTypes::Map && otherType.from == @from && otherType.to == @to
end
decode(msg) click to toggle source
# File lib/types.rb, line 179
def decode(msg)
  hash = Hash.new
  mapSize, rest = Uint.new.decode(msg)
  (mapSize - 1).to_i.downto(0) do
    key, rest = @from.decode(rest)
    value, rest = @to.decode(rest)
    hash[key] = value
  end
  return hash, rest
end
encode(msg) click to toggle source
# File lib/types.rb, line 170
def encode(msg)
  bytes = Uint.new.encode(msg.size)
  msg.each do |from, to|
    bytes << @from.encode(from)
    bytes << @to.encode(to)
  end
  return bytes
end
finalize_references(schema) click to toggle source
# File lib/types.rb, line 141
def finalize_references(schema)
  return if @finalized
  @finalized = true
  if @from.is_a?(Symbol)
    @to = schema[@to]
  else
    @to.finalize_references(schema)
  end
end
from() click to toggle source
# File lib/types.rb, line 162
def from
  @from
end
to() click to toggle source
# File lib/types.rb, line 166
def to
  @to
end