Class: Moped::BSON::Binary

Inherits:
Object show all
Defined in:
lib/moped/bson/binary.rb

Constant Summary collapse

SUBTYPE_MAP =
{
  generic:  "\x00",
  function: "\x01",
  old:      "\x02",
  uuid:     "\x03",
  md5:      "\x05",
  user:     "\x80"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, data) ⇒ Binary

Returns a new instance of Binary.



16
17
18
19
# File 'lib/moped/bson/binary.rb', line 16

def initialize(type, data)
  @type = type
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



14
15
16
# File 'lib/moped/bson/binary.rb', line 14

def data
  @data
end

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/moped/bson/binary.rb', line 14

def type
  @type
end

Class Method Details

.__bson_load__(io) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/moped/bson/binary.rb', line 22

def __bson_load__(io)
  length, = io.read(4).unpack(INT32_PACK)
  type = SUBTYPE_MAP.invert[io.read(1)]

  if type == :old
    length -= 4
    io.read(4)
  end

  data = io.read length
  new(type, data)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



36
37
38
# File 'lib/moped/bson/binary.rb', line 36

def ==(other)
  BSON::Binary === other && data == other.data && type == other.type
end

#__bson_dump__(io, key) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/moped/bson/binary.rb', line 45

def __bson_dump__(io, key)
  io << Types::BINARY
  io << key
  io << NULL_BYTE

  if type == :old
    io << [data.bytesize + 4].pack(INT32_PACK)
    io << SUBTYPE_MAP[type]
    io << [data.bytesize].pack(INT32_PACK)
    io << data
  else
    io << [data.bytesize].pack(INT32_PACK)
    io << SUBTYPE_MAP[type]
    io << data
  end
end

#hashObject



41
42
43
# File 'lib/moped/bson/binary.rb', line 41

def hash
  [data, type].hash
end

#inspectObject



62
63
64
# File 'lib/moped/bson/binary.rb', line 62

def inspect
  "#<#{self.class.name} type=#{type.inspect} length=#{data.bytesize}>"
end

#to_sObject



66
67
68
# File 'lib/moped/bson/binary.rb', line 66

def to_s
  data.to_s
end