Class: XDR::Union

Inherits:
Object
  • Object
show all
Extended by:
Concerns::ConvertsToXDR, DSL::Union
Includes:
ActiveModel::AttributeMethods, ActiveModel::Model
Defined in:
lib/xdr/union.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::ConvertsToXDR

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.



69
70
71
72
73
74
# File 'lib/xdr/union.rb', line 69

def initialize(switch = :__unset__, value = :void)
  @switch = nil
  @arm = nil
  @value = nil
  set(switch, value) unless switch == :__unset__
end

Instance Attribute Details

#armObject (readonly)

Returns the value of attribute arm.



13
14
15
# File 'lib/xdr/union.rb', line 13

def arm
  @arm
end

#switchObject (readonly)

Returns the value of attribute switch.



12
13
14
# File 'lib/xdr/union.rb', line 12

def switch
  @switch
end

Class Method Details

.arm_for_switch(switch) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/xdr/union.rb', line 22

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



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/xdr/union.rb', line 39

def self.normalize_switch(switch)
  if switch.is_a?(switch_type)
    switch
  elsif switch_type.valid?(switch)
    switch
  elsif switch_type.respond_to?(:from_name)
    switch_type.from_name(switch)
  else
    raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{switch_type}"
  end
end

.read(io) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/xdr/union.rb', line 51

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

Returns:

  • (Boolean)


65
66
67
# File 'lib/xdr/union.rb', line 65

def self.valid?(val)
  val.is_a?(self)
end

.write(val, io) ⇒ Object



59
60
61
62
63
# File 'lib/xdr/union.rb', line 59

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



118
119
120
121
122
# File 'lib/xdr/union.rb', line 118

def ==(other)
  return false unless other.is_a?(self.class)
  return false unless other.switch == switch
  other.value == value
end

#attribute(attr) ⇒ Object



103
104
105
106
107
# File 'lib/xdr/union.rb', line 103

def attribute(attr)
  return nil unless @arm.to_s == attr

  get
end

#attribute!(attr) ⇒ Object



109
110
111
112
113
# File 'lib/xdr/union.rb', line 109

def attribute!(attr)
  raise XDR::ArmNotSetError, "#{attr} is not the set arm" unless @arm.to_s == attr

  get
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
# File 'lib/xdr/union.rb', line 124

def eql?(other)
  return false unless other.is_a?(self.class)
  return false unless other.switch.eql? switch
  other.value.eql? value
end

#hashObject



130
131
132
# File 'lib/xdr/union.rb', line 130

def hash
  [self.class, switch, value].hash
end

#set(switch, value = :void) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/xdr/union.rb', line 86

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

Parameters:

  • format (:raw, :hex, :base64) (defaults to: :raw)

    The encoding used for the bytes produces, one of (:raw, :hex, :base64)

Returns:

  • (String)

    The encoded bytes of this struct



82
83
84
# File 'lib/xdr/union.rb', line 82

def to_xdr(format = :raw)
  self.class.to_xdr(self, format)
end

#valueObject Also known as: get



97
98
99
# File 'lib/xdr/union.rb', line 97

def value
  @value unless @value == :void
end