Class: BSON::Code

Inherits:
Object
  • Object
show all
Includes:
JSON
Defined in:
lib/bson/code.rb

Overview

Represents a code type, which is a wrapper around javascript code.

See Also:

Since:

  • 2.0.0

Constant Summary collapse

BSON_TYPE =

A code is type 0x0D in the BSON spec.

Since:

  • 2.0.0

::String.new(13.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 = "") ⇒ Code

Instantiate the new code.

Examples:

Instantiate the new code.

BSON::Code.new("this.value = 5")

Parameters:

  • javascript (String) (defaults to: "")

    The javascript code.

Since:

  • 2.0.0



81
82
83
# File 'lib/bson/code.rb', line 81

def initialize(javascript = "")
  @javascript = javascript
end

Instance Attribute Details

#javascriptObject

Since:

  • 2.0.0



35
36
37
# File 'lib/bson/code.rb', line 35

def javascript
  @javascript
end

Class Method Details

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

Deserialize code 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



110
111
112
# File 'lib/bson/code.rb', line 110

def self.from_bson(buffer, **options)
  new(buffer.get_string)
end

Instance Method Details

#==(other) ⇒ true, false

Determine if this code object is equal to another object.

Examples:

Check the code equality.

code == other

Parameters:

  • other (Object)

    The object to compare against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.0.0



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).

Parameters:

  • opts (Hash)

    a customizable set of options

Returns:

  • (Hash)

    The extended json representation.

Since:

  • 2.0.0



69
70
71
# File 'lib/bson/code.rb', line 69

def as_extended_json(**_options)
  { "$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.

Returns:

  • (Hash)

    The extended json representation.

Since:

  • 2.0.0



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.

Examples:

Encode the code.

code.to_bson

Returns:

See Also:

Since:

  • 2.0.0



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