module XDR::Concerns::ConvertsToXDR

Public Instance Methods

from_xdr(string) click to toggle source

Deserializes an object from the provided string of bytes

@param string [String] the bytes to read from

@return [Object] the deserialized value

# File lib/xdr/concerns/converts_to_xdr.rb, line 53
def from_xdr(string)
  io = StringIO.new(string)
  read(io)
end
read(io) click to toggle source

Reads from the provided IO an instance of the implementing class @param io [IO] the io to read from

@return [Object] the deserialized value

# File lib/xdr/concerns/converts_to_xdr.rb, line 19
def read(io)
  raise NotImplementedError, "implement in including class"
end
to_xdr(val) click to toggle source

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

@param val [Object] the value to serialize

@return [String] the produced bytes

# 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) click to toggle source

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

@param value [Object] the value to test

@return [Boolean] true if valid, false otherwise

# File lib/xdr/concerns/converts_to_xdr.rb, line 29
def valid?(value)
  raise NotImplementedError, "implement in including class"
end
write(val, io) click to toggle source

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

@param val [Object] The object to serialize @param io [IO] an IO object to write to

# File lib/xdr/concerns/converts_to_xdr.rb, line 10
def write(val, io)
  raise NotImplementedError, "implement in including class"
end

Private Instance Methods

padding_for(length) click to toggle source
# File lib/xdr/concerns/converts_to_xdr.rb, line 59
def padding_for(length)
  case length % 4
  when 0 ; 0
  when 1 ; 3
  when 2 ; 2
  when 3 ; 1
  end
end