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 :type => [:uint8, {:initial_value => :initial_element_value}],
        :initial_length => 2
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]

Constant Summary

Constants included from DSLMixin

DSLMixin::BlockParsers

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, #debug_name, #debug_name_of, #eval_parameter, #get_parameter, #has_parameter?, #inspect, #num_bytes, #offset, #offset_of, #pretty_print, #read, read, register, register_class, register_self, register_subclasses, #rel_offset, #snapshot, #to_binary_s, #to_s, #write

Methods included from AcceptedParametersMixin

included

Constructor Details

#initialize(parameters = {}, parent = nil) ⇒ Wrapper

Returns a new instance of Wrapper.



43
44
45
46
47
48
# File 'lib/bindata/wrapper.rb', line 43

def initialize(parameters = {}, parent = nil)
  super

  prototype = get_parameter(:wrapped)
  @wrapped = prototype.instantiate(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

:nodoc:



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

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

Class Method Details

.sanitize_parameters!(params, sanitizer) ⇒ Object

:nodoc:



29
30
31
32
33
34
35
36
37
38
# File 'lib/bindata/wrapper.rb', line 29

def sanitize_parameters!(params, sanitizer) #:nodoc:
  raise "no wrapped type was specified in #{self}" if field.nil?

  wrapped_type = field.type
  wrapped_params = field.params.dup

  params.move_unknown_parameters_to(wrapped_params)

  params[:wrapped] = sanitizer.create_sanitized_object_prototype(wrapped_type, wrapped_params, endian)
end

Instance Method Details

#clearObject

:nodoc:



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

def clear #:nodoc:
  wrapped.clear
end

#clear?Boolean

:nodoc:

Returns:

  • (Boolean)


54
55
56
# File 'lib/bindata/wrapper.rb', line 54

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

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

:nodoc:

Returns:

  • (Boolean)


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

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