class BareTypes::Union
Public Class Methods
new(intToType)
click to toggle source
# File lib/types.rb, line 217 def initialize(intToType) intToType.keys.each do |i| raise MinimumSizeError("Union's integer representations must be > 0, instead got: #{i}") if i < 0 or !i.is_a?(Integer) end raise MinimumSizeError("Union must have at least one type") if intToType.keys.size < 1 @intToType = intToType end
Public Instance Methods
==(otherType)
click to toggle source
# File lib/types.rb, line 209 def ==(otherType) return false unless otherType.is_a?(BareTypes::Union) @intToType.each do |int, type| return false unless type == otherType.intToType[int] end return true end
decode(msg)
click to toggle source
# File lib/types.rb, line 243 def decode(msg) int, rest = Uint.new.decode(msg) type = @intToType[int] value, rest = type.decode(rest) return {value: value, type: type}, rest end
encode(msg)
click to toggle source
# File lib/types.rb, line 225 def encode(msg) type = msg[:type] value = msg[:value] unionTypeInt = nil unionType = nil @intToType.each do |int, typ| if type.class == typ.class unionTypeInt = int unionType = typ break end end raise SchemaMismatch("Unable to find given type in union: #{@intToType.inspect}, type: #{type}") if unionType.nil? || unionTypeInt.nil? bytes = Uint.new.encode(unionTypeInt) encoded = unionType.encode(value) bytes << encoded end
finalize_references(schema)
click to toggle source
# File lib/types.rb, line 197 def finalize_references(schema) return if @finalized @finalized = true @intToType.keys.each do |key| if @intToType[key].is_a?(Symbol) @intToType[key] = schema[@intToType[key]] else @intToType[key].finalize_references(schema) end end end
intToType()
click to toggle source
# File lib/types.rb, line 193 def intToType @intToType end