Class: XDR::Struct

Inherits:
Object
  • Object
show all
Extended by:
Concerns::ConvertsToXDR, DSL::Struct
Includes:
ActiveModel::AttributeMethods, ActiveModel::Model
Defined in:
lib/xdr/struct.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::ConvertsToXDR

from_xdr, read, valid?, write

Methods included from DSL::Struct

attribute

Class Method Details

.read(io) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/xdr/struct.rb', line 15

def self.read(io)
  new.tap do |result|
    fields.each do |name, type|
      result.public_send("#{name}=", type.read(io))
    end
  end
end

.valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/xdr/struct.rb', line 30

def self.valid?(val)
  val.is_a?(self)
end

.write(val, io) ⇒ Object



23
24
25
26
27
28
# File 'lib/xdr/struct.rb', line 23

def self.write(val, io)
  fields.each do |name, type|
    field_val = val.public_send(name)
    type.write(field_val, io)
  end
end

Instance Method Details

#to_xdr(format = :raw) ⇒ String

Serializes struct to xdr, return a string of bytes

Parameters:

  • format=:raw (Symbol)

    The encoding used for the bytes produces, one of (:raw, :hex, :base64)

Returns:

  • (String)

    The encoded bytes of this struct



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/xdr/struct.rb', line 40

def to_xdr(format=:raw)
  raw = self.class.to_xdr(self)

  case format
  when :raw ;     raw
  when :hex ;     raw.unpack("H*").first
  when :base64 ;  Base64.strict_encode64(raw)
  else ; 
    raise ArgumentError, "Invalid format #{format.inspect}; must be :raw, :hex, or :base64"
  end
end