Class: BinData::BasePrimitive

Inherits:
Base
  • Object
show all
Defined in:
lib/bindata/base_primitive.rb

Overview

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.value #=> 42
obj.value = 5
obj.value #=> 5
obj.clear
obj.value #=> 42

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

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

obj = BinData::Uint8.new(:check_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. In the interval between calls to #do_read and #done_read, #value will return the value of the data read from the IO, not the result of the :value param.

:check_value

Raise an error unless the value read in 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.

Direct Known Subclasses

DoubleBe, DoubleLe, FloatBe, FloatLe, Int8, Primitive, Rest, String, Stringz, Uint8

Instance Attribute Summary

Attributes inherited from Base

#parent

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, #read, read, sanitize_parameters!, #single_value?, #snapshot, #to_binary_s, #to_s, #write

Constructor Details

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

Returns a new instance of BasePrimitive.



54
55
56
57
58
59
# File 'lib/bindata/base_primitive.rb', line 54

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

  @value   = nil
  @in_read = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/bindata/base_primitive.rb', line 84

def method_missing(symbol, *args, &block)
  if value.respond_to?(symbol)
    value.__send__(symbol, *args, &block)
  else
    super
  end
end

Instance Method Details

#clearObject



61
62
63
64
# File 'lib/bindata/base_primitive.rb', line 61

def clear
  @value = nil
  @in_read = false
end

#clear?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/bindata/base_primitive.rb', line 66

def clear?
  @value.nil?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  # double dispatch
  other.eql?(snapshot)
end

#hashObject



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

def hash
  snapshot.hash
end

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

Returns:

  • (Boolean)


80
81
82
# File 'lib/bindata/base_primitive.rb', line 80

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

#valueObject



70
71
72
73
# File 'lib/bindata/base_primitive.rb', line 70

def value
  # TODO: warn "#value is deprecated, use #snapshot instead"
  snapshot
end

#value=(val) ⇒ Object



75
76
77
78
# File 'lib/bindata/base_primitive.rb', line 75

def value=(val)
  # TODO: warn "#value= is deprecated, use #assign instead"
  assign(val)
end