Class: Persistence::Adapters::GridFs

Inherits:
Object
  • Object
show all
Defined in:
lib/persistence/adapters/grid_fs.rb

Overview

GridFS filesystem adapter. Stores and loads files.

Author:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (GridFs) initialize(options = {})

Intializes adapter with given options.

instance.

Options Hash (options):

  • :database (Mongo::DB)

    Database object



22
23
24
25
# File 'lib/persistence/adapters/grid_fs.rb', line 22

def initialize(options = {})
  @database = options[:database]
  @grid = ::Mongo::Grid.new(@database)
end

Instance Attribute Details

- (Object) database

Returns the value of attribute database



14
15
16
# File 'lib/persistence/adapters/grid_fs.rb', line 14

def database
  @database
end

- (Object) grid

Returns the value of attribute grid



15
16
17
# File 'lib/persistence/adapters/grid_fs.rb', line 15

def grid
  @grid
end

Instance Method Details

- (true, false) delete_file(id)

Deletes the file from persistence store. Returns true if file was removed, otherwise return false.



63
64
65
66
67
68
69
# File 'lib/persistence/adapters/grid_fs.rb', line 63

def delete_file(id)
  id = self.to_id(id)
  self.grid.get(id)
  grid.delete(id)
rescue ::Mongo::GridFileNotFound
  false
end

- (String) get_file(id)

Retreives the file from persistence store and returns it's content.



53
54
55
56
# File 'lib/persistence/adapters/grid_fs.rb', line 53

def get_file(id)
  id = self.to_id(id)
  self.grid.get(id).read
end

- (Hash) metadata(id)

Returns file metadata (filename, content type).



31
32
33
34
35
36
37
# File 'lib/persistence/adapters/grid_fs.rb', line 31

def (id)
  id = self.to_id(id)
  io = self.grid.get(id)
  { filename: io.filename, content_type: io.content_type }
rescue ::Mongo::GridFileNotFound
  nil
end

- (BSON::ObjectId) push_file(source, original_filename, id = nil)

Pushes the file into persistence store and returns it's id.



43
44
45
46
# File 'lib/persistence/adapters/grid_fs.rb', line 43

def push_file(source, original_filename, id = nil)
  data = self.read_from(source)
  self.grid.put(data, filename: original_filename, _id: id)
end