Class: BinData::Wrapper

Inherits:
Base
  • Object
show all
Includes:
DSLMixin
Defined in:
lib/bindata/wrapper.rb

Overview

A Wrapper allows the creation of new BinData types that provide default parameters.

require 'bindata'

class Uint8Array < BinData::Wrapper
  default_parameter :initial_element_value => 0

  array :initial_length => 2 do
    uint8 :initial_value => :initial_element_value
  end
end

arr = Uint8Array.new
arr.snapshot #=> [0, 0]

arr = Uint8Array.new(:initial_length => 5, :initial_element_value => 3)
arr.snapshot #=> [3, 3, 3, 3 ,3]

Instance Attribute Summary

Attributes inherited from Base

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSLMixin

included

Methods inherited from Base

#==, #_assign, #_do_num_bytes, #_do_read, #_do_write, #_snapshot, arg_extractor, bindata_name, #debug_name, #debug_name_of, #eval_parameter, #get_parameter, #has_parameter?, #initialize_with_deprecation, #inspect, #new, #num_bytes, #offset, #offset_of, #pretty_print, #read, read, register, register_self, register_subclasses, #rel_offset, #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

#method_missing(symbol, *args, &block) ⇒ Object

:nodoc:



83
84
85
# File 'lib/bindata/wrapper.rb', line 83

def method_missing(symbol, *args, &block) #:nodoc:
  @wrapped.__send__(symbol, *args, &block)
end

Class Method Details

.sanitize_parameters!(params) ⇒ Object

:nodoc:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bindata/wrapper.rb', line 40

def sanitize_parameters!(params) #:nodoc:
  raise "no wrapped type was specified in #{self}" if fields[0].nil?

  wrapped_type = fields[0].type
  wrapped_params = fields[0].params.dup

  params.move_unknown_parameters_to(wrapped_params)

  params.endian = endian unless endian.nil?
  params[:wrapped] = params.create_sanitized_object_prototype(wrapped_type, wrapped_params)

  wrapped_class = params[:wrapped].instance_variable_get(:@obj_class)
  warn "BinData::Wrapper is deprecated as of BinData 1.3.2.  #{self} should derive from #{wrapped_class}\n   See http://bindata.rubyforge.org/#extending_existing_types"
end

Instance Method Details

#assign(val) ⇒ Object



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

def assign(val)
  @wrapped.assign(val)
end

#clearObject

:nodoc:



63
64
65
# File 'lib/bindata/wrapper.rb', line 63

def clear #:nodoc:
  @wrapped.clear
end

#clear?Boolean

:nodoc:

Returns:

  • (Boolean)


67
68
69
# File 'lib/bindata/wrapper.rb', line 67

def clear? #:nodoc:
  @wrapped.clear?
end

#do_num_bytesObject

:nodoc:



95
96
97
# File 'lib/bindata/wrapper.rb', line 95

def do_num_bytes #:nodoc:
  @wrapped.do_num_bytes
end

#do_read(io) ⇒ Object

:nodoc:



87
88
89
# File 'lib/bindata/wrapper.rb', line 87

def do_read(io) #:nodoc:
  @wrapped.do_read(io)
end

#do_write(io) ⇒ Object

:nodoc



91
92
93
# File 'lib/bindata/wrapper.rb', line 91

def do_write(io) #:nodoc
  @wrapped.do_write(io)
end

#initialize_instanceObject



58
59
60
61
# File 'lib/bindata/wrapper.rb', line 58

def initialize_instance
  prototype = get_parameter(:wrapped)
  @wrapped = prototype.instantiate(nil, self)
end

#respond_to?(symbol, include_private = false) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


79
80
81
# File 'lib/bindata/wrapper.rb', line 79

def respond_to?(symbol, include_private = false) #:nodoc:
  @wrapped.respond_to?(symbol, include_private) || super
end

#snapshotObject



75
76
77
# File 'lib/bindata/wrapper.rb', line 75

def snapshot
  @wrapped.snapshot
end