class XDR::Union

Attributes

arm[R]
switch[R]

Public Class Methods

arm_for_switch(switch) click to toggle source
# 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
new(switch=nil, value=:void) click to toggle source
# 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
read(io) click to toggle source
# 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) click to toggle source
# File lib/xdr/union.rb, line 50
def self.valid?(val)
  val.is_a?(self)
end
write(val, io) click to toggle source
# 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

Public Instance Methods

attribute!(attr) click to toggle source
# 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
get()
Alias for: value
set(switch, value=:void) click to toggle source
# 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() click to toggle source
# File lib/xdr/union.rb, line 61
def to_xdr
  self.class.to_xdr self
end
value() click to toggle source
# File lib/xdr/union.rb, line 76
def value
  @value unless @value == :void
end
Also aliased as: get

Private Instance Methods

valid_for_arm_type(value, arm) click to toggle source
# File lib/xdr/union.rb, line 91
def valid_for_arm_type(value, arm)
  arm_type = arms[@arm]

  return value == :void if arm_type.nil?

  arm_type.valid?(value)
end