Class: BSON::CodeWithScope
- Inherits:
-
Object
- Object
- BSON::CodeWithScope
- 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.
Constant Summary collapse
- BSON_TYPE =
A code with scope is type 0x0F in the BSON spec.
::String.new(15.chr, encoding: BINARY).freeze
Instance Attribute Summary collapse
-
#javascript ⇒ String
The javascript code.
- #scope ⇒ Object
Class Method Summary collapse
-
.from_bson(buffer, **options) ⇒ TrueClass, FalseClass
Deserialize a code with scope from BSON.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Determine if this code with scope object is equal to another object.
-
#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).
-
#as_json(*_args) ⇒ Hash
Return a representation of the object for use in application-level JSON serialization.
-
#initialize(javascript = "", scope = {}) ⇒ CodeWithScope
constructor
Instantiate the new code with scope.
-
#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer
Encode the javascript code with scope.
Methods included from JSON
Constructor Details
#initialize(javascript = "", scope = {}) ⇒ CodeWithScope
Instantiate the new code with scope.
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
#javascript ⇒ String
Returns The javascript code.
39 40 41 |
# File 'lib/bson/code_with_scope.rb', line 39 def javascript @javascript end |
Class Method Details
.from_bson(buffer, **options) ⇒ TrueClass, FalseClass
Deserialize a code with scope from BSON.
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, **) # 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 .empty? ::Hash.from_bson(buffer) else ::Hash.from_bson(buffer, **) 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.
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).
73 74 75 |
# File 'lib/bson/code_with_scope.rb', line 73 def as_extended_json(**) { "$code" => javascript, "$scope" => scope.as_extended_json(**) } 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.
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.
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 |