Class: Dragonfly::DataStorage::CouchDataStore

Inherits:
Object
  • Object
show all
Includes:
Configurable, Serializer
Defined in:
lib/dragonfly/data_storage/couch_data_store.rb

Configuration Summary

Configurable attributes

Configurable attributes for an object in general object can be configured either by using something like
  object.configure do |c|
    c.some_configurable_attribute = "some value"
    c.some_other_configurable_attribute = 42
    ...
  end
or
  object.some_configurable_attribute = "some value"

Configurable attributes for instances of Dragonfly::DataStorage::CouchDataStore

Instance Method Summary (collapse)

Methods included from Serializer

#b64_decode, #b64_encode, #json_decode, #json_encode, #marshal_decode, #marshal_encode

Methods included from Configurable

included

Constructor Details

- (CouchDataStore) initialize(opts = {})

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

- (Object) db



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

- (Object) destroy(uid)

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

- (Object) retrieve(uid)

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

- (Object) store(temp_object, opts = {})

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

- (Object) url_for(uid, opts = {})



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