Class: BareTypes::Union
Instance Attribute Summary collapse
-
#intToType ⇒ Object
Returns the value of attribute intToType.
Instance Method Summary collapse
- #==(otherType) ⇒ Object
- #cycle_search(seen) ⇒ Object
- #decode(msg) ⇒ Object
- #encode(msg, buffer) ⇒ Object
- #finalize_references(schema) ⇒ Object
-
#initialize(intToType) ⇒ Union
constructor
A new instance of Union.
- #to_schema(buffer) ⇒ Object
Constructor Details
#initialize(intToType) ⇒ Union
Returns a new instance of Union.
539 540 541 542 543 544 545 |
# File 'lib/types.rb', line 539 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 |
Instance Attribute Details
#intToType ⇒ Object
Returns the value of attribute intToType.
507 508 509 |
# File 'lib/types.rb', line 507 def intToType @intToType end |
Instance Method Details
#==(otherType) ⇒ Object
531 532 533 534 535 536 537 |
# File 'lib/types.rb', line 531 def ==(otherType) return false unless otherType.is_a?(BareTypes::Union) @intToType.each do |int, type| return false unless type == otherType.intToType[int] end true end |
#cycle_search(seen) ⇒ Object
509 510 511 512 513 514 515 516 517 |
# File 'lib/types.rb', line 509 def cycle_search(seen) if @intToType.size == 1 seen.add(self) @intToType.values.each do |type| type.cycle_search(seen) end seen.pop end end |
#decode(msg) ⇒ Object
575 576 577 578 579 580 |
# File 'lib/types.rb', line 575 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, buffer) ⇒ Object
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
# File 'lib/types.rb', line 558 def encode(msg, buffer) 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? Uint.new.encode(unionTypeInt, buffer) unionType.encode(value, buffer) end |
#finalize_references(schema) ⇒ Object
519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/types.rb', line 519 def finalize_references(schema) return if @finalized @finalized = true @intToType.keys.each do |key| if @intToType[key].is_a?(Symbol) @intToType[key] = Reference.new(@intToType[key], schema[@intToType[key]]) else @intToType[key].finalize_references(schema) end end end |
#to_schema(buffer) ⇒ Object
547 548 549 550 551 552 553 554 555 556 |
# File 'lib/types.rb', line 547 def to_schema(buffer) buffer << "(" strs = [] @intToType.size.times do strs << "" end @intToType.values.each_with_index.map { |type, i| type.to_schema(strs[i]) } buffer << strs.join(" | ") buffer << ")" end |