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

13.chr.force_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


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

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

Instance Attribute Details

#javascriptObject

Since:

  • 2.0.0


33
34
35
# File 'lib/bson/code.rb', line 33

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


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

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


45
46
47
48
# File 'lib/bson/code.rb', line 45

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

Parameters:

  • opts (Hash)

    a customizable set of options

Returns:

  • (Hash)

    The extended json representation.

Since:

  • 2.0.0


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

def as_extended_json(**options)
  { "$code" => javascript }
end

#as_json(*args) ⇒ Hash

Deprecated.

Use as_extended_json instead.

Get the code as JSON hash data.

Examples:

Get the code as a JSON hash.

code.as_json

Returns:

  • (Hash)

    The code as a JSON hash.

Since:

  • 2.0.0


59
60
61
# File 'lib/bson/code.rb', line 59

def as_json(*args)
  as_extended_json
end

#to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ BSON::ByteBuffer

Encode the javascript code.

Examples:

Encode the code.

code.to_bson

Returns:

See Also:

Since:

  • 2.0.0


96
97
98
# File 'lib/bson/code.rb', line 96

def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
  buffer.put_string(javascript) # @todo: was formerly to_bson_string
end