Class: BSON::CodeWithScope

Inherits:
Object
  • Object
show all
Includes:
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

::String.new(15.chr, encoding: BINARY).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JSON

#to_json

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



86
87
88
89
# File 'lib/bson/code_with_scope.rb', line 86

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



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

def javascript
  @javascript
end

#scopeObject

Since:

  • 2.0.0



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

attr_reader :javascript, :scope

Class Method Details

.from_bson(buffer, **options) ⇒ TrueClass, FalseClass

Deserialize a code with scope from BSON.

Parameters:

  • buffer (ByteBuffer)

    The byte buffer.

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :mode (nil | :bson)

    Decoding mode to use.

Returns:

See Also:

Since:

  • 2.0.0



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bson/code_with_scope.rb', line 120

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

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



51
52
53
54
# File 'lib/bson/code_with_scope.rb', line 51

def ==(other)
  return false unless other.is_a?(CodeWithScope)
  javascript == other.javascript && scope == other.scope
end

#as_extended_json(**options) ⇒ Hash

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

Parameters:

  • opts (Hash)

    a customizable set of options

Returns:

  • (Hash)

    The extended json representation.

Since:

  • 2.0.0



73
74
75
# File 'lib/bson/code_with_scope.rb', line 73

def as_extended_json(**options)
  { "$code" => javascript, "$scope" => scope.as_extended_json(**options) }
end

#as_json(*_args) ⇒ Hash

Return a representation of the object for use in application-level JSON serialization. Since BSON::CodeWithScope is used exclusively in BSON-related contexts, this method returns the canonical Extended JSON representation.

Returns:

  • (Hash)

    The extended json representation.

Since:

  • 2.0.0



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

def as_json(*_args)
  as_extended_json
end

#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer

Encode the javascript code with scope.

Examples:

Encode the code with scope.

code_with_scope.to_bson

Returns:

See Also:

Since:

  • 2.0.0



101
102
103
104
105
106
107
# File 'lib/bson/code_with_scope.rb', line 101

def to_bson(buffer = ByteBuffer.new)
  position = buffer.length
  buffer.put_int32(0)
  buffer.put_string(javascript)
  scope.to_bson(buffer)
  buffer.replace_int32(position, buffer.length - position)
end