class BSON::CodeWithScope

Represents a code with scope, which is a wrapper around javascript code with variable scope provided.

@see bsonspec.org/#/specification

@since 2.0.0

Constants

BSON_TYPE

A code with scope is type 0x0F in the BSON spec.

@since 2.0.0

Attributes

javascript[R]

@!attribute javascript

@return [ String ] The javascript code.
@since 2.0.0

@!attribute scope

@return [ Hash ] The variable scope.
@since 2.0.0
scope[R]

@!attribute javascript

@return [ String ] The javascript code.
@since 2.0.0

@!attribute scope

@return [ Hash ] The variable scope.
@since 2.0.0

Public Class Methods

from_bson(buffer, **options) click to toggle source

Deserialize a code with scope from BSON.

@param [ ByteBuffer ] buffer The byte buffer.

@option options [ nil | :bson ] :mode Decoding mode to use.

@return [ TrueClass, FalseClass ] The decoded code with scope.

@see bsonspec.org/#/specification

@since 2.0.0

# File lib/bson/code_with_scope.rb, line 122
def self.from_bson(buffer, **options)
  # Code with scope has a length (?) field which is not needed for
  # decoding, but spec tests want this field validated.
  start_position = buffer.read_position
  length = buffer.get_int32
  javascript = buffer.get_string
  scope = if options.empty?
    ::Hash.from_bson(buffer)
  else
    ::Hash.from_bson(buffer, **options)
  end
  read_bytes = buffer.read_position - start_position
  if read_bytes != length
    raise Error::BSONDecodeError, "CodeWithScope invalid: claimed length #{length}, actual length #{read_bytes}"
  end
  new(javascript, scope)
end
new(javascript = "", scope = {}) click to toggle source

Instantiate the new code with scope.

@example Instantiate the code with scope.

BSON::CodeWithScope.new("this.value = name", name: "sid")

@param [ String ] javascript The javascript code. @param [ Hash ] scope The variable scope.

@since 2.0.0

# File lib/bson/code_with_scope.rb, line 88
def initialize(javascript = "", scope = {})
  @javascript = javascript
  @scope = scope
end

Public Instance Methods

==(other) click to toggle source

Determine if this code with scope object is equal to another object.

@example Check the code with scope equality.

code_with_scope == other

@param [ Object ] other The object to compare against.

@return [ true, false ] If the objects are equal.

@since 2.0.0

# File lib/bson/code_with_scope.rb, line 50
def ==(other)
  return false unless other.is_a?(CodeWithScope)
  javascript == other.javascript && scope == other.scope
end
as_extended_json(**options) click to toggle source

Converts this object to a representation directly serializable to Extended JSON (github.com/mongodb/specifications/blob/master/source/extended-json.rst).

@option opts [ nil | :relaxed | :legacy ] :mode Serialization mode

(default is canonical extended JSON)

@return [ Hash ] The extended json representation.

# File lib/bson/code_with_scope.rb, line 75
def as_extended_json(**options)
  { "$code" => javascript, "$scope" => scope.as_extended_json(**options) }
end
as_json(*args) click to toggle source

Get the code with scope as JSON hash data.

@example Get the code with scope as a JSON hash.

code_with_scope.as_json

@return [ Hash ] The code with scope as a JSON hash.

@since 2.0.0 @deprecated Use as_extended_json instead.

# File lib/bson/code_with_scope.rb, line 64
def as_json(*args)
  as_extended_json
end
to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) click to toggle source

Encode the javascript code with scope.

@example Encode the code with scope.

code_with_scope.to_bson

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

@see bsonspec.org/#/specification

@since 2.0.0

# File lib/bson/code_with_scope.rb, line 103
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
  position = buffer.length
  buffer.put_int32(0)
  buffer.put_string(javascript)
  scope.to_bson(buffer)
  buffer.replace_int32(position, buffer.length - position)
end