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 = nil, value = :void) ⇒ Union
Returns a new instance of Union.
54
55
56
57
58
59
|
# File 'lib/xdr/union.rb', line 54
def initialize(switch=nil, value=:void)
@switch = nil
@arm = nil
@value = nil
set(switch, value) if switch
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
|
# File 'lib/xdr/union.rb', line 23
def self.arm_for_switch(switch)
raise XDR::InvalidSwitchError unless switch.is_a?(switch_type)
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
|
.read(io) ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/xdr/union.rb', line 36
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
50
51
52
|
# File 'lib/xdr/union.rb', line 50
def self.valid?(val)
val.is_a?(self)
end
|
.write(val, io) ⇒ Object
44
45
46
47
48
|
# File 'lib/xdr/union.rb', line 44
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
#attribute!(attr) ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/xdr/union.rb', line 82
def attribute!(attr)
if @arm.to_s != attr
raise XDR::ArmNotSetError, "#{attr} is not the set arm"
end
get
end
|
#set(switch, value = :void) ⇒ Object
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/xdr/union.rb', line 65
def set(switch, value=:void)
@switch = switch.is_a?(switch_type) ? switch : switch_type.from_name(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
61
62
63
|
# File 'lib/xdr/union.rb', line 61
def to_xdr
self.class.to_xdr self
end
|
#value ⇒ Object
Also known as:
get
76
77
78
|
# File 'lib/xdr/union.rb', line 76
def value
@value unless @value == :void
end
|