Class: Mongo::Protocol::CachingHash Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/protocol/caching_hash.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A Hash that caches the results of #to_bson.

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ CachingHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of CachingHash.



26
27
28
# File 'lib/mongo/protocol/caching_hash.rb', line 26

def initialize(hash)
  @hash = hash
end

Instance Method Details

#bson_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
# File 'lib/mongo/protocol/caching_hash.rb', line 30

def bson_type
  Hash::BSON_TYPE
end

#to_bson(buffer = BSON::ByteBuffer.new, validating_keys = nil) ⇒ BSON::ByteBuffer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Caches the result of to_bson and writes it to the given buffer on subsequent calls to this method. If this method is originally called without validation, and then is subsequently called with validation, we will want to recalculate the to_bson to trigger the validations.

Parameters:

  • buffer (BSON::ByteBuffer) (defaults to: BSON::ByteBuffer.new)

    The encoded BSON buffer to append to.

  • validating_keys (true, false) (defaults to: nil)

    Whether keys should be validated when serializing. This option is deprecated and will not be used. It will removed in version 3.0.

Returns:

  • (BSON::ByteBuffer)

    The buffer with the encoded object.



44
45
46
47
48
49
# File 'lib/mongo/protocol/caching_hash.rb', line 44

def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = nil)
  if !@bytes
    @bytes = @hash.to_bson(BSON::ByteBuffer.new).to_s
  end
  buffer.put_bytes(@bytes)
end