Class: DMAP::MeasuredInteger

Inherits:
Object
  • Object
show all
Defined in:
lib/dmap.rb

Overview

Allows people to specify a integer and the size of the binary representation required

If you create one with a wanted_box_size that’s not 1,2,4,8 you’ll run into trouble elsewhere (most likely)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, box_size = 1, signed = false) ⇒ MeasuredInteger

Returns a new instance of MeasuredInteger.



240
241
242
243
244
# File 'lib/dmap.rb', line 240

def initialize(value,box_size = 1,signed = false)
  @value = value.to_i
  @box_size = box_size
  @signed = signed
end

Instance Attribute Details

#binaryObject

Returns the value of attribute binary.



238
239
240
# File 'lib/dmap.rb', line 238

def binary
  @binary
end

#box_sizeObject

Returns the value of attribute box_size.



238
239
240
# File 'lib/dmap.rb', line 238

def box_size
  @box_size
end

#signedObject

Returns the value of attribute signed.



238
239
240
# File 'lib/dmap.rb', line 238

def signed
  @signed
end

#valueObject (readonly)

Returns the value of attribute value.



237
238
239
# File 'lib/dmap.rb', line 237

def value
  @value
end

Instance Method Details

#inspectObject



259
260
261
# File 'lib/dmap.rb', line 259

def inspect
  @value
end

#pack_codeObject



255
256
257
# File 'lib/dmap.rb', line 255

def pack_code
  {1=>"C",-1=>"c",2=>"n",4=>"N",8=>"Q",-8=>"q"}[@box_size * (@signed ? -1 : 1)] # FIXME: pack codes for all signed cases
end

#to_dmapObject

TODO: Tidy me



246
247
248
249
250
251
252
253
# File 'lib/dmap.rb', line 246

def to_dmap # TODO: Tidy me
  case @box_size
  when 1,2,4,8
    [@box_size,@value].pack("N"<<pack_code)
  else
    raise "I don't know how to unpack an integer #{@box_size} bytes long"
  end
end