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.
15.chr.force_encoding(BINARY).freeze
Instance Attribute Summary collapse
-
#javascript ⇒ String
The javascript code.
- #scope ⇒ Object
Class Method Summary collapse
-
.from_bson(buffer) ⇒ 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(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ String
Encode the javascript code with scope.
Methods included from JSON
Constructor Details
#initialize(javascript = "", scope = {}) ⇒ CodeWithScope
Instantiate the new code with scope.
75 76 77 78 |
# File 'lib/bson/code_with_scope.rb', line 75 def initialize(javascript = "", scope = {}) @javascript = javascript @scope = scope end |
Instance Attribute Details
#javascript ⇒ String
Returns The javascript code.
37 38 39 |
# File 'lib/bson/code_with_scope.rb', line 37 def javascript @javascript end |
Class Method Details
.from_bson(buffer) ⇒ TrueClass, FalseClass
Deserialize a code with scope from BSON.
107 108 109 110 |
# File 'lib/bson/code_with_scope.rb', line 107 def self.from_bson(buffer) buffer.get_int32 # Throw away the total length. new(buffer.get_string, ::Hash.from_bson(buffer)) end |
Instance Method Details
#==(other) ⇒ true, false
Determine if this code with scope object is equal to another object.
49 50 51 52 |
# File 'lib/bson/code_with_scope.rb', line 49 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.
62 63 64 |
# File 'lib/bson/code_with_scope.rb', line 62 def as_json(*args) { "$code" => javascript, "$scope" => scope } end |
#to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ String
Encode the javascript code with scope.
90 91 92 93 94 95 96 |
# File 'lib/bson/code_with_scope.rb', line 90 def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) position = buffer.length buffer.put_int32(0) buffer.put_string(javascript) scope.to_bson(buffer) buffer.replace_int32(position, buffer.length - position) end |