Class: BinData::Choice
- 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, with the exception of :default. :default is to be used when then :selection does not exist in the :choices hash.
: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
Class Method Summary collapse
-
.sanitize_parameters!(params) ⇒ Object
:nodoc:.
- .turn_off_tracing ⇒ Object
- .turn_on_tracing ⇒ Object
Instance Method Summary collapse
- #do_read_with_hook(io) ⇒ Object
- #initialize_instance ⇒ Object
- #initialize_shared_instance ⇒ Object
-
#method_missing(symbol, *args, &block) ⇒ Object
:nodoc:.
-
#respond_to?(symbol, include_private = false) ⇒ Boolean
:nodoc:.
-
#safe_respond_to?(symbol, include_private = false) ⇒ Boolean
:nodoc:.
-
#selection ⇒ Object
Returns the current selection.
- #trace_selection ⇒ Object
Methods included from DSLMixin
Methods inherited from Base
#==, #=~, #abs_offset, arg_extractor, bindata_name, #clear, #debug_name, #eval_parameter, #get_parameter, #has_parameter?, #initialize_with_warning, #inspect, #lazy_evaluator, #new, #num_bytes, #offset, #pretty_print, #read, read, register_subclasses, #rel_offset, #to_binary_s, #to_s, unregister_self, #write
Methods included from AcceptedParametersPlugin
#accepted_parameters, #default_parameters, #mandatory_parameters, #mutually_exclusive_parameters, #optional_parameters
Methods included from RegisterNamePlugin
Methods included from CheckOrAdjustOffsetPlugin
Methods included from Framework
#assign, #clear?, #debug_name_of, included, #offset_of, #snapshot
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
:nodoc:
136 137 138 |
# File 'lib/bindata/choice.rb', line 136 def method_missing(symbol, *args, &block) #:nodoc: current_choice.__send__(symbol, *args, &block) end |
Class Method Details
.sanitize_parameters!(params) ⇒ Object
:nodoc:
70 71 72 73 74 75 76 77 78 |
# File 'lib/bindata/choice.rb', line 70 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_tracing ⇒ Object
77 78 79 |
# File 'lib/bindata/trace.rb', line 77 def turn_off_tracing alias_method :do_read, :do_read_without_hook end |
.turn_on_tracing ⇒ Object
72 73 74 75 |
# File 'lib/bindata/trace.rb', line 72 def turn_on_tracing alias_method :do_read_without_hook, :do_read alias_method :do_read, :do_read_with_hook end |
Instance Method Details
#do_read_with_hook(io) ⇒ Object
82 83 84 85 |
# File 'lib/bindata/trace.rb', line 82 def do_read_with_hook(io) trace_selection do_read_without_hook(io) end |
#initialize_instance ⇒ Object
114 115 116 117 |
# File 'lib/bindata/choice.rb', line 114 def initialize_instance @choices = {} @last_selection = nil end |
#initialize_shared_instance ⇒ Object
109 110 111 112 |
# File 'lib/bindata/choice.rb', line 109 def initialize_shared_instance extend CopyOnChangePlugin if eval_parameter(:copy_on_change) == true super end |
#respond_to?(symbol, include_private = false) ⇒ Boolean
:nodoc:
132 133 134 |
# File 'lib/bindata/choice.rb', line 132 def respond_to?(symbol, include_private = false) #:nodoc: current_choice.respond_to?(symbol, include_private) || super end |
#safe_respond_to?(symbol, include_private = false) ⇒ Boolean
:nodoc:
128 129 130 |
# File 'lib/bindata/choice.rb', line 128 def safe_respond_to?(symbol, include_private = false) #:nodoc: base_respond_to?(symbol, include_private) end |
#selection ⇒ Object
Returns the current selection.
120 121 122 123 124 125 126 |
# File 'lib/bindata/choice.rb', line 120 def selection selection = eval_parameter(:selection) if selection.nil? raise IndexError, ":selection returned nil for #{debug_name}" end selection end |
#trace_selection ⇒ Object
87 88 89 90 91 92 |
# File 'lib/bindata/trace.rb', line 87 def trace_selection BinData:: do |tracer| selection_string = eval_parameter(:selection).inspect tracer.trace_obj("#{debug_name}-selection-", selection_string) end end |