Parent

Included Modules

Files

Class/Module Index [+]

Quicksearch

BinData::BasePrimitive

A BinData::BasePrimitive object is a container for a value that has a particular binary representation. A value corresponds to a primitive type such as as integer, float or string. Only one value can be contained by this object. This value can be read from or written to an IO stream.

require 'bindata'

obj = BinData::Uint8.new(:initial_value => 42)
obj #=> 42
obj.assign(5)
obj #=> 5
obj.clear
obj #=> 42

obj = BinData::Uint8.new(:value => 42)
obj #=> 42
obj.assign(5)
obj #=> 42

obj = BinData::Uint8.new(:assert_value => 3)
obj.read("\005") #=> BinData::ValidityError: value is '5' but expected '3'

obj = BinData::Uint8.new(:assert_value => lambda { value < 5 })
obj.read("\007") #=> BinData::ValidityError: value not as expected

Parameters

Parameters may be provided at initialisation to control the behaviour of an object. These params include those for BinData::Base as well as:

:initial_value

This is the initial value to use before one is either read or explicitly set with value=.

:value

The object will always have this value. Calls to value= are ignored when using this param. While reading, value will return the value of the data read from the IO, not the result of the :value param.

:assert

Raise an error unless the value read or assigned meets this criteria. The variable value is made available to any lambda assigned to this parameter. A boolean return indicates success or failure. Any other return is compared to the value just read in.

:asserted_value

Equavalent to :assert and :value.

Public Class Methods

bit_aligned() click to toggle source
# File lib/bindata/alignment.rb, line 71
def BasePrimitive.bit_aligned
  include BitAligned
end
turn_off_tracing() click to toggle source
# File lib/bindata/trace.rb, line 52
def turn_off_tracing
  alias_method :do_read, :do_read_without_hook
end
turn_on_tracing() click to toggle source
# File lib/bindata/trace.rb, line 47
def turn_on_tracing
  alias_method :do_read_without_hook, :do_read
  alias_method :do_read, :do_read_with_hook
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/bindata/base_primitive.rb, line 110
def <=>(other)
  snapshot <=> other
end
assign(val) click to toggle source
# File lib/bindata/base_primitive.rb, line 72
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
do_read_with_hook(io) click to toggle source
# File lib/bindata/trace.rb, line 57
def do_read_with_hook(io)
  do_read_without_hook(io)
  trace_value
end
eql?(other) click to toggle source
# File lib/bindata/base_primitive.rb, line 114
def eql?(other)
  # double dispatch
  other.eql?(snapshot)
end
hash() click to toggle source
# File lib/bindata/base_primitive.rb, line 119
def hash
  snapshot.hash
end
initialize_instance() click to toggle source
# File lib/bindata/base_primitive.rb, line 64
def initialize_instance
  @value = nil
end
initialize_shared_instance() click to toggle source
# File lib/bindata/base_primitive.rb, line 56
def initialize_shared_instance
  extend InitialValuePlugin  if has_parameter?(:initial_value)
  extend ValuePlugin         if has_parameter?(:value)
  extend AssertPlugin        if has_parameter?(:assert)
  extend AssertedValuePlugin if has_parameter?(:asserted_value)
  super
end
snapshot() click to toggle source
# File lib/bindata/base_primitive.rb, line 84
def snapshot
  _value
end
trace_value() click to toggle source
# File lib/bindata/trace.rb, line 62
def trace_value
  BinData::trace_message do |tracer|
    value_string = _value.inspect
    tracer.trace_obj(debug_name, value_string)
  end
end
value() click to toggle source
# File lib/bindata/base_primitive.rb, line 88
def value
  snapshot
end
value=(val) click to toggle source
# File lib/bindata/base_primitive.rb, line 92
def value=(val)
  assign(val)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.