class accessors for default root collection RUBY-93

This commit is contained in:
Kyle Banker 2010-02-08 13:04:28 -05:00
parent 98af49f465
commit aad2f5508c
2 changed files with 33 additions and 9 deletions

View File

@ -79,6 +79,14 @@ module GridFS
attr_reader :md5 attr_reader :md5
def self.default_root_collection
@@default_root_collection ||= DEFAULT_ROOT_COLLECTION
end
def self.default_root_collection=(name)
@@default_root_collection = name
end
# 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.
@ -165,7 +173,7 @@ module GridFS
def self.unlink(db, *names) def self.unlink(db, *names)
names.each do |name| names.each do |name|
gs = GridStore.new(db, name) gs = GridStore.new(db, name)
gs.send(:delete_chunks) gs.delete_chunks
gs.collection.remove('_id' => gs.files_id) gs.collection.remove('_id' => gs.files_id)
end end
end end
@ -203,7 +211,7 @@ module GridFS
# file's metadata. See also GridStore#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] || GridStore.default_root_collection
doc = collection.find({'filename' => @filename}).next_document doc = collection.find({'filename' => @filename}).next_document
if doc if doc
@ -490,6 +498,11 @@ module GridFS
@db == nil @db == nil
end end
def delete_chunks
chunk_collection.remove({'files_id' => @files_id}) if @files_id
@curr_chunk = nil
end
#--- #---
# ================ protected ================ # ================ protected ================
#+++ #+++
@ -539,11 +552,6 @@ module GridFS
buf buf
end end
def delete_chunks
chunk_collection.remove({'files_id' => @files_id}) if @files_id
@curr_chunk = nil
end
def nth_chunk(n) def nth_chunk(n)
mongo_chunk = chunk_collection.find({'files_id' => @files_id, 'n' => n}).next_document mongo_chunk = chunk_collection.find({'files_id' => @files_id, 'n' => n}).next_document
Chunk.new(self, mongo_chunk || {}) Chunk.new(self, mongo_chunk || {})

View File

@ -152,6 +152,22 @@ class GridStoreTest < Test::Unit::TestCase
assert_equal 0, @@chunks.count assert_equal 0, @@chunks.count
end end
def test_unlink_alternate_root_collection
GridStore.default_root_collection = 'gridfs'
GridStore.open(@@db, 'foobar', 'w') do |f|
f.puts "Hello"
end
assert GridStore.exist?(@@db, 'foobar')
GridStore.default_root_collection = 'fs'
GridStore.unlink(@@db, 'foobar')
assert !GridStore.exist?(@@db, 'foobar')
GridStore.default_root_collection = 'gridfs'
GridStore.unlink(@@db, 'foobar')
assert !GridStore.exist?(@@db, 'foobar')
end
def test_mv def test_mv
assert_equal 1, @@files.count assert_equal 1, @@files.count
assert_equal 1, @@chunks.count assert_equal 1, @@chunks.count