Class: StoreTasks

Inherits:
Object show all
Defined in:
app/volt/tasks/store_tasks.rb

Instance Method Summary collapse

Constructor Details

#initialize(channel = nil, dispatcher = nil) ⇒ StoreTasks

Returns a new instance of StoreTasks.



4
5
6
7
8
9
10
# File 'app/volt/tasks/store_tasks.rb', line 4

def initialize(channel=nil, dispatcher=nil)
  @@mongo_db ||= Mongo::MongoClient.new("localhost", 27017)
  @@db ||= @@mongo_db.db("development")

  @channel = channel
  @dispatcher = dispatcher
end

Instance Method Details

#dbObject



12
13
14
# File 'app/volt/tasks/store_tasks.rb', line 12

def db
  @@db
end

#delete(collection, id) ⇒ Object



41
42
43
44
45
46
# File 'app/volt/tasks/store_tasks.rb', line 41

def delete(collection, id)
  puts "DELETE: #{collection.inspect} - #{id.inspect}"
  @@db[collection].remove('_id' => id)

  QueryTasks.live_query_pool.updated_collection(collection, @channel)
end

#save(collection, data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/volt/tasks/store_tasks.rb', line 16

def save(collection, data)
  # puts "Insert: #{data.inspect} on #{collection.inspect}"

  data = data.symbolize_keys
  id = data[:_id]

  # Try to create
  # TODO: Seems mongo is dumb and doesn't let you upsert with custom id's
  begin
    @@db[collection].insert(data)
  rescue Mongo::OperationFailure => error
    # Really mongo client?
    if error.message[/^11000[:]/]
      # Update because the id already exists
      update_data = data.dup
      update_data.delete(:_id)
      @@db[collection].update({:_id => id}, update_data)
    else
      raise
    end
  end

  QueryTasks.live_query_pool.updated_collection(collection, @channel)
end