Class: BinData::Record

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

Overview

A Record is a declarative wrapper around Struct.

require 'bindata'

class Tuple < BinData::Record
  int8  :x
  int8  :y
  int8  :z
end

class SomeDataType < BinData::Record
  hide 'a'

  int32le :a
  int16le :b
  tuple   :s
end

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

Parameters

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

:fields

An array specifying the fields for this struct. Each element of the array is of the form [type, name, params]. Type is a symbol representing a registered type. Name is the name of this field. Params is an optional hash of parameters to pass to this field when instantiating it.

:hide

A list of the names of fields that are to be hidden from the outside world. Hidden fields don’t appear in #snapshot or #field_names but are still accessible by name.

:endian

Either :little or :big. This specifies the default endian of any numerics in this struct, or in any nested data objects.

Direct Known Subclasses

MultiValue

Constant Summary

Constants inherited from Struct

Struct::RESERVED

Instance Attribute Summary

Attributes inherited from Base

#parent

Class Method Summary collapse

Methods inherited from Struct

#_do_num_bytes, #clear, #clear?, #debug_name_of, #field_names, #initialize, #method_missing, #offset_of, #orig__do_num_bytes, #orig_clear, #orig_clear?, #orig_offset_of, #respond_to?

Methods inherited from Base

#==, accepted_parameters, #assign, #clear, #clear?, #debug_name, #debug_name_of, default_parameters, #eval_parameter, #get_parameter, #has_parameter?, #initialize, #inspect, mandatory_parameters, mutually_exclusive_parameters, #num_bytes, #offset, #offset_of, optional_parameters, #read, read, #single_value?, #snapshot, #to_binary_s, #to_s, #write

Constructor Details

This class inherits a constructor from BinData::Struct

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BinData::Struct

Class Method Details

.endian(endian = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/bindata/record.rb', line 54

def endian(endian = nil)
  @endian ||= nil
  if [:little, :big].include?(endian)
    @endian = endian
  elsif endian != nil
    raise ArgumentError, "unknown value for endian '#{endian}'", caller(1)
  end
  @endian
end

.hide(*args) ⇒ Object



64
65
66
67
68
# File 'lib/bindata/record.rb', line 64

def hide(*args)
  @hide ||= []
  @hide.concat(args.collect { |name| name.to_s })
  @hide
end

.inherited(subclass) ⇒ Object

:nodoc:



49
50
51
52
# File 'lib/bindata/record.rb', line 49

def inherited(subclass) #:nodoc:
  # Register the names of all subclasses of this class.
  register(subclass.name, subclass)
end

.method_missing(symbol, *args) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/bindata/record.rb', line 70

def method_missing(symbol, *args)
  name, params = args

  type = symbol
  name = name.to_s
  params ||= {}

  append_field(type, name, params)
end

.sanitize_parameters!(params, sanitizer) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/bindata/record.rb', line 80

def sanitize_parameters!(params, sanitizer)
  params[:fields] = fields
  params[:endian] = endian unless endian.nil?
  params[:hide]   = hide   unless hide.empty?

  super(params, sanitizer)
end