lib/bindata/base.rb in bindata-0.11.1 vs lib/bindata/base.rb in bindata-1.0.0
- old
+ new
@@ -1,11 +1,10 @@
require 'bindata/io'
require 'bindata/lazy'
require 'bindata/params'
require 'bindata/registry'
require 'bindata/sanitize'
-require 'stringio'
module BinData
# Error raised when unexpected results occur when reading data from IO.
class ValidityError < StandardError ; end
@@ -68,11 +67,11 @@
@accepted_parameters = AcceptedParameters.new(ancestor_params)
end
@accepted_parameters
end
- def sanitize_parameters!(params, sanitizer)
+ def sanitize_parameters!(params, sanitizer) #:nodoc:
end
#-------------
private
@@ -131,17 +130,17 @@
do_read(io)
done_read
self
end
- def do_read(io)
+ def do_read(io) #:nodoc:
check_or_adjust_offset(io)
clear
_do_read(io)
end
- def done_read
+ def done_read #:nodoc:
_done_read
end
protected :do_read, :done_read
# Writes the value for this data to +io+.
@@ -151,23 +150,22 @@
do_write(io)
io.flush
self
end
- def do_write(io)
+ def do_write(io) #:nodoc:
_do_write(io)
end
protected :do_write
# Returns the number of bytes it will take to write this data.
- def num_bytes(deprecated = nil)
- num = do_num_bytes(deprecated)
- num.ceil
+ def num_bytes
+ do_num_bytes.ceil
end
- def do_num_bytes(deprecated = nil)
- _do_num_bytes(deprecated)
+ def do_num_bytes #:nodoc:
+ _do_num_bytes
end
protected :do_num_bytes
# Assigns the value of +val+ to this data object. Note that +val+ will
# always be deep copied to ensure no aliasing problems can occur.
@@ -180,11 +178,11 @@
_snapshot
end
# Returns the string representation of this data object.
def to_binary_s
- io = StringIO.new
+ io = BinData::IO.create_string_io
write(io)
io.rewind
io.read
end
@@ -196,10 +194,15 @@
# Return a string representing this data object.
def to_s
snapshot.to_s
end
+ # Work with Ruby's pretty-printer library.
+ def pretty_print(pp) #:nodoc:
+ pp.pp(snapshot)
+ end
+
# Returns a user friendly name of this object for debugging purposes.
def debug_name
if parent
parent.debug_name_of(self)
else
@@ -208,17 +211,26 @@
end
# Returns the offset of this object wrt to its most distant ancestor.
def offset
if parent
+ parent.offset + parent.offset_of(self)
+ else
+ 0
+ end
+ end
+
+ # Returns the offset of this object wrt to its parent.
+ def rel_offset
+ if parent
parent.offset_of(self)
else
0
end
end
- def ==(other)
+ def ==(other) #:nodoc:
# double dispatch
other == snapshot
end
#---------------
@@ -274,18 +286,18 @@
raise NotImplementedError
end
# Returns the debug name of +child+. This only needs to be implemented
# by objects that contain child objects.
- def debug_name_of(child)
- raise NotImplementedError
+ def debug_name_of(child) #:nodoc:
+ debug_name
end
# Returns the offset of +child+. This only needs to be implemented
# by objects that contain child objects.
- def offset_of(child)
- raise NotImplementedError
+ def offset_of(child) #:nodoc:
+ 0
end
# Reads the data for this data object from +io+.
def _do_read(io)
raise NotImplementedError
@@ -300,10 +312,10 @@
def _do_write(io)
raise NotImplementedError
end
# Returns the number of bytes it will take to write this data.
- def _do_num_bytes(deprecated)
+ def _do_num_bytes
raise NotImplementedError
end
# Assigns the value of +val+ to this data object. Note that +val+ will
# always be deep copied to ensure no aliasing problems can occur.