Class: BSON::CodeWithScope

Inherits:
Object
  • Object
show all
Includes:
Encodable, JSON
Defined in:
lib/bson/code_with_scope.rb

Overview

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

See Also:

Since:

  • 2.0.0

Constant Summary collapse

BSON_TYPE =

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

Since:

  • 2.0.0

15.chr.force_encoding(BINARY).freeze

Constants included from Encodable

Encodable::BSON_ADJUST, Encodable::PLACEHOLDER, Encodable::STRING_ADJUST

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JSON

#to_json

Methods included from Encodable

#encode_binary_data_with_placeholder, #encode_with_placeholder_and_null

Constructor Details

#initialize(javascript = "", scope = {}) ⇒ CodeWithScope

Instantiate the new code with scope.

Examples:

Instantiate the code with scope.

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

Parameters:

  • javascript (String) (defaults to: "")

    The javascript code.

  • scope (Hash) (defaults to: {})

    The variable scope.

Since:

  • 2.0.0



76
77
78
79
# File 'lib/bson/code_with_scope.rb', line 76

def initialize(javascript = "", scope = {})
  @javascript = javascript
  @scope = scope
end

Instance Attribute Details

#javascriptString

Returns The javascript code.

Returns:

  • (String)

    The javascript code.

Since:

  • 2.0.0



38
39
40
# File 'lib/bson/code_with_scope.rb', line 38

def javascript
  @javascript
end

#scopeObject

Since:

  • 2.0.0



38
# File 'lib/bson/code_with_scope.rb', line 38

attr_reader :javascript, :scope

Class Method Details

.from_bson(bson) ⇒ TrueClass, FalseClass

Deserialize a code with scope from BSON.

Parameters:

  • bson (BSON)

    The encoded code with scope.

Returns:

See Also:

Since:

  • 2.0.0



110
111
112
113
# 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

Instance Method Details

#==(other) ⇒ true, false

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

Examples:

Check the code with scope equality.

code_with_scope == other

Parameters:

  • other (Object)

    The object to compare against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.0.0



50
51
52
53
# 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_json(*args) ⇒ Hash

Get the code with scope as JSON hash data.

Examples:

Get the code with scope as a JSON hash.

code_with_scope.as_json

Returns:

  • (Hash)

    The code with scope as a JSON hash.

Since:

  • 2.0.0



63
64
65
# File 'lib/bson/code_with_scope.rb', line 63

def as_json(*args)
  { "$code" => javascript, "$scope" => scope }
end

#to_bson(encoded = ''.force_encoding(BINARY)) ⇒ String

Encode the javascript code with scope.

Examples:

Encode the code with scope.

code_with_scope.to_bson

Returns:

  • (String)

    The encoded string.

See Also:

Since:

  • 2.0.0



91
92
93
94
95
96
97
98
99
# 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