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
-
#from_xdr(string, encoding = "raw") ⇒ Object
Deserializes an object from the provided string of bytes.
-
#read(io) ⇒ Object
abstract
Reads from the provided IO an instance of the implementing class.
-
#to_xdr(val, encoding = "raw") ⇒ String
Serialized the provided val to xdr, returning a string of the serialized data.
-
#valid?(value) ⇒ Boolean
abstract
Returns true if the value provided is compatible with this serializer class.
-
#write(val, io) ⇒ void
abstract
Serialized the provided ‘val` to xdr and writes it to `io`.
Instance Method Details
#from_xdr(string, encoding = "raw") ⇒ Object
Deserializes an object from the provided string of bytes
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 61 def from_xdr(string, encoding = "raw") raw = case String(encoding) when "raw" then string when "hex" then [string].pack("H*") when "base64" then Base64.strict_decode64(string) else raise ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'" end io = StringIO.new(raw) result = read(io) if io.pos != io.length raise ArgumentError, "Input string not fully consumed! are you decoding the right xdr type?" end result end |
#read(io) ⇒ Object
This method is abstract.
Reads from the provided IO an instance of the implementing class
23 24 25 |
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 23 def read(io) raise NotImplementedError, "implement in including class" end |
#to_xdr(val, encoding = "raw") ⇒ String
Serialized the provided val to xdr, returning a string of the serialized data
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 42 def to_xdr(val, encoding = "raw") io = StringIO.new write(val, io) raw = io.string.force_encoding("ASCII-8BIT") case String(encoding) when "raw" then raw when "hex" then raw.unpack1("H*") when "base64" then Base64.strict_encode64(raw) else raise ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'" end end |
#valid?(value) ⇒ Boolean
This method is abstract.
Returns true if the value provided is compatible with this serializer class
32 33 34 |
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 32 def valid?(value) raise NotImplementedError, "implement in including class" end |
#write(val, io) ⇒ void
This method is abstract.
This method returns an undefined value.
Serialized the provided ‘val` to xdr and writes it to `io`
13 14 15 |
# File 'lib/xdr/concerns/converts_to_xdr.rb', line 13 def write(val, io) raise NotImplementedError, "implement in including class" end |