Class: Mongo::Protocol::CachingHash Private

Inherits:
Object
  • Object
show all
Defined in:
build/ruby-driver-v2.18/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 'build/ruby-driver-v2.18/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 'build/ruby-driver-v2.18/lib/mongo/protocol/caching_hash.rb', line 30

def bson_type
  Hash::BSON_TYPE
end

#to_bson(buffer = BSON::ByteBuffer.new, validating_keys = BSON::Config.validating_keys?) ⇒ 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: BSON::Config.validating_keys?)

    Whether keys should be validated when serializing.

Returns:

  • (BSON::ByteBuffer)

    The buffer with the encoded object.



43
44
45
46
47
48
49
50
51
52
# File 'build/ruby-driver-v2.18/lib/mongo/protocol/caching_hash.rb', line 43

def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = BSON::Config.validating_keys?)
  if !@bytes
    @bytes = @hash.to_bson(BSON::ByteBuffer.new, validating_keys).to_s
  elsif needs_validation?(validating_keys)
    @validated = true
    return @hash.to_bson(buffer, validating_keys)
  end
  @validated ||= validating_keys
  buffer.put_bytes(@bytes)
end