minor: gridfs docs

This commit is contained in:
Kyle Banker 2010-01-19 10:26:40 -05:00
parent 16fbbe8e4e
commit c12a40ad3e

View File

@ -77,7 +77,6 @@ module GridFS
attr_reader :md5 attr_reader :md5
# Determine whether a given file exists in the GridStore. # Determine whether a given file exists in the GridStore.
# #
# @param [Mongo::DB] a MongoDB database. # @param [Mongo::DB] a MongoDB database.
@ -121,10 +120,10 @@ module GridFS
# #
# @return [String] the file data # @return [String] the file data
def self.read(db, name, length=nil, offset=nil) def self.read(db, name, length=nil, offset=nil)
GridStore.open(db, name, 'r') { |gs| GridStore.open(db, name, 'r') do |gs|
gs.seek(offset) if offset gs.seek(offset) if offset
gs.read(length) gs.read(length)
} end
end end
# List the contents of all GridFS files stored in the given db and # List the contents of all GridFS files stored in the given db and
@ -135,9 +134,9 @@ module GridFS
# #
# @return [Array] # @return [Array]
def self.list(db, root_collection=DEFAULT_ROOT_COLLECTION) def self.list(db, root_collection=DEFAULT_ROOT_COLLECTION)
db.collection("#{root_collection}.files").find().map { |f| db.collection("#{root_collection}.files").find().map do |f|
f['filename'] f['filename']
} end
end end
# Get each line of data from the specified file # Get each line of data from the specified file
@ -149,9 +148,9 @@ module GridFS
# #
# @return [Array] # @return [Array]
def self.readlines(db, name, separator=$/) def self.readlines(db, name, separator=$/)
GridStore.open(db, name, 'r') { |gs| GridStore.open(db, name, 'r') do |gs|
gs.readlines(separator) gs.readlines(separator)
} end
end end
# Remove one for more files from the given db. # Remove one for more files from the given db.
@ -159,14 +158,14 @@ module GridFS
# @param [Mongo::Database] a MongoDB database. # @param [Mongo::Database] a MongoDB database.
# @param [Array<String>] the filenames to remove # @param [Array<String>] the filenames to remove
def self.unlink(db, *names) def self.unlink(db, *names)
names.each { |name| names.each do |name|
gs = GridStore.new(db, name) gs = GridStore.new(db, name)
gs.send(:delete_chunks) gs.send(:delete_chunks)
gs.collection.remove('_id' => gs.files_id) gs.collection.remove('_id' => gs.files_id)
} end
end end
class << self class << self
alias_method :delete, :unlink alias_method :delete, :unlink
end end
# Rename a file in this collection. Note that this method uses # Rename a file in this collection. Note that this method uses
@ -183,24 +182,20 @@ module GridFS
# Initialize a GridStore instance for reading, writing, or modifying a given file. # Initialize a GridStore instance for reading, writing, or modifying a given file.
# Note that it's often easier to work with the various GridStore class methods (open, read, etc.). # Note that it's often easier to work with the various GridStore class methods (open, read, etc.).
# #
#
# @param [Mongo::DB] db a MongoDB database. # @param [Mongo::DB] db a MongoDB database.
# @param [String] name a filename. # @param [String] name a filename.
# @param [String] mode either 'r', 'w', or 'w+' for reading, writing, or appending, respectively. # @param [String] mode either 'r', 'w', or 'w+' for reading, writing, or appending, respectively.
# #
# @option options [String] root ('r', 'w', 'w+') # @option options [String] root DEFAULT_ROOT_COLLECTION ('r', 'w', 'w+') the name of the root collection to use.
# :root :: (r, w, w+) Name of root collection to use, instead of
# DEFAULT_ROOT_COLLECTION.
# #
# :metadata:: (w, w+) A hash containing any data you want persisted as # @option options [String] metadata ({}) (w, w+) A hash containing any data you want persisted as
# this file's metadata. See also metadata= # this file's metadata.
# #
# :chunk_size :: (w) Sets chunk size for files opened for writing # @option options [Integer] chunk_size (Chunk::DEFAULT_CHUNK_SIZE) (w) Sets chunk size for files opened for writing.
# See also chunk_size= which may only be called before # See also GridStore#chunk_size=.
# any data is written.
# #
# :content_type :: (w) Default value is DEFAULT_CONTENT_TYPE. See # @option options [String] content_type ('text/plain') Set the content type stored as the
# also #content_type= # file's metadata. See also GridStore#content_type=.
def initialize(db, name, mode='r', options={}) def initialize(db, name, mode='r', options={})
@db, @filename, @mode = db, name, mode @db, @filename, @mode = db, name, mode
@root = options[:root] || DEFAULT_ROOT_COLLECTION @root = options[:root] || DEFAULT_ROOT_COLLECTION