Class: BinData::Wrapper

Inherits:
Base
  • Object
show all
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]

Instance Attribute Summary

Attributes inherited from Base

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

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

Returns a new instance of Wrapper.



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

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

  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



104
105
106
# File 'lib/bindata/wrapper.rb', line 104

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

Class Method Details

.endian(endian = nil) ⇒ Object



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

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

.inherited(subclass) ⇒ Object

:nodoc:



25
26
27
28
# File 'lib/bindata/wrapper.rb', line 25

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

.method_missing(symbol, *args) ⇒ Object



40
41
42
43
44
45
# File 'lib/bindata/wrapper.rb', line 40

def method_missing(symbol, *args)
  type = symbol
  params = args.length == 0 ? {} : args[0]

  set_wrapped(type, params)
end

.sanitize_parameters!(params, sanitizer) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/bindata/wrapper.rb', line 47

def sanitize_parameters!(params, sanitizer)
  raise "Nothing to wrap" unless defined? @wrapped

  wrapped_type, wrapped_params = @wrapped
  wrapped_params = wrapped_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



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

def clear
  wrapped.clear
end

#clear?Boolean

Returns:

  • (Boolean)


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

def clear?
  wrapped.clear?
end

#debug_name_of(child) ⇒ Object



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

def debug_name_of(child)
  debug_name
end

#offset_of(child) ⇒ Object



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

def offset_of(child)
  offset
end

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

Returns:

  • (Boolean)


100
101
102
# File 'lib/bindata/wrapper.rb', line 100

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