Module: MongoScript::Execution::ClassMethods
- Defined in:
- lib/mongoscript/execution.rb
Instance Method Summary collapse
-
#code_for(script_name) ⇒ Object
code from stored files.
- #execute(code, args = [], options = {}) ⇒ Object
-
#execute_readonly_code(code, *args) ⇒ Object
raw code note: to pass in Mongo options for the $exec call, like nolock, you need to call execute directly since otherwise we have no way to tell Mongo options from an argument list ending in a JS hash.
- #execute_readonly_routine(script_name, *args) ⇒ Object
- #execute_readwrite_code(code, *args) ⇒ Object
- #execute_readwrite_routine(script_name, *args) ⇒ Object
Instance Method Details
#code_for(script_name) ⇒ Object
code from stored files
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mongoscript/execution.rb', line 42 def code_for(script_name) script_name = script_name.to_s dir = @script_dirs.find {|d| File.exists?(File.join(d, "#{script_name}.js"))} raise ScriptNotFound, "Unable to find script #{script_name}" unless dir code = File.read(File.join(dir, "#{script_name}.js")) LOADED_SCRIPTS[script_name] ||= code # for future extension # code_hash = Digest::MD5.hex_digest(code) # LOADED_SCRIPTS[script_name] = { # :code => code, # :hash => code_hash, # :installed => false # } # code end |
#execute(code, args = [], options = {}) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/mongoscript/execution.rb', line 80 def execute(code, args = [], = {}) # see http://mrdanadams.com/2011/mongodb-eval-ruby-driver/ result = MongoScript.database.command({:$eval => code, args: resolve_arguments(args)}.merge()) unless result["ok"] == 1.0 raise ExecutionFailure, "MongoScript.execute JS didn't return {ok: 1.0}! Result: #{result.inspect}" end result["retval"] end |
#execute_readonly_code(code, *args) ⇒ Object
raw code note: to pass in Mongo options for the $exec call, like nolock, you need to call execute directly since otherwise we have no way to tell Mongo options from an argument list ending in a JS hash
70 71 72 73 74 |
# File 'lib/mongoscript/execution.rb', line 70 def execute_readonly_code(code, *args) # for readonly operations, set nolock to true to improve concurrency # http://www.mongodb.org/display/DOCS/Server-side+Code+Execution#Server-sideCodeExecution-NotesonConcurrency execute(code, args, {:nolock => true}) end |
#execute_readonly_routine(script_name, *args) ⇒ Object
59 60 61 |
# File 'lib/mongoscript/execution.rb', line 59 def execute_readonly_routine(script_name, *args) execute_readonly_code(code_for(script_name), *args) end |
#execute_readwrite_code(code, *args) ⇒ Object
76 77 78 |
# File 'lib/mongoscript/execution.rb', line 76 def execute_readwrite_code(code, *args) execute(code, args) end |
#execute_readwrite_routine(script_name, *args) ⇒ Object
63 64 65 |
# File 'lib/mongoscript/execution.rb', line 63 def execute_readwrite_routine(script_name, *args) execute_readwrite_code(code_for(script_name), *args) end |