Class: BinData::Choice

Inherits:
Base
  • Object
show all
Includes:
DSLMixin
Defined in:
lib/bindata/choice.rb,
lib/bindata/trace.rb

Overview

A Choice is a collection of data objects of which only one is active at any particular time. Method calls will be delegated to the active choice.

require 'bindata'

type1 = [:string, {:value => "Type1"}]
type2 = [:string, {:value => "Type2"}]

choices = {5 => type1, 17 => type2}
a = BinData::Choice.new(:choices => choices, :selection => 5)
a # => "Type1"

choices = [ type1, type2 ]
a = BinData::Choice.new(:choices => choices, :selection => 1)
a # => "Type2"

choices = [ nil, nil, nil, type1, nil, type2 ]
a = BinData::Choice.new(:choices => choices, :selection => 3)
a # => "Type1"

Chooser = Struct.new(:choice)
mychoice = Chooser.new
mychoice.choice = 'big'

choices = {'big' => :uint16be, 'little' => :uint16le}
a = BinData::Choice.new(:choices => choices, :copy_on_change => true,
                        :selection => lambda { mychoice.choice })
a.assign(256)
a.to_binary_s #=> "\001\000"

mychoice.choice = 'little'
a.to_binary_s #=> "\000\001"

Parameters

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

:choices

Either an array or a hash specifying the possible data objects. The format of the array/hash.values 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]. An implementation constraint is that the hash may not contain symbols as keys.

:selection

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

:copy_on_change

If set to true, copy the value of the previous selection to the current selection whenever the selection changes. Default is false.

Instance Attribute Summary

Attributes inherited from Base

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSLMixin

included

Methods inherited from Base

#==, #_assign, #_do_num_bytes, #_do_read, #_do_write, #_snapshot, arg_extractor, bindata_name, #debug_name, #debug_name_of, #eval_parameter, #get_parameter, #has_parameter?, #initialize_with_deprecation, #inspect, #new, #num_bytes, #offset, #offset_of, #pretty_print, #read, read, register, register_self, register_subclasses, #rel_offset, #to_binary_s, #to_s, unregister_self, #write

Methods included from CheckOrAdjustOffsetMixin

#do_read_with_adjust_offset, #do_read_with_check_offset, included

Methods included from AcceptedParametersMixin

included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object

:nodoc:



145
146
147
# File 'lib/bindata/choice.rb', line 145

def method_missing(symbol, *args, &block) #:nodoc:
  current_choice.__send__(symbol, *args, &block)
end

Class Method Details

.sanitize_parameters!(params) ⇒ Object

:nodoc:



68
69
70
71
72
73
74
75
76
# File 'lib/bindata/choice.rb', line 68

def sanitize_parameters!(params) #:nodoc:
  params.merge!(dsl_params)

  if params.needs_sanitizing?(:choices)
    choices = choices_as_hash(params[:choices])
    ensure_valid_keys(choices)
    params[:choices] = params.create_sanitized_choices(choices)
  end
end

.turn_off_tracingObject



77
78
79
# File 'lib/bindata/trace.rb', line 77

def turn_off_tracing
  alias_method :hook_before_do_read, :null_method
end

.turn_on_tracingObject



73
74
75
# File 'lib/bindata/trace.rb', line 73

def turn_on_tracing
  alias_method :hook_before_do_read, :trace_selection
end

Instance Method Details

#assign(val) ⇒ Object



133
134
135
# File 'lib/bindata/choice.rb', line 133

def assign(val)
  current_choice.assign(val)
end

#clearObject

:nodoc:



125
126
127
# File 'lib/bindata/choice.rb', line 125

def clear #:nodoc:
  current_choice.clear
end

#clear?Boolean

:nodoc:

Returns:

  • (Boolean)


129
130
131
# File 'lib/bindata/choice.rb', line 129

def clear? #:nodoc:
  current_choice.clear?
end

#do_num_bytesObject

:nodoc:



158
159
160
# File 'lib/bindata/choice.rb', line 158

def do_num_bytes #:nodoc:
  current_choice.do_num_bytes
end

#do_read(io) ⇒ Object

:nodoc:



149
150
151
152
# File 'lib/bindata/choice.rb', line 149

def do_read(io) #:nodoc:
  hook_before_do_read
  current_choice.do_read(io)
end

#do_write(io) ⇒ Object

:nodoc:



154
155
156
# File 'lib/bindata/choice.rb', line 154

def do_write(io) #:nodoc:
  current_choice.do_write(io)
end

#initialize_instanceObject



115
116
117
118
# File 'lib/bindata/choice.rb', line 115

def initialize_instance
  @choices = {}
  @last_selection = nil
end

#initialize_shared_instanceObject



107
108
109
110
111
112
113
# File 'lib/bindata/choice.rb', line 107

def initialize_shared_instance
  if eval_parameter(:copy_on_change) == true
    class << self
      alias_method :hook_after_current_choice, :copy_previous_value
    end
  end
end

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

:nodoc:

Returns:

  • (Boolean)


141
142
143
# File 'lib/bindata/choice.rb', line 141

def respond_to?(symbol, include_private = false) #:nodoc:
  current_choice.respond_to?(symbol, include_private) || super
end

#selectionObject

A convenience method that returns the current selection.



121
122
123
# File 'lib/bindata/choice.rb', line 121

def selection
  eval_parameter(:selection)
end

#snapshotObject



137
138
139
# File 'lib/bindata/choice.rb', line 137

def snapshot
  current_choice.snapshot
end