Class: BinData::Record

Inherits:
Struct show all
Includes:
DSLMixin
Defined in:
lib/bindata/record.rb

Overview

A Record is a declarative wrapper around Struct.

See Struct for more info.

Constant Summary

Constants inherited from Struct

Struct::RESERVED

Instance Attribute Summary

Attributes inherited from Base

#parent

Class Method Summary collapse

Methods included from DSLMixin

included

Methods inherited from Struct

#[], #[]=, #assign, #clear, #clear?, #debug_name_of, #do_num_bytes, #do_read, #do_write, #each_pair, #field_names, #has_key?, #initialize_instance, #initialize_shared_instance, #method_missing, #offset_of, #respond_to?, #snapshot

Methods inherited from Base

#==, #=~, #abs_offset, bindata_name, #clear, #debug_name, #eval_parameter, #get_parameter, #has_parameter?, #initialize_instance, #initialize_with_warning, #inspect, #lazy_evaluator, #new, #num_bytes, #offset, #pretty_print, #read, read, register_subclasses, #rel_offset, #safe_respond_to?, #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

included, #initialize_shared_instance

Methods included from CheckOrAdjustOffsetPlugin

included, #initialize_shared_instance

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 in the class BinData::Struct

Class Method Details

.arg_extractorObject



16
17
18
# File 'lib/bindata/record.rb', line 16

def arg_extractor
  MultiFieldArgExtractor
end

.define_field_accessors(fields) ⇒ Object

Defines accessor methods to avoid the overhead of going through Struct#method_missing. This is purely a speed optimisation. Removing this method will not have any effect on correctness.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bindata/record.rb', line 31

def define_field_accessors(fields) #:nodoc:
  unless method_defined?(:bindata_defined_accessors_for_fields?)
    fields.each_with_index do |field, i|
      name = field.name_as_sym
      if name
        define_field_accessors_for(name, i)
      end
    end

    define_method(:bindata_defined_accessors_for_fields?) { true }
  end
end

.define_field_accessors_for(name, index) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/bindata/record.rb', line 44

def define_field_accessors_for(name, index)
  define_method(name) do
    instantiate_obj_at(index) unless @field_objs[index]
    @field_objs[index]
  end
  define_method(name.to_s + "=") do |*vals|
    instantiate_obj_at(index) unless @field_objs[index]
    @field_objs[index].assign(*vals)
  end
end

.sanitize_parameters!(params) ⇒ Object

:nodoc:



20
21
22
23
24
25
26
# File 'lib/bindata/record.rb', line 20

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

  super(params)

  define_field_accessors(params[:fields].fields)
end