Class: BSON::Code

Inherits:
Object show all
Defined in:
lib/jmongo/mongo/bson.rb

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.



70
71
72
73
74
75
# File 'lib/jmongo/mongo/bson.rb', line 70

def initialize(code, scope={})
  unless code.is_a?(String)
    raise ArgumentError, "BSON::Code must be in the form of a String; #{code.class} is not accepted."
  end
  @bson_code  = BsonCode.new(code, scope.to_bson)
end

Instance Method Details

#==(other) ⇒ Object



89
90
91
92
# File 'lib/jmongo/mongo/bson.rb', line 89

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

#codeObject



77
78
79
# File 'lib/jmongo/mongo/bson.rb', line 77

def code
  @bson_code.code
end

#inspectObject



94
95
96
# File 'lib/jmongo/mongo/bson.rb', line 94

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

#lengthObject



85
86
87
# File 'lib/jmongo/mongo/bson.rb', line 85

def length
  code.length
end

#scopeObject



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

def scope
  @bson_code.scope
end

#to_bsonObject Also known as: to_bson_code



102
103
104
# File 'lib/jmongo/mongo/bson.rb', line 102

def to_bson
  @bson_code
end

#to_sObject



98
99
100
# File 'lib/jmongo/mongo/bson.rb', line 98

def to_s
  code.to_s
end