Class: Uorm::Mongo::Persistance

Inherits:
Struct
  • Object
show all
Defined in:
lib/uorm/mongo/persistance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject

Returns the value of attribute client

Returns:

  • (Object)

    the current value of client



3
4
5
# File 'lib/uorm/mongo/persistance.rb', line 3

def client
  @client
end

#modelObject

Returns the value of attribute model

Returns:

  • (Object)

    the current value of 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