Class: Rmodel::Mongo::Source
- Inherits:
-
Object
- Object
- Rmodel::Mongo::Source
- Defined in:
- lib/rmodel/mongo/source.rb
Defined Under Namespace
Classes: Query
Instance Method Summary collapse
- #build_query ⇒ Object
- #delete(id) ⇒ Object
- #delete_by_query(query) ⇒ Object
- #exec_query(query) ⇒ Object
- #find(id) ⇒ Object
-
#initialize(connection, collection) ⇒ Source
constructor
A new instance of Source.
- #insert(doc) ⇒ Object
- #update(id, doc) ⇒ Object
Constructor Details
#initialize(connection, collection) ⇒ Source
Returns a new instance of Source.
11 12 13 14 15 16 17 |
# File 'lib/rmodel/mongo/source.rb', line 11 def initialize(connection, collection) @connection = connection raise ArgumentError, 'Connection is not setup' unless @connection @collection = collection raise ArgumentError, 'Collection can not be guessed' unless @collection end |
Instance Method Details
#build_query ⇒ Object
37 38 39 |
# File 'lib/rmodel/mongo/source.rb', line 37 def build_query Query.new end |
#delete(id) ⇒ Object
33 34 35 |
# File 'lib/rmodel/mongo/source.rb', line 33 def delete(id) @connection[@collection].find(_id: id).delete_one end |
#delete_by_query(query) ⇒ Object
45 46 47 |
# File 'lib/rmodel/mongo/source.rb', line 45 def delete_by_query(query) exec_query(query).delete_many end |
#exec_query(query) ⇒ Object
41 42 43 |
# File 'lib/rmodel/mongo/source.rb', line 41 def exec_query(query) @connection[@collection].find(query.selector, query.) end |
#find(id) ⇒ Object
19 20 21 |
# File 'lib/rmodel/mongo/source.rb', line 19 def find(id) @connection[@collection].find('_id' => id).first end |
#insert(doc) ⇒ Object
23 24 25 26 27 |
# File 'lib/rmodel/mongo/source.rb', line 23 def insert(doc) doc = doc.merge('_id' => BSON::ObjectId.new) if doc['_id'].nil? @connection[@collection].insert_one(doc) doc['_id'] end |
#update(id, doc) ⇒ Object
29 30 31 |
# File 'lib/rmodel/mongo/source.rb', line 29 def update(id, doc) @connection[@collection].find(_id: id).update_one(doc) end |