Class: BSON::Code

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

Overview

JavaScript code to be evaluated by MongoDB.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, scope = {}) ⇒ Code

Wrap code to be evaluated by MongoDB.

Parameters:

  • code (String)

    the JavaScript code.

  • a (Hash)

    document mapping identifiers to values, which represent the scope in which the code is to be executed.


28
29
30
31
32
33
34
35
# File 'lib/bson/types/code.rb', line 28

def initialize(code, scope={})
  @code  = code
  @scope = scope

  unless @code.is_a?(String)
    raise ArgumentError, "BSON::Code must be in the form of a String; #{@code.class} is not allowed."
  end
end

Instance Attribute Details

#codeObject

Hash mapping identifiers to their values


21
22
23
# File 'lib/bson/types/code.rb', line 21

def code
  @code
end

#scopeObject

Hash mapping identifiers to their values


21
22
23
# File 'lib/bson/types/code.rb', line 21

def scope
  @scope
end

Instance Method Details

#==(other) ⇒ Object


41
42
43
44
# File 'lib/bson/types/code.rb', line 41

def ==(other)
  self.class == other.class &&
    @code == other.code && @scope == other.scope
end

#inspectObject


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

def inspect
  "<BSON::Code:#{object_id} @data=\"#{@code}\" @scope=\"#{@scope.inspect}\">"
end

#lengthObject


37
38
39
# File 'lib/bson/types/code.rb', line 37

def length
  @code.length
end

#to_bson_codeObject


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

def to_bson_code
  self
end