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, #debug_name_of, default_parameters, #eval_parameter, #get_parameter, #has_parameter?, #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

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

Returns a new instance of Wrapper.



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

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

:nodoc:



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

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

Class Method Details

.endian(endian = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# 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}' in #{self}", 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

:nodoc:



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

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

  set_wrapped(type, params)
end

.sanitize_parameters!(params, sanitizer) ⇒ Object

:nodoc:



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

def sanitize_parameters!(params, sanitizer) #:nodoc:
  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

:nodoc:



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

def clear #:nodoc:
  wrapped.clear
end

#clear?Boolean

:nodoc:

Returns:

  • (Boolean)


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

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

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

:nodoc:

Returns:

  • (Boolean)


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

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