Class: BareTypes::Optional

Inherits:
BaseType show all
Defined in:
lib/types.rb

Overview

endregion

Instance Method Summary collapse

Methods inherited from BaseType

#cycle_search

Constructor Details

#initialize(optionalType) ⇒ Optional

Returns a new instance of Optional.



421
422
423
424
# File 'lib/types.rb', line 421

def initialize(optionalType)
  raise VoidUsedOutsideTaggedSet() if optionalType.class == BareTypes::Void
  @optionalType = optionalType
end

Instance Method Details

#==(otherType) ⇒ Object



397
398
399
# File 'lib/types.rb', line 397

def ==(otherType)
  return otherType.class == BareTypes::Optional && otherType.optionalType == @optionalType
end

#decode(msg) ⇒ Object



435
436
437
438
439
440
441
# File 'lib/types.rb', line 435

def decode(msg)
  if msg.unpack("C")[0] == 0
    return nil, msg[1..msg.size]
  else
    return @optionalType.decode(msg[1..msg.size])
  end
end

#encode(msg, buffer) ⇒ Object



426
427
428
429
430
431
432
433
# File 'lib/types.rb', line 426

def encode(msg, buffer)
  if msg.nil?
    buffer << "\x00".b
  else
    buffer << "\x01".b
    @optionalType.encode(msg, buffer)
  end
end

#finalize_references(schema) ⇒ Object



407
408
409
410
411
412
413
414
415
# File 'lib/types.rb', line 407

def finalize_references(schema)
  return if @finalized
  @finalized = true
  if @optionalType.is_a?(Symbol)
    @optionalType = Reference.new(@optionalType, schema[@optionalType])
  else
    @optionalType.finalize_references(schema)
  end
end

#optionalTypeObject



417
418
419
# File 'lib/types.rb', line 417

def optionalType
  @optionalType
end

#to_schema(buffer) ⇒ Object



401
402
403
404
405
# File 'lib/types.rb', line 401

def to_schema(buffer)
  buffer << "optional<"
  @optionalType.to_schema(buffer)
  buffer << ">"
end