Class: XDR::Struct
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
from_xdr, read, valid?, write
attribute
Constructor Details
#initialize(attributes = {}) ⇒ Struct
Returns a new instance of Struct.
39
40
41
42
|
# File 'lib/xdr/struct.rb', line 39
def initialize(attributes={})
@attributes = {}
super
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
18
19
20
|
# File 'lib/xdr/struct.rb', line 18
def attributes
@attributes
end
|
Class Method Details
.read(io) ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/xdr/struct.rb', line 20
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
35
36
37
|
# File 'lib/xdr/struct.rb', line 35
def self.valid?(val)
val.is_a?(self)
end
|
.write(val, io) ⇒ Object
28
29
30
31
32
33
|
# File 'lib/xdr/struct.rb', line 28
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
#==(other) ⇒ Object
Compares two structs for equality
66
67
68
69
|
# File 'lib/xdr/struct.rb', line 66
def == (other)
return false unless other.is_a?(self.class)
other.attributes == self.attributes
end
|
#eql?(other) ⇒ Boolean
71
72
73
74
|
# File 'lib/xdr/struct.rb', line 71
def eql? (other)
return false unless other.is_a?(self.class)
other.attributes.eql? self.attributes
end
|
#hash ⇒ Object
76
77
78
|
# File 'lib/xdr/struct.rb', line 76
def hash
[self.class, self.attributes].hash
end
|
#read_attribute(attr) ⇒ Object
80
81
82
|
# File 'lib/xdr/struct.rb', line 80
def read_attribute(attr)
@attributes[attr]
end
|
#to_xdr(format = :raw) ⇒ String
Serializes struct to xdr, return a string of bytes
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/xdr/struct.rb', line 51
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
|
#write_attribute(attr, value) ⇒ Object
84
85
86
|
# File 'lib/xdr/struct.rb', line 84
def write_attribute(attr, value)
@attributes[attr] = value
end
|