Class: Dragonfly::MongoidDataStore

Inherits:
Object
  • Object
show all
Includes:
Serializer
Defined in:
lib/dragonfly/mongoid_data_store.rb,
lib/dragonfly/mongoid_data_store/version.rb

Defined Under Namespace

Classes: DataNotFound

Constant Summary collapse

OBJECT_ID =
BSON::ObjectId
VERSION =
'0.2.0'.freeze

Instance Method Summary collapse

Instance Method Details

#destroy(uid) ⇒ Object



39
40
41
# File 'lib/dragonfly/mongoid_data_store.rb', line 39

def destroy(uid)
  Mongoid::GridFs.delete(uid)
end

#read(uid) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dragonfly/mongoid_data_store.rb', line 26

def read(uid)
  grid_file = Mongoid::GridFS.get(uid)

  meta = {}
  meta = marshal_b64_decode(grid_file.meta) if grid_file.respond_to?(:meta)
  meta[:stored_at] = grid_file.upload_date if grid_file.respond_to?(:upload_date)

  [grid_file.data, meta]

rescue Mongoid::Errors::DocumentNotFound => e
  raise DataNotFound, "#{e} - #{uid}"
end

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



15
16
17
18
19
20
21
22
23
24
# File 'lib/dragonfly/mongoid_data_store.rb', line 15

def write(temp_object, opts = {})
  content_type = opts[:content_type] || opts[:mime_type] || 'application/octet-stream'
  meta = temp_object.meta
  meta = meta.merge(opts[:meta]) if opts[:meta].present?

  temp_object.file do |f|
    grid_file = Mongoid::GridFS.put(f, content_type: content_type, meta: marshal_b64_encode(meta))
    grid_file.id.to_s
  end
end