Class: NFS::XDR::Union

Inherits:
Object
  • Object
show all
Defined in:
lib/nfs/xdr.rb

Overview

Each arm of the union is represented as a struct

Instance Method Summary collapse

Constructor Details

#initialize(disc_type, &block) ⇒ Union

Returns a new instance of Union.



241
242
243
244
245
246
# File 'lib/nfs/xdr.rb', line 241

def initialize(disc_type, &block)
  @disc_type = disc_type
  @arms = {}
  @default_arm = nil
  instance_eval(&block) if block_given?
end

Instance Method Details

#arm(disc_value, struct = nil, &block) ⇒ Object

Add an arm



249
250
251
252
253
254
255
# File 'lib/nfs/xdr.rb', line 249

def arm(disc_value, struct = nil, &block)
  if block_given?
    struct = Structure.new(&block)
  end

  @arms[disc_value] = struct
end

#decode(string) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/nfs/xdr.rb', line 279

def decode(string)
  disc = @disc_type.decode(string)
  arm = @default_arm

  if @arms.include?(disc)
    arm = @arms[disc]
  end

  result = nil

  if arm.nil?
    result = {}
  else
    result = arm.decode(string)
  end

  result[:_discriminant] = disc
  result
end

#default(struct = nil, &block) ⇒ Object

Set the default arm



258
259
260
261
262
263
264
# File 'lib/nfs/xdr.rb', line 258

def default(struct = nil, &block)
  if block_given?
    struct = Structure.new(&block)
  end

  @default_arm = struct
end

#encode(struct) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/nfs/xdr.rb', line 266

def encode(struct)
  disc = struct[:_discriminant]
  arm = @default_arm
  arm = @arms[disc] if @arms.include?(disc)
  result = @disc_type.encode(disc)

  unless arm.nil?
    result << arm.encode(struct)
  end

  result
end