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.



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

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



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

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

#codeObject



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

def code
  @bson_code.code
end

#inspectObject



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

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

#lengthObject



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

def length
  code.length
end

#scopeObject



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

def scope
  @bson_code.scope
end

#to_bsonObject Also known as: to_bson_code



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

def to_bson
  @bson_code
end

#to_sObject



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

def to_s
  code.to_s
end