Object
Represents a code with scope, which is a wrapper around javascript code with variable scope provided.
@see bsonspec.org/#/specification
@since 2.0.0
Deserialize a code with scope from BSON.
@param [ BSON ] bson The encoded code with scope.
@return [ TrueClass, FalseClass ] The decoded code with scope.
@see bsonspec.org/#/specification
@since 2.0.0
# File lib/bson/code_with_scope.rb, line 110 def self.from_bson(bson) bson.read(4) # Throw away the total length. new(bson.read(Int32.from_bson(bson)).from_bson_string.chop!, ::Hash.from_bson(bson)) end
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 76 def initialize(javascript = "", scope = {}) @javascript = javascript @scope = scope end
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
Encode the javascript code with scope.
@example Encode the code with scope.
code_with_scope.to_bson
@return [ String ] The encoded string.
@see bsonspec.org/#/specification
@since 2.0.0
# File lib/bson/code_with_scope.rb, line 91 def to_bson(encoded = ''.force_encoding(BINARY)) # -1 because we are removing an extra byte out = encode_with_placeholder_and_null(BSON_ADJUST - 1, encoded) do |encoded| javascript.to_bson(encoded) scope.to_bson(encoded) end # an extra null byte has been added; we must remove it out.chop! end
Generated with the Darkfish Rdoc Generator 2.