Class: Uorm::Mongo::Persistance
- Inherits:
-
Struct
- Object
- Struct
- Uorm::Mongo::Persistance
- Defined in:
- lib/uorm/mongo/persistance.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#model ⇒ Object
Returns the value of attribute model.
Instance Method Summary collapse
- #all(query = {}, &block) ⇒ Object
- #create(object, cb = nil) ⇒ Object
- #delete(object) ⇒ Object
- #find(id, &block) ⇒ Object
- #update(object, cb = nil) ⇒ Object
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client
3 4 5 |
# File 'lib/uorm/mongo/persistance.rb', line 3 def client @client end |
#model ⇒ Object
Returns the value of attribute model
3 4 5 |
# File 'lib/uorm/mongo/persistance.rb', line 3 def model @model end |
Instance Method Details
#all(query = {}, &block) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/uorm/mongo/persistance.rb', line 4 def all query = {}, &block formatted = format_query(query) if block_given? client.afind(formatted).each(&block) else client.find(formatted) end end |
#create(object, cb = nil) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/uorm/mongo/persistance.rb', line 22 def create object, cb = nil if cb safe_insert = client.safe_insert attributes_without_id object safe_insert.callback do |object_id| object.id = object_id cb.succeed object end else object.id = ::EM::Synchrony.sync client.safe_insert attributes_without_id object end end |
#delete(object) ⇒ Object
45 46 47 48 |
# File 'lib/uorm/mongo/persistance.rb', line 45 def delete object # wont return a deferrable :/ client.remove _id: object.id end |
#find(id, &block) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/uorm/mongo/persistance.rb', line 13 def find id, &block object_id = ::BSON::ObjectId(id.to_s) if block_given? client.afirst(_id: object_id).callback(&block) else client.first(_id: object_id) end end |
#update(object, cb = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/uorm/mongo/persistance.rb', line 34 def update object, cb = nil if cb safe_update = client.safe_update({ _id: object.id }, attributes_without_id(object)) safe_update.callback do cb.succeed object end else ::EM::Synchrony.sync client.safe_update({ _id: object.id }, attributes_without_id(object)) end end |