Class: Rmodel::Mongo::Source
- Inherits:
-
Object
- Object
- Rmodel::Mongo::Source
- Defined in:
- lib/rmodel/mongo/source.rb
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.
6 7 8 9 10 11 12 |
# File 'lib/rmodel/mongo/source.rb', line 6 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
32 33 34 |
# File 'lib/rmodel/mongo/source.rb', line 32 def build_query Query.new end |
#delete(id) ⇒ Object
28 29 30 |
# File 'lib/rmodel/mongo/source.rb', line 28 def delete(id) @connection[@collection].find(_id: id).delete_one end |
#delete_by_query(query) ⇒ Object
40 41 42 |
# File 'lib/rmodel/mongo/source.rb', line 40 def delete_by_query(query) exec_query(query).delete_many end |
#exec_query(query) ⇒ Object
36 37 38 |
# File 'lib/rmodel/mongo/source.rb', line 36 def exec_query(query) @connection[@collection].find(query.selector, query.) end |
#find(id) ⇒ Object
14 15 16 |
# File 'lib/rmodel/mongo/source.rb', line 14 def find(id) @connection[@collection].find('_id' => id).first end |
#insert(doc) ⇒ Object
18 19 20 21 22 |
# File 'lib/rmodel/mongo/source.rb', line 18 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
24 25 26 |
# File 'lib/rmodel/mongo/source.rb', line 24 def update(id, doc) @connection[@collection].find(_id: id).update_one(doc) end |