class Mongo::Protocol::CachingHash

A Hash that caches the results of to_bson.

@api private

Public Class Methods

new(hash) click to toggle source
# File lib/mongo/protocol/caching_hash.rb, line 26
def initialize(hash)
  @hash = hash
end

Public Instance Methods

bson_type() click to toggle source
# 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) click to toggle source

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.

@param [ BSON::ByteBuffer ] buffer The encoded BSON buffer to append to. @param [ true, false ] validating_keys Whether keys should be validated when serializing.

This option is deprecated and will not be used. It will removed in version 3.0.

@return [ BSON::ByteBuffer ] The buffer with the encoded object.

# 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