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
attribute, 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
111
112
113
114
115
|
# File 'lib/xdr/union.rb', line 111
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
99
100
101
102
103
104
105
|
# File 'lib/xdr/union.rb', line 99
def attribute!(attr)
if @arm.to_s != attr
raise XDR::ArmNotSetError, "#{attr} is not the set arm"
end
get
end
|
#eql?(other) ⇒ Boolean
117
118
119
120
121
|
# File 'lib/xdr/union.rb', line 117
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
123
124
125
|
# File 'lib/xdr/union.rb', line 123
def hash
[self.class, self.switch, self.value].hash
end
|
#set(switch, value = :void) ⇒ Object
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/xdr/union.rb', line 82
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 ⇒ Object
78
79
80
|
# File 'lib/xdr/union.rb', line 78
def to_xdr
self.class.to_xdr self
end
|
#value ⇒ Object
Also known as:
get
93
94
95
|
# File 'lib/xdr/union.rb', line 93
def value
@value unless @value == :void
end
|