Module: MongoMapper::Files

Defined in:
lib/mm-files/files.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object



3
4
5
6
7
# File 'lib/mm-files/files.rb', line 3

def self.included(model)
  model.class_eval do
    after_create :_sync_pending_files
  end
end

Instance Method Details

#fetch_file(filename) ⇒ Object



21
22
23
# File 'lib/mm-files/files.rb', line 21

def fetch_file(filename)
  MongoMapper::File.fetch(self, filename) if !new?
end

#filesObject



25
26
27
28
29
30
31
# File 'lib/mm-files/files.rb', line 25

def files
  criteria, options = FinderOptions.new(:metadata => {:_id => self.id}).to_a
  coll = self.class.database.collection("#{self.collection.name}.files")
  @files = coll.find(criteria, options).map do |a|
    MongoMapper::File.new(self, a)
  end
end

#put_file(filename, data, metadata = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mm-files/files.rb', line 9

def put_file(filename, data,  = {})
  if !new?
    XGen::Mongo::GridFS::GridStore.open(self.class.database, filename, "w",
                                        :root => self.collection.name,
                                        :metadata => .deep_merge({:_id => self.id})) do |f|
      f << data
    end
  else
    (@_pending_files ||= {})[filename] = data
  end
end