Class: Moped::BSON::Binary

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

Overview

Represents binary data in the BSON specification.

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, data) ⇒ Binary

Create the new binary type.

Examples:

Create the new binary.

Moped::BSON::Binary.new(:md5, data)

Parameters:

  • type (Symbol)

    The type of data. Should be one of :generic, :function, :old, :uuid, :md5, :user

  • data (Object)

    The binary data.

Since:

  • 1.0.0



83
84
85
86
# File 'lib/moped/bson/binary.rb', line 83

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.__bson_load__(io) ⇒ Binary

Load the BSON from the raw data to a binary.

Examples:

Load the raw data.

Moped::BSON::Binary.__bson_load__(data)

Parameters:

  • io (String)

    The raw bytes of data.

Returns:

  • (Binary)

    The binary object.

Since:

  • 1.0.0



124
125
126
127
128
129
130
131
132
133
# File 'lib/moped/bson/binary.rb', line 124

def __bson_load__(io)
  length, = io.read(4).unpack(INT32_PACK)
  type = SUBTYPE_TYPES[io.read(1)]
  if type == :old
    length -= 4
    io.read(4)
  end
  data = io.read(length)
  new(type, data)
end

Instance Method Details

#==(other) ⇒ true, false Also known as: eql?

Check equality on the object.

Examples:

Check equality.

object == other

Parameters:

  • other (Object)

    The object to check against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 1.0.0



56
57
58
# File 'lib/moped/bson/binary.rb', line 56

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

#__bson_dump__(io, key) ⇒ Object

Dump the binary into it’s raw bytes.

Examples:

Dump the binary to raw bytes.

binary.__bson_dump__(string, "data")

Parameters:

  • io (String)

    The raw bytes to write to.

  • key (String)

    The field name.

Since:

  • 1.0.0



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/moped/bson/binary.rb', line 29

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

#hashFixnum

Gets the hash code for the object.

Examples:

Get the hash code.

object.hash

Returns:

  • (Fixnum)

    The hash code.

Since:

  • 1.0.0



69
70
71
# File 'lib/moped/bson/binary.rb', line 69

def hash
  [data, type].hash
end

#inspectString

Gets the string inspection for the object.

Examples:

Get the string inspection.

object.inspect

Returns:

  • (String)

    The inspection.

Since:

  • 1.0.0



96
97
98
# File 'lib/moped/bson/binary.rb', line 96

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

#to_sString

Get the string representation of the object.

Examples:

Get the string representation.

object.to_s

Returns:

  • (String)

    The string representation.

Since:

  • 1.0.0



108
109
110
# File 'lib/moped/bson/binary.rb', line 108

def to_s
  data.to_s
end