Class: BSON::Code
Overview
Represents a code type, which is a wrapper around javascript code.
Constant Summary collapse
- BSON_TYPE =
A code is type 0x0D in the BSON spec.
::String.new(13.chr, encoding: BINARY).freeze
Instance Attribute Summary collapse
Class Method Summary collapse
-
.from_bson(buffer, **options) ⇒ TrueClass, FalseClass
Deserialize code from BSON.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Determine if this code 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/extended-json.md).
-
#as_json(*_args) ⇒ Hash
Return a representation of the object for use in application-level JSON serialization.
-
#initialize(javascript = "") ⇒ Code
constructor
Instantiate the new code.
-
#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer
Encode the javascript code.
Methods included from JSON
Constructor Details
#initialize(javascript = "") ⇒ Code
Instantiate the new code.
81 82 83 |
# File 'lib/bson/code.rb', line 81 def initialize(javascript = "") @javascript = javascript end |
Instance Attribute Details
Class Method Details
.from_bson(buffer, **options) ⇒ TrueClass, FalseClass
Deserialize code from BSON.
110 111 112 |
# File 'lib/bson/code.rb', line 110 def self.from_bson(buffer, **) new(buffer.get_string) end |
Instance Method Details
#==(other) ⇒ true, false
Determine if this code object is equal to another object.
47 48 49 50 |
# File 'lib/bson/code.rb', line 47 def ==(other) return false unless other.is_a?(Code) javascript == other.javascript 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/extended-json.md).
69 70 71 |
# File 'lib/bson/code.rb', line 69 def as_extended_json(**) { "$code" => javascript } end |
#as_json(*_args) ⇒ Hash
Return a representation of the object for use in application-level JSON serialization. Since BSON::Code is used exclusively in BSON-related contexts, this method returns the canonical Extended JSON representation.
58 59 60 |
# File 'lib/bson/code.rb', line 58 def as_json(*_args) as_extended_json end |
#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer
Encode the javascript code.
95 96 97 |
# File 'lib/bson/code.rb', line 95 def to_bson(buffer = ByteBuffer.new) buffer.put_string(javascript) # @todo: was formerly to_bson_string end |