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.

Constant Summary

Constants inherited from Struct

Struct::RESERVED

Instance Attribute Summary

Attributes inherited from Base

#parent

Class Method Summary collapse

Methods inherited from Struct

#clear, #clear?, #debug_name_of, #field_names, #initialize, #method_missing, #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, #pretty_print, #read, read, #rel_offset, #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
63
# File 'lib/bindata/record.rb', line 54

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

.fieldsObject

:nodoc:



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

def fields #:nodoc:
  @fields ||= default_fields
end

.hide(*args) ⇒ Object



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

def hide(*args)
  @hide ||= default_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

:nodoc:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bindata/record.rb', line 75

def method_missing(symbol, *args) #:nodoc:
  name, params = args

  if name.is_a?(Hash)
    params = name
    name = nil
  end

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

  append_field(type, name, params)
end

.sanitize_parameters!(params, sanitizer) ⇒ Object

:nodoc:



90
91
92
93
94
95
96
# File 'lib/bindata/record.rb', line 90

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

  super(params, sanitizer)
end