Class: BinData::Choice

Inherits:
Base
  • Object
show all
Defined in:
lib/bindata/choice.rb

Overview

A Choice is a collection of data objects of which only one is active at any particular time.

require 'bindata'
require 'stringio'

choices = [ [:int8, {:value => 3}], [:int8, {:value => 5}] ]
a = BinData::Choice.new(:choices => choices, :selection => 1)
a.value # => 5

Parameters

Parameters may be provided at initialisation to control the behaviour of an object. These params are:

:choices

An array specifying the possible data objects. The format of the array is a list of symbols representing the data object type. If a choice is to have params passed to it, then it should be provided as [type_symbol, hash_params].

:selection

An index into the :choices array which specifies the currently active choice.

Instance Method Summary collapse

Methods inherited from Base

#do_read, #klass_lookup, lookup, mandatory_parameters, #num_bytes, optional_parameters, parameters, #read, read, register, #single_value?, #write

Constructor Details

#initialize(params = {}, env = nil) ⇒ Choice

Returns a new instance of Choice.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bindata/choice.rb', line 34

def initialize(params = {}, env = nil)
  super(params, env)

  # instantiate all choices
  @choices = []
  param(:choices).each do |choice_type, choice_params|
    choice_params ||= {}
    klass = klass_lookup(choice_type)
    if klass.nil?
      raise TypeError, "unknown type '#{choice_type.id2name}' for #{self}"
    end
    @choices << klass.new(choice_params, create_env)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/bindata/choice.rb', line 100

def method_missing(symbol, *args)
  if the_choice.respond_to?(symbol)
    the_choice.__send__(symbol, *args)
  else
    super
  end
end

Instance Method Details

#_do_read(io) ⇒ Object

Reads the value of the selected data object from io.



60
61
62
# File 'lib/bindata/choice.rb', line 60

def _do_read(io)
  the_choice.do_read(io)
end

#_num_bytes(what) ⇒ Object

Returns the number of bytes it will take to write the selected data object.



76
77
78
# File 'lib/bindata/choice.rb', line 76

def _num_bytes(what)
  the_choice.num_bytes(what)
end

#_write(io) ⇒ Object

Writes the value of the selected data object to io.



70
71
72
# File 'lib/bindata/choice.rb', line 70

def _write(io)
  the_choice.write(io)
end

#clearObject

Resets the internal state to that of a newly created object.



50
51
52
# File 'lib/bindata/choice.rb', line 50

def clear
  the_choice.clear
end

#clear?Boolean

Returns if the selected data object is clear?.

Returns:

  • (Boolean)


55
56
57
# File 'lib/bindata/choice.rb', line 55

def clear?
  the_choice.clear?
end

#done_readObject

To be called after calling #do_read.



65
66
67
# File 'lib/bindata/choice.rb', line 65

def done_read
  the_choice.done_read
end

#field_namesObject

Returns a list of the names of all fields of the selected data object.



86
87
88
# File 'lib/bindata/choice.rb', line 86

def field_names
  the_choice.field_names
end

#find_obj_for_name(name) ⇒ Object

Returns the data object that stores values for name.



91
92
93
# File 'lib/bindata/choice.rb', line 91

def find_obj_for_name(name)
  field_names.include?(name) ? the_choice.find_obj_for_name(name) : nil
end

#respond_to?(symbol, include_private = false) ⇒ Boolean

Override to include selected data object.

Returns:

  • (Boolean)


96
97
98
# File 'lib/bindata/choice.rb', line 96

def respond_to?(symbol, include_private = false)
  super || the_choice.respond_to?(symbol, include_private)
end

#snapshotObject

Returns a snapshot of the selected data object.



81
82
83
# File 'lib/bindata/choice.rb', line 81

def snapshot
  the_choice.snapshot
end