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, #field_names, #has_key?, #initialize_instance, #initialize_shared_instance, #method_missing, #offset_of, #respond_to?, #snapshot

Methods inherited from Base

#==, #_assign, #_do_num_bytes, #_do_read, #_do_write, #_snapshot, #assign, bindata_name, #clear, #clear?, #debug_name, #debug_name_of, #eval_parameter, #get_parameter, #has_parameter?, #initialize_instance, #initialize_with_deprecation, #inspect, #new, #num_bytes, #offset, #offset_of, #pretty_print, #read, read, register, register_self, register_subclasses, #rel_offset, #snapshot, #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 in the class BinData::Struct

Class Method Details

.arg_extractorObject



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

def arg_extractor
  RecordArgExtractor
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.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bindata/record.rb', line 85

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



98
99
100
101
102
103
104
105
106
107
# File 'lib/bindata/record.rb', line 98

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:



74
75
76
77
78
79
80
# File 'lib/bindata/record.rb', line 74

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

  super(params)

  define_field_accessors(params[:fields].fields)
end