Class: BinData::Record

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

Overview

A Record is a declarative wrapper around Struct.

require 'bindata'

class SomeDataType < BinData::Record
  hide :a

  int32le :a
  int16le :b
  struct  :s do
    int8  :x
    int8  :y
    int8  :z
  end
end

obj = SomeDataType.new
obj.field_names   =># ["b", "s"]
obj.s.field_names =># ["x", "y", "z"]

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

#==, #=~, 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



34
35
36
# File 'lib/bindata/record.rb', line 34

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.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bindata/record.rb', line 49

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



62
63
64
65
66
67
68
69
70
71
# File 'lib/bindata/record.rb', line 62

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:



38
39
40
41
42
43
44
# File 'lib/bindata/record.rb', line 38

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

  super(params)

  define_field_accessors(params[:fields].fields)
end