Class: Copy::Storage::Mongodb
- Inherits:
-
Object
- Object
- Copy::Storage::Mongodb
- Defined in:
- lib/copy/storage/mongodb.rb
Instance Method Summary collapse
- #get(name) ⇒ Object
-
#initialize(connection_url) ⇒ Mongodb
constructor
A new instance of Mongodb.
- #set(name, content) ⇒ Object
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 |