Class: Dragonfly::DataStorage::CouchDataStore
- Defined in:
- lib/dragonfly/data_storage/couch_data_store.rb
Instance Method Summary collapse
- #db ⇒ Object
- #destroy(uid) ⇒ Object
-
#initialize(opts = {}) ⇒ CouchDataStore
constructor
A new instance of CouchDataStore.
- #retrieve(uid) ⇒ Object
- #store(temp_object, opts = {}) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ CouchDataStore
Returns a new instance of CouchDataStore.
16 17 18 19 20 21 22 |
# File 'lib/dragonfly/data_storage/couch_data_store.rb', line 16 def initialize(opts={}) self.host = opts[:host] self.port = opts[:port] self.database = opts[:database] if opts[:database] self.username = opts[:username] self.password = opts[:password] end |
Instance Method Details
#db ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/dragonfly/data_storage/couch_data_store.rb', line 54 def db @db ||= begin auth = username.blank? ? nil : "#{username}:#{password}@" url = "http://#{auth}#{host}:#{port}" CouchRest.new(url).database!(database) end end |
#destroy(uid) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/dragonfly/data_storage/couch_data_store.rb', line 47 def destroy(uid) doc = db.get(uid) db.delete_doc(doc) rescue RestClient::ResourceNotFound => e raise DataNotFound, "#{e} - #{uid}" end |
#retrieve(uid) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/dragonfly/data_storage/couch_data_store.rb', line 39 def retrieve(uid) doc = db.get(uid) name = doc['_attachments'].keys.first [doc.(name), marshal_decode(doc['meta'])] rescue RestClient::ResourceNotFound => e raise DataNotFound, "#{e} - #{uid}" end |
#store(temp_object, opts = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dragonfly/data_storage/couch_data_store.rb', line 24 def store(temp_object, opts={}) = opts[:meta] || {} name = [:name] || temp_object.original_filename || 'file' content_type = opts[:content_type] || opts[:mime_type] || 'application/octet-stream' temp_object.file do |f| doc = CouchRest::Document.new(:meta => marshal_encode()) response = db.save_doc(doc) doc.(name, f, {:content_type => content_type}) response['id'] end rescue RuntimeError => e raise UnableToStore, "#{e} - #{temp_object.inspect}" end |