Class: Dragonfly::DataStorage::CouchDataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/data_storage/couch_data_store.rb

Instance Method Summary collapse

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

#dbObject



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

Raises:



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

Raises:



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.fetch_attachment(name), marshal_decode(doc['meta'])]
rescue RestClient::ResourceNotFound => e
  raise DataNotFound, "#{e} - #{uid}"
end

#store(temp_object, opts = {}) ⇒ Object

Raises:



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={})
  meta = opts[:meta] || {}
  name = meta[: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(meta))
    response = db.save_doc(doc)
    doc.put_attachment(name, f, {:content_type => content_type})
    response['id']
  end
rescue RuntimeError => e
  raise UnableToStore, "#{e} - #{temp_object.inspect}"
end