Class: XDR::Option

Inherits:
Object
  • Object
show all
Includes:
Concerns::ConvertsToXDR
Defined in:
lib/xdr/option.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::ConvertsToXDR

#from_xdr, #to_xdr

Constructor Details

#initialize(child_type) ⇒ Option

Returns a new instance of Option.



8
9
10
11
# File 'lib/xdr/option.rb', line 8

def initialize(child_type)
  # TODO, raise an error if child_type is not ConvertToXDR
  @child_type = child_type
end

Instance Attribute Details

#child_typeObject (readonly)

Returns the value of attribute child_type.



6
7
8
# File 'lib/xdr/option.rb', line 6

def child_type
  @child_type
end

Instance Method Details

#read(io) ⇒ Object



22
23
24
25
# File 'lib/xdr/option.rb', line 22

def read(io)
  present = XDR::Bool.read(io)
  @child_type.read(io) if present
end

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/xdr/option.rb', line 27

def valid?(val)
  val.nil? || @child_type.valid?(val)
end

#write(val, io) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/xdr/option.rb', line 13

def write(val, io)
  if val.nil?
    XDR::Bool.write(false, io)
  else
    XDR::Bool.write(true, io)
    @child_type.write(val, io)
  end
end