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.



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

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



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

def db
  @@db
end

#find(collection, scope, query = nil) ⇒ Object



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

def find(collection, scope, query=nil)
  puts "FIND: #{collection.inspect} - #{scope}"
  return @@db[collection].find(scope).to_a
end

#save(collection, data) ⇒ Object



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 17

def save(collection, data)
  puts "Insert: #{data.inspect} on #{collection.inspect}"
  # Try to create
  # TODO: Seems mongo is dumb and doesn't let you upsert with custom id's
  begin
    @@db[collection].insert(data)
    id = {'_id' => data.delete('_id')}
  rescue Mongo::OperationFailure => error
    # Really mongo client?
    if error.message[/^11000[:]/]
      # Update because the id already exists
      id = {'_id' => data.delete('_id')}
      @@db[collection].update(id, data)
    else
      raise
    end
  end
  
  id = id['_id']
  # ChannelHandler.send_message_all(@channel, 'update', nil, id, data.merge('_id' => id))
  
  ChannelTasks.send_message_to_channel("#{collection}##{id}", ['update', nil, id, data.merge('_id' => id)], @channel)
end