Class: BSON::CodeWithScope
- Inherits:
-
Object
- Object
- BSON::CodeWithScope
- 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.
15.chr.force_encoding(BINARY).freeze
Constants included from Encodable
Encodable::BSON_ADJUST, Encodable::PLACEHOLDER, Encodable::STRING_ADJUST
Instance Attribute Summary collapse
-
#javascript ⇒ String
The javascript code.
- #scope ⇒ Object
Class Method Summary collapse
-
.from_bson(bson) ⇒ 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_json(*args) ⇒ Hash
Get the code with scope as JSON hash data.
-
#initialize(javascript = "", scope = {}) ⇒ CodeWithScope
constructor
Instantiate the new code with scope.
-
#to_bson(encoded = ''.force_encoding(BINARY)) ⇒ String
Encode the javascript code with scope.
Methods included from 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.
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
#javascript ⇒ String
Returns The javascript code.
38 39 40 |
# File 'lib/bson/code_with_scope.rb', line 38 def javascript @javascript end |
Class Method Details
.from_bson(bson) ⇒ TrueClass, FalseClass
Deserialize a code with scope from BSON.
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.
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.
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.
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 |