Module: XDR::Concerns::ConvertsToXDR

Includes:
ReadsBytes
Included in:
Array, Bool, Double, Enum, Float, Hyper, Int, Opaque, Option, Quadruple, String, Struct, Union, UnsignedHyper, UnsignedInt, VarArray, VarOpaque, Void
Defined in:
lib/xdr/concerns/converts_to_xdr.rb

Instance Method Summary collapse

Instance Method Details

#from_xdr(string) ⇒ Object

Deserializes an object from the provided string of bytes

Parameters:

  • string (String)

    the bytes to read from

Returns:

  • (Object)

    the deserialized value



53
54
55
56
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 53

def from_xdr(string)
  io = StringIO.new(string)
  read(io)
end

#read(io) ⇒ Object

Reads from the provided IO an instance of the implementing class

Parameters:

  • io (IO)

    the io to read from

Returns:

  • (Object)

    the deserialized value

Raises:

  • (NotImplementedError)


19
20
21
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 19

def read(io)
  raise NotImplementedError, "implement in including class"
end

#to_xdr(val) ⇒ String

Serialized the provided val to xdr, returning a string of the serialized data

Parameters:

  • val (Object)

    the value to serialize

Returns:

  • (String)

    the produced bytes



40
41
42
43
44
45
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 40

def to_xdr(val)
  StringIO.
    new.
    tap{|io| write(val, io)}.
    string.force_encoding("ASCII-8BIT")
end

#valid?(value) ⇒ Boolean

Returns true if the value provided is compatible with this serializer class

Parameters:

  • value (Object)

    the value to test

Returns:

  • (Boolean)

    true if valid, false otherwise

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 29

def valid?(value)
  raise NotImplementedError, "implement in including class"
end

#write(val, io) ⇒ Object

Serialized the provided ‘val` to xdr and writes it to `io`

Parameters:

  • val (Object)

    The object to serialize

  • io (IO)

    an IO object to write to

Raises:

  • (NotImplementedError)


10
11
12
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 10

def write(val, io)
  raise NotImplementedError, "implement in including class"
end