From 284c442c465d18e1d4add7812487d15ef604b829 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Tue, 30 Mar 2010 11:54:23 -0400 Subject: [PATCH] filename is now optional for Grid#put RUBY-116 --- lib/mongo/gridfs/grid.rb | 11 +++++++++-- test/grid_test.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/mongo/gridfs/grid.rb b/lib/mongo/gridfs/grid.rb index 0a9984c..cc9750d 100644 --- a/lib/mongo/gridfs/grid.rb +++ b/lib/mongo/gridfs/grid.rb @@ -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) diff --git a/test/grid_test.rb b/test/grid_test.rb index 9486f79..1467fcb 100644 --- a/test/grid_test.rb +++ b/test/grid_test.rb @@ -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')