Class: Moped::BSON::Code

Inherits:
Object show all
Defined in:
lib/moped/bson/code.rb

Overview

Object representation of a javascript expression.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, scope = nil) ⇒ Code

Create the new code type.

Examples:

Create the new code.

Moped::BSON::Code.new("this.value = param", param: "test")

Parameters:

  • code (String)

    The javascript code.

  • scope (Object) (defaults to: nil)

    The scoped variables and values.

Since:

  • 1.0.0



76
77
78
79
# File 'lib/moped/bson/code.rb', line 76

def initialize(code, scope = nil)
  @code = code
  @scope = scope
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



7
8
9
# File 'lib/moped/bson/code.rb', line 7

def code
  @code
end

#scopeObject (readonly)

Returns the value of attribute scope.



7
8
9
# File 'lib/moped/bson/code.rb', line 7

def scope
  @scope
end

Class Method Details

.__bson_load__(io) ⇒ Code

Load the BSON from the raw data to a code.

Examples:

Load the raw data.

Moped::BSON::Code.__bson_load__(data)

Parameters:

  • io (String)

    The raw bytes of data.

Returns:

  • (Code)

    The code object.

Since:

  • 1.0.0



105
106
107
108
# File 'lib/moped/bson/code.rb', line 105

def __bson_load__(io)
  code = io.read(*io.read(4).unpack(INT32_PACK)).from_utf8_binary.chop!
  new(code)
end

Instance Method Details

#==(other) ⇒ true, false Also known as: eql?

Check equality on the object.

Examples:

Check equality.

object == other

Parameters:

  • other (Object)

    The object to check against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 1.0.0



50
51
52
# File 'lib/moped/bson/code.rb', line 50

def ==(other)
  BSON::Code === other && code == other.code && scope == other.scope
end

#__bson_dump__(io, key) ⇒ Object

Dump the code into it’s raw bytes.

Examples:

Dump the code to raw bytes.

code.__bson_dump__(string, "expression")

Parameters:

  • io (String)

    The raw bytes to write to.

  • key (String)

    The field name.

Since:

  • 1.0.0



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/moped/bson/code.rb', line 18

def __bson_dump__(io, key)
  if scoped?
    io << Types::CODE_WITH_SCOPE
    io << key.to_bson_cstring
    code_start = io.bytesize
    io << START_LENGTH
    data = code.to_utf8_binary
    io << [data.bytesize+1].pack(INT32_PACK)
    io << data
    io << NULL_BYTE
    scope.__bson_dump__(io)
    io[code_start, 4] = [io.bytesize - code_start].pack(INT32_PACK)
  else
    io << Types::CODE
    io << key.to_bson_cstring
    data = code.to_utf8_binary
    io << [data.bytesize+1].pack(INT32_PACK)
    io << data
    io << NULL_BYTE
  end
end

#hashFixnum

Gets the hash code for the object.

Examples:

Get the hash code.

object.hash

Returns:

  • (Fixnum)

    The hash code.

Since:

  • 1.0.0



63
64
65
# File 'lib/moped/bson/code.rb', line 63

def hash
  [code, scope].hash
end

#scoped?true, false

Is the code scoped?

Examples:

Is the code scoped?

code.scoped?

Returns:

  • (true, false)

    If the code is scoped.

Since:

  • 1.0.0



89
90
91
# File 'lib/moped/bson/code.rb', line 89

def scoped?
  !!scope
end