Skip to content

Instantly share code, notes, and snippets.

@skippy
Created April 1, 2010 04:10
Show Gist options
  • Save skippy/351334 to your computer and use it in GitHub Desktop.
Save skippy/351334 to your computer and use it in GitHub Desktop.
# wrapping the new GridFileSystem so it has some of the nice helpers of the old GridFS class
# HACK: I set MongoMapper.database to the database I need for the particular call, so I can
# get away with simpler method calls....
module Mongo
class GridFSExt
def self.read(file_loc)
@gridfs = GridFileSystem.new(MongoMapper.database)
@gridfs.open(file_loc, "r") {|f| f.read } rescue nil
end
def self.exist?(file_loc)
@gridfs = GridFileSystem.new(MongoMapper.database)
val = @gridfs.open(file_loc, "r") rescue nil
!val.nil?
end
def self.list(root_collection=Mongo::Grid::DEFAULT_FS_NAME)
# @gridfs = GridFileSystem.new(MongoMapper.database)
MongoMapper.database.collection("#{root_collection}.files").find().map do |f|
f['filename']
end
end
def self.delete(*names)
@gridfs = GridFileSystem.new(MongoMapper.database)
names.each do |name|
@gridfs.delete(name)
end
end
def self.method_missing(method, *args, &block)
@gridfs = GridFileSystem.new(MongoMapper.database)
if @gridfs.respond_to?(method)
@gridfs.__send__(method, *args, &block)
else
super
end
end
end
class GridIO
alias_method :<<, :write
public :get_md5
alias_method :md5, :get_md5
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment