Permalink
Browse files

Adding Collection#file_by_title.

Refactoring.
  • Loading branch information...
1 parent 41ce03c commit f5081a048888b4c7cabe906d554f559b479dd802 @gimite committed Mar 24, 2013
Showing with 49 additions and 31 deletions.
  1. +24 −9 lib/google_drive/collection.rb
  2. +4 −15 lib/google_drive/session.rb
  3. +21 −7 test/test_google_drive.rb
View
33 lib/google_drive/collection.rb
@@ -116,23 +116,38 @@ def subcollections(params = {})
return files_with_type("folder", params)
end
+ # Returns a file (can be a spreadsheet, document, subcollection or other files) in the
+ # collection which exactly matches +title+ as GoogleDrive::File.
+ # Returns nil if not found. If multiple collections with the +title+ are found, returns
+ # one of them.
+ #
+ # If given an Array, does a recursive subcollection traversal.
+ def file_by_title(title)
+ return file_by_title_with_type(title, nil)
+ end
+
# Returns its subcollection whose title exactly matches +title+ as GoogleDrive::Collection.
# Returns nil if not found. If multiple collections with the +title+ are found, returns
# one of them.
#
- # If given an Array does a recursive subfolder traversal.
+ # If given an Array, does a recursive subcollection traversal.
def subcollection_by_title(title)
- if title.respond_to? :shift
- relpath = title
- if relpath.length == 1
- return subcollection_by_title relpath[0]
+ return file_by_title_with_type(title, "folder")
+ end
+
+ protected
+
+ def file_by_title_with_type(title, type)
+ if title.is_a?(Array)
+ rel_path = title
+ if rel_path.empty?
+ return self
else
- subcollection_title = relpath.shift
- subcollection = subcollection_by_title subcollection_title
- return subcollection.subcollection_by_title relpath
+ parent = subcollection_by_title(rel_path[0...-1])
+ return parent && parent.file_by_title_with_type(rel_path[-1], type)
end
else
- return subcollections("title" => title, "title-exact" => "true")[0]
+ return files_with_type(type, "title" => title, "title-exact" => "true")[0]
end
end
View
19 lib/google_drive/session.rb
@@ -133,22 +133,11 @@ def files(params = {})
# Returns nil if not found. If multiple files with the +title+ are found, returns
# one of them.
#
- # If given an Array, traverse folders by title:
- # session.file_by_title ['myfolder', 'mysubfolder/even/w/slash', 'myfile' ]
+ # If given an Array, traverses collections by title. e.g.
+ # session.file_by_title(["myfolder", "mysubfolder/even/w/slash", "myfile"])
def file_by_title(title)
- if title.respond_to? :pop
- path = title
- if path.any?
- if path.size > 1
- basetitle = path.pop
- collection = root_collection.subcollection_by_title path
- return collection.files("title" => basetitle, "title-exact" => "true")[0]
- else
- return file_by_title path[0]
- end
- else
- return nil
- end
+ if title.is_a?(Array)
+ return self.root_collection.file_by_title(title)
else
return files("title" => title, "title-exact" => "true")[0]
end
View
28 test/test_google_drive.rb
@@ -16,7 +16,6 @@ class TC_GoogleDrive < Test::Unit::TestCase
@@session = nil
-
def test_spreadsheet_online()
session = get_session()
@@ -210,19 +209,34 @@ def test_collection_and_file_online()
assert_equal("", file2.download_to_string())
# Checks if file exists in root.
- files = root.files("title" => test_file_title, "title-exact" => true)
- assert_equal(1, files.size)
- assert_equal(test_file_title, files[0].title)
+ tfile = session.file_by_title(test_file_title)
+ assert_not_nil(tfile)
+ assert_equal(test_file_title, tfile.title)
+ tfile = root.file_by_title(test_file_title)
+ assert_not_nil(tfile)
+ assert_equal(test_file_title, tfile.title)
+ tfiles = root.files("title" => test_file_title, "title-exact" => true)
+ assert_equal(1, tfiles.size)
+ assert_equal(test_file_title, tfiles[0].title)
+ tfile = session.file_by_title([test_file_title])
+ assert_not_nil(tfile)
+ assert_equal(test_file_title, tfile.title)
# Moves file to collection.
collection.add(file)
root.remove(file)
# Checks if file exists in collection.
assert(root.files("title" => test_file_title, "title-exact" => true).empty?)
- files = collection.files("title" => test_file_title, "title-exact" => true)
- assert_equal(1, files.size)
- assert_equal(test_file_title, files[0].title)
+ tfile = collection.file_by_title(test_file_title)
+ assert_not_nil(tfile)
+ assert_equal(test_file_title, tfile.title)
+ tfiles = collection.files("title" => test_file_title, "title-exact" => true)
+ assert_equal(1, tfiles.size)
+ assert_equal(test_file_title, tfiles[0].title)
+ tfile = session.file_by_title([test_collection_title, test_file_title])
+ assert_not_nil(tfile)
+ assert_equal(test_file_title, tfile.title)
# Deletes files.
delete_test_file(file, true)

0 comments on commit f5081a0

Please sign in to comment.