filename is now optional for Grid#put RUBY-116

This commit is contained in:
Kyle Banker 2010-03-30 11:54:23 -04:00
parent b87e3dd3fb
commit 284c442c46
2 changed files with 38 additions and 2 deletions

View File

@ -40,8 +40,8 @@ module Mongo
# Store a file in the file store.
#
# @param [String, #read] data a string or io-like object to store.
# @param [String] filename a name for the file.
#
# @options opts [String] :filename (nil) a name for the file.
# @options opts [Hash] :metadata ({}) any additional data to store with the file.
# @options opts [ObjectID] :_id (ObjectID) a unique id for
# the file to be use in lieu of an automatically generated one.
@ -53,7 +53,14 @@ module Mongo
# will be validated using an md5 hash. If validation fails, an exception will be raised.
#
# @return [Mongo::ObjectID] the file's id.
def put(data, filename, opts={})
def put(data, opts={}, old_opts={})
if opts.is_a?(String)
warn "The filename is now optional. Please pass the filename as a hash option: Grid#put(data, :filename => 'file.jpg')."
filename = opts
opts = old_opts
else
filename = opts[:filename]
end
opts.merge!(default_grid_io_opts)
file = GridIO.new(@files, @chunks, filename, 'w', opts=opts)
file.write(data)

View File

@ -46,6 +46,35 @@ class GridTest < Test::Unit::TestCase
end
end
context "Filename not required" do
setup do
@data = "GRIDDATA" * 50000
@grid = Grid.new(@db, 'test-fs')
@metadata = {'app' => 'photos'}
end
should "store the file without a filename" do
id = @grid.put(@data, 'sample', :metadata => @metadata)
file = @grid.get(id)
assert_equal 'sample', file.filename
assert_equal @metadata, file.metadata
end
should "store without a filename" do
id = @grid.put(@data, :metadata => @metadata)
file = @grid.get(id)
assert_nil file.filename
assert_equal @metadata, file.metadata
end
should "store with filename and metadata" do
id = @grid.put(@data, :filename => 'sample', :metadata => @metadata)
file = @grid.get(id)
assert_equal 'sample', file.filename
assert_equal @metadata, file.metadata
end
end
context "Storing data with a length of zero" do
setup do
@grid = Grid.new(@db, 'test-fs')