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] if opts[:host]
  self.port = opts[:port] if 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
# File 'lib/dragonfly/data_storage/couch_data_store.rb', line 54

def db
  @db ||= begin
    url = "http://#{auth}#{host}:#{port}"
    CouchRest.new(url).database!(database)
  end
end

#destroy(uid) ⇒ Object

Raises:



46
47
48
49
50
51
52
# File 'lib/dragonfly/data_storage/couch_data_store.rb', line 46

def destroy(uid)
  doc_id, attachment = parse_uid(uid)
  doc = db.get(doc_id)
  db.delete_doc(doc)
rescue RestClient::ResourceNotFound => e
  raise DataNotFound, "#{e} - #{uid}"
end

#retrieve(uid) ⇒ Object

Raises:



38
39
40
41
42
43
44
# File 'lib/dragonfly/data_storage/couch_data_store.rb', line 38

def retrieve(uid)
  doc_id, attachment = parse_uid(uid)
  doc = db.get(doc_id)
  [doc.fetch_attachment(attachment), 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
# File 'lib/dragonfly/data_storage/couch_data_store.rb', line 24

def store(temp_object, opts={})
  name = temp_object.name || 'file'
  content_type = opts[:content_type] || opts[:mime_type] || 'application/octet-stream'
  
  temp_object.file do |f|
    doc = CouchRest::Document.new(:meta => marshal_encode(temp_object.meta))
    response = db.save_doc(doc)
    doc.put_attachment(name, f.dup, :content_type => content_type)
    form_uid(response['id'], name)
  end
rescue RuntimeError => e
  raise UnableToStore, "#{e} - #{temp_object.inspect}"
end

#url_for(uid, opts = {}) ⇒ Object



61
62
63
64
# File 'lib/dragonfly/data_storage/couch_data_store.rb', line 61

def url_for(uid, opts={})
  doc_id, attachment = parse_uid(uid)
  "http://#{host}:#{port}/#{database}/#{doc_id}/#{attachment}"
end