Class: XDR::Union
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
from_xdr, read, valid?, write
Methods included from DSL::Union
switch_on
Constructor Details
#initialize(switch = :__unset__, value = :void) ⇒ Union
Returns a new instance of Union.
71
72
73
74
75
76
|
# File 'lib/xdr/union.rb', line 71
def initialize(switch=:__unset__, value=:void)
@switch = nil
@arm = nil
@value = nil
set(switch, value) unless switch == :__unset__
end
|
Instance Attribute Details
#arm ⇒ Object
Returns the value of attribute arm.
14
15
16
|
# File 'lib/xdr/union.rb', line 14
def arm
@arm
end
|
#switch ⇒ Object
Returns the value of attribute switch.
13
14
15
|
# File 'lib/xdr/union.rb', line 13
def switch
@switch
end
|
Class Method Details
.arm_for_switch(switch) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/xdr/union.rb', line 23
def self.arm_for_switch(switch)
begin
switch = normalize_switch switch
rescue ArgumentError => e
raise XDR::InvalidSwitchError, e.message
end
result = switches.fetch(switch, :switch_not_found)
result = switches.fetch(:default, :switch_not_found) if result == :switch_not_found
if result == :switch_not_found
raise XDR::InvalidSwitchError, "Bad switch: #{switch}"
end
result
end
|
.normalize_switch(switch) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/xdr/union.rb', line 40
def self.normalize_switch(switch)
case
when switch.is_a?(self.switch_type)
switch
when self.switch_type.valid?(switch)
switch
when self.switch_type.respond_to?(:from_name)
self.switch_type.from_name(switch)
else
raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{self.switch_type}"
end
end
|
.read(io) ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/xdr/union.rb', line 53
def self.read(io)
switch = switch_type.read(io)
arm = arm_for_switch(switch)
arm_type = arms[arm] || XDR::Void
value = arm_type.read(io)
new(switch, value)
end
|
.valid?(val) ⇒ Boolean
67
68
69
|
# File 'lib/xdr/union.rb', line 67
def self.valid?(val)
val.is_a?(self)
end
|
.write(val, io) ⇒ Object
61
62
63
64
65
|
# File 'lib/xdr/union.rb', line 61
def self.write(val, io)
switch_type.write(val.switch, io)
arm_type = arms[val.arm] || XDR::Void
arm_type.write(val.get,io)
end
|
Instance Method Details
#==(other) ⇒ Object
Compares two unions for equality
120
121
122
123
124
|
# File 'lib/xdr/union.rb', line 120
def == (other)
return false unless other.is_a?(self.class)
return false unless other.switch == self.switch
other.value == self.value
end
|
#attribute(attr) ⇒ Object
105
106
107
108
109
|
# File 'lib/xdr/union.rb', line 105
def attribute(attr)
return nil unless @arm.to_s == attr
get
end
|
#attribute!(attr) ⇒ Object
111
112
113
114
115
|
# File 'lib/xdr/union.rb', line 111
def attribute!(attr)
raise XDR::ArmNotSetError, "#{attr} is not the set arm" unless @arm.to_s == attr
get
end
|
#eql?(other) ⇒ Boolean
126
127
128
129
130
|
# File 'lib/xdr/union.rb', line 126
def eql?(other)
return false unless other.is_a?(self.class)
return false unless other.switch.eql? self.switch
other.value.eql? self.value
end
|
#hash ⇒ Object
132
133
134
|
# File 'lib/xdr/union.rb', line 132
def hash
[self.class, self.switch, self.value].hash
end
|
#set(switch, value = :void) ⇒ Object
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/xdr/union.rb', line 88
def set(switch, value=:void)
@switch = self.class.normalize_switch switch
@arm = self.class.arm_for_switch @switch
raise XDR::InvalidValueError unless valid_for_arm_type(value, @arm)
@value = value
rescue XDR::EnumNameError
raise XDR::InvalidSwitchError, "Bad switch: #{switch}"
end
|
#to_xdr(format = :raw) ⇒ String
Serializes struct to xdr, return a string of bytes
84
85
86
|
# File 'lib/xdr/union.rb', line 84
def to_xdr(format=:raw)
self.class.to_xdr(self, format)
end
|
#value ⇒ Object
Also known as:
get
99
100
101
|
# File 'lib/xdr/union.rb', line 99
def value
@value unless @value == :void
end
|