Class: Copy::Storage::Mongodb

Inherits:
Object
  • Object
show all
Defined in:
lib/copy/storage/mongodb.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection_url) ⇒ Mongodb

Returns a new instance of Mongodb.



6
7
8
9
10
11
12
13
14
# File 'lib/copy/storage/mongodb.rb', line 6

def initialize(connection_url)
  uri         = URI.parse(connection_url)
  connection  = ::Mongo::Connection.from_uri(connection_url)
  database    = connection.db(uri.path.gsub(/^\//, ''))
  
  @collection = database['copy-content']
  @collection.ensure_index([['name', Mongo::ASCENDING]], :unique => true)
  @collection
end

Instance Method Details

#get(name) ⇒ Object



16
17
18
19
# File 'lib/copy/storage/mongodb.rb', line 16

def get(name)
  doc = find(name)
  doc['content'] unless doc.nil?
end

#set(name, content) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/copy/storage/mongodb.rb', line 21

def set(name, content)
  doc = find(name)
  if doc
    doc['content'] = content
    @collection.update({ '_id' => doc['_id'] }, doc)
  else
    @collection.insert('name' => name, 'content' => content)
  end
end