Class: Jamnagar::Adapters::MongoAdapter
- Defined in:
- lib/jamnagar/adapters/mongo_adapter.rb
Class Method Summary collapse
Instance Method Summary collapse
- #find(params = {}) ⇒ Object
- #find_and_modify(params = {}) ⇒ Object
- #find_first(params = {}) ⇒ Object
- #get(key) ⇒ Object
-
#initialize(driver = nil, database = "test", collection = "catchall") ⇒ MongoAdapter
constructor
A new instance of MongoAdapter.
- #store(key, value) ⇒ Object
- #update(query = {}, updates = {}) ⇒ Object
Constructor Details
#initialize(driver = nil, database = "test", collection = "catchall") ⇒ MongoAdapter
Returns a new instance of MongoAdapter.
11 12 13 14 15 |
# File 'lib/jamnagar/adapters/mongo_adapter.rb', line 11 def initialize(driver=nil, database="test", collection="catchall") @driver = driver || Jamnagar::Adapters::MongoAdapter.driver @driver.use database @collection = collection end |
Class Method Details
.driver ⇒ Object
7 8 9 |
# File 'lib/jamnagar/adapters/mongo_adapter.rb', line 7 def self.driver @@driver ||= Moped::Session.new(["127.0.0.1:27017"]) end |
Instance Method Details
#find(params = {}) ⇒ Object
29 30 31 32 33 |
# File 'lib/jamnagar/adapters/mongo_adapter.rb', line 29 def find(params={}) @driver.with(safe: { wtimeout: 5 }) do |_session| _session[@collection.to_sym].find(params) end end |
#find_and_modify(params = {}) ⇒ Object
47 48 49 50 51 |
# File 'lib/jamnagar/adapters/mongo_adapter.rb', line 47 def find_and_modify(params={}) @driver.with(safe: { wtimeout: 5 }) do |_session| _session[@collection.to_sym].find(params[:query]).update(params[:update]) end end |
#find_first(params = {}) ⇒ Object
35 36 37 38 39 |
# File 'lib/jamnagar/adapters/mongo_adapter.rb', line 35 def find_first(params={}) @driver.with(safe: { wtimeout: 5 }) do |_session| _session[@collection.to_sym].find(params).limit(1).first end end |
#get(key) ⇒ Object
23 24 25 26 27 |
# File 'lib/jamnagar/adapters/mongo_adapter.rb', line 23 def get(key) @driver.with(safe: { wtimeout: 5 }) do |_session| _session[@collection.to_sym].find("_id" => key).first end end |
#store(key, value) ⇒ Object
17 18 19 20 21 |
# File 'lib/jamnagar/adapters/mongo_adapter.rb', line 17 def store(key, value) @driver.with(safe: { wtimeout: 5 }) do |_session| _session[@collection.to_sym].insert(value) end end |
#update(query = {}, updates = {}) ⇒ Object
41 42 43 44 45 |
# File 'lib/jamnagar/adapters/mongo_adapter.rb', line 41 def update(query={}, updates={}) @driver.with(safe: { wtimeout: 5 }) do |_session| _session[@collection.to_sym].find(query).update(updates) end end |