Class: Dragonfly::DataStorage::MongoDataStore

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

Constant Summary

OBJECT_ID =

Mongo gem deprecated ObjectID in favour of ObjectId

defined?(BSON::ObjectId) ? BSON::ObjectId : BSON::ObjectID
INVALID_OBJECT_ID =
defined?(BSON::InvalidObjectId) ? BSON::InvalidObjectId : BSON::InvalidObjectID

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::MongoDataStore

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

- (MongoDataStore) initialize(opts = {})

Returns a new instance of MongoDataStore



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dragonfly/data_storage/mongo_data_store.rb', line 24

def initialize(opts={})
  self.host = opts[:host]
  self.hosts = opts[:hosts]
  self.connection_opts = opts[:connection_opts] if opts[:connection_opts]
  self.port = opts[:port]
  self.database = opts[:database] if opts[:database]
  self.username = opts[:username]
  self.password = opts[:password]
  self.connection = opts[:connection]
  self.db = opts[:db]
end

Instance Method Details

- (Object) connection



66
67
68
69
70
71
72
# File 'lib/dragonfly/data_storage/mongo_data_store.rb', line 66

def connection
  @connection ||= if hosts
    Mongo::ReplSetConnection.new(hosts, connection_opts)
  else
    Mongo::Connection.new(host, port, connection_opts)
  end
end

- (Object) db



74
75
76
# File 'lib/dragonfly/data_storage/mongo_data_store.rb', line 74

def db
  @db ||= connection.db(database)
end

- (Object) destroy(uid)

Raises:



59
60
61
62
63
64
# File 'lib/dragonfly/data_storage/mongo_data_store.rb', line 59

def destroy(uid)
  ensure_authenticated!
  grid.delete(bson_id(uid))
rescue Mongo::GridFileNotFound, INVALID_OBJECT_ID => e
  raise DataNotFound, "#{e} - #{uid}"
end

- (Object) grid



78
79
80
# File 'lib/dragonfly/data_storage/mongo_data_store.rb', line 78

def grid
  @grid ||= Mongo::Grid.new(db)
end

- (Object) retrieve(uid)

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dragonfly/data_storage/mongo_data_store.rb', line 46

def retrieve(uid)
  ensure_authenticated!
  grid_io = grid.get(bson_id(uid))
  meta = marshal_decode(grid_io.)
  meta.merge!(:stored_at => grid_io.upload_date)
  [
    grid_io.read,
    meta
  ]
rescue Mongo::GridFileNotFound, INVALID_OBJECT_ID => e
  raise DataNotFound, "#{e} - #{uid}"
end

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



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

def store(temp_object, opts={})
  ensure_authenticated!
  content_type = opts[:content_type] || opts[:mime_type] || 'application/octet-stream'
  temp_object.file do |f|
    mongo_id = grid.put(f, :content_type => content_type,
                           :metadata => marshal_encode(temp_object.meta))
    mongo_id.to_s
  end
end