class BinData::BasePrimitive
:asserted_value
] Equavalent to :assert
and :value
.
the value just read in.
or failure. Any other return is compared to
parameter. A boolean return indicates success
made available to any lambda assigned to this
meets this criteria. The variable value
is
[:assert
] Raise an error unless the value read or assigned
IO, not the result of the :value
param.
will return the value of the data read from the
using this param. While reading, #value
Calls to #value= are ignored when
[:value
] The object will always have this value.
either #read or explicitly set with #value=.
[:initial_value
] This is the initial value to use before one is
an object. These params include those for BinData::Base as well as:
Parameters may be provided at initialisation to control the behaviour of
== Parameters
obj.read(“007”) #=> BinData::ValidityError: value not as expected
obj = BinData::Uint8.new(:check_value => lambda { value < 5 })
obj.read(“005”) #=> BinData::ValidityError: value is ‘5’ but expected ‘3’
obj = BinData::Uint8.new(:check_value => 3)
obj #=> 42
obj.assign(5)
obj #=> 42
obj = BinData::Uint8.new(:value => 42)
obj #=> 42
obj.clear
obj #=> 5
obj.assign(5)
obj #=> 42
obj = BinData::Uint8.new(:initial_value => 42)
require ‘bindata’
this object. This value can be read from or written to an IO stream.
such as as integer, float or string. Only one value can be contained by
particular binary representation. A value corresponds to a primitive type
A BinData::BasePrimitive object is a container for a value that has a
def <=>(other)
def <=>(other) snapshot <=> other end
def _value
method. This indirection is so that #snapshot can be overridden in
The unmodified value of this data object. Note that #snapshot calls this
def _value @value != nil ? @value : sensible_default() end
def assign(val)
def assign(val) raise ArgumentError, "can't set a nil value for #{debug_name}" if val.nil? raw_val = val.respond_to?(:snapshot) ? val.snapshot : val @value = begin raw_val.dup rescue TypeError # can't dup Fixnums raw_val end end
def clear? #:nodoc:
def clear? #:nodoc: @value.nil? end
def do_num_bytes #:nodoc:
def do_num_bytes #:nodoc: value_to_binary_string(_value).length end
def do_read(io) #:nodoc:
def do_read(io) #:nodoc: @value = read_and_return_value(io) end
def do_read_with_hook(io)
def do_read_with_hook(io) do_read_without_hook(io) trace_value end
def do_write(io) #:nodoc:
def do_write(io) #:nodoc: io.writebytes(value_to_binary_string(_value)) end
def eql?(other)
def eql?(other) # double dispatch other.eql?(snapshot) end
def hash
def hash snapshot.hash end
def initialize_instance
def initialize_instance @value = nil end
def initialize_shared_instance
def initialize_shared_instance if has_parameter?(:check_value) and has_parameter?(:value) warn ":check_value has been deprecated. Consider using :asserted_value instead of :check_value and :value in #{self.class}." elsif has_parameter?(:check_value) warn ":check_value has been deprecated. Use :assert instead in #{self.class}." end extend InitialValuePlugin if has_parameter?(:initial_value) extend ValuePlugin if has_parameter?(:value) extend CheckValuePlugin if has_parameter?(:check_value) extend AssertPlugin if has_parameter?(:assert) extend AssertedValuePlugin if has_parameter?(:asserted_value) super end
def method_missing(symbol, *args, &block) #:nodoc:
def method_missing(symbol, *args, &block) #:nodoc: child = snapshot if child.respond_to?(symbol) child.__send__(symbol, *args, &block) else super end end
def read_and_return_value(io)
def read_and_return_value(io) raise NotImplementedError end
def respond_to?(symbol, include_private = false) #:nodoc:
def respond_to?(symbol, include_private = false) #:nodoc: child = snapshot child.respond_to?(symbol, include_private) || super end
def sensible_default
def sensible_default raise NotImplementedError end
def snapshot
def snapshot _value end
def trace_value
def trace_value BinData::trace_message do |tracer| value_string = _value.inspect tracer.trace_obj(debug_name, value_string) end end
def turn_off_tracing
def turn_off_tracing alias_method :do_read, :do_read_without_hook end
def turn_on_tracing
def turn_on_tracing alias_method :do_read_without_hook, :do_read alias_method :do_read, :do_read_with_hook end
def value
def value snapshot end
def value=(val)
def value=(val) assign(val) end
def value_to_binary_string(val)
def value_to_binary_string(val) raise NotImplementedError end