remove trailing white space from all files
[arvados.git] / sdk / R / R / Subcollection.R
index c42e7e55be9c61d4d258d5dc5da0c6c22d402ccf..45fe34347903192bed1d8618e14aceedf73a18f2 100644 (file)
@@ -1,10 +1,49 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
+
 source("./R/util.R")
 
-#' Arvados SubCollection Object
+#' Subcollection
+#'
+#' Subcollection class represents a folder inside Arvados collection.
+#' It is essentially a composite of arvadosFiles and other subcollections.
+#'
+#' @section Usage:
+#' \preformatted{subcollection = Subcollection$new(name)}
+#'
+#' @section Arguments:
+#' \describe{
+#'   \item{name}{Name of the subcollection.}
+#' }
 #'
-#' Update description
+#' @section Methods:
+#' \describe{
+#'   \item{getName()}{Returns name of the subcollection.}
+#'   \item{getRelativePath()}{Returns subcollection path relative to the root.}
+#'   \item{add(content)}{Adds ArvadosFile or Subcollection specified by content to the subcollection.}
+#'   \item{remove(name)}{Removes ArvadosFile or Subcollection specified by name from the subcollection.}
+#'   \item{get(relativePath)}{If relativePath is valid, returns ArvadosFile or Subcollection specified by relativePath, else returns NULL.}
+#'   \item{getFileListing()}{Returns subcollections file content as character vector.}
+#'   \item{getSizeInBytes()}{Returns subcollections content size in bytes.}
+#'   \item{move(newLocation)}{Moves subcollection to a new location inside collection.}
+#' }
 #'
-#' @export Subcollection
+#' @name Subcollection
+#' @examples
+#' \dontrun{
+#' myFolder <- Subcollection$new("myFolder")
+#' myFile   <- ArvadosFile$new("myFile")
+#'
+#' myFolder$add(myFile)
+#' myFolder$get("myFile")
+#' myFolder$remove("myFile")
+#'
+#' myFolder$move("newLocation/myFolder")
+#' }
+NULL
+
+#' @export
 Subcollection <- R6::R6Class(
 
     "Subcollection",
@@ -13,11 +52,11 @@ Subcollection <- R6::R6Class(
 
         initialize = function(name)
         {
-            private$name       <- name
+            private$name <- name
         },
 
         getName = function() private$name,
-        
+
         getRelativePath = function()
         {
             relativePath <- c(private$name)
@@ -38,13 +77,17 @@ Subcollection <- R6::R6Class(
             if("ArvadosFile"   %in% class(content) ||
                "Subcollection" %in% class(content))
             {
+                if(content$getName() == "")
+                    stop("Content has invalid name.")
+
                 childWithSameName <- self$get(content$getName())
+
                 if(!is.null(childWithSameName))
                     stop(paste("Subcollection already contains ArvadosFile",
                                "or Subcollection with same name."))
 
                 if(!is.null(private$collection))
-                {       
+                {
                     if(self$getRelativePath() != "")
                         contentPath <- paste0(self$getRelativePath(),
                                               "/", content$getFileListing())
@@ -83,6 +126,7 @@ Subcollection <- R6::R6Class(
                 {
                     REST <- private$collection$getRESTService()
                     REST$delete(child$getRelativePath(), private$collection$uuid)
+
                     child$setCollection(NULL)
                 }
 
@@ -101,23 +145,8 @@ Subcollection <- R6::R6Class(
 
         getFileListing = function(fullPath = TRUE)
         {
-            content <- NULL
-
-            if(fullPath)
-            {
-                for(child in private$children)
-                    content <- c(content, child$getFileListing())
-
-                if(private$name != "")
-                    content <- unlist(paste0(private$name, "/", content))
-            }
-            else
-            {
-                for(child in private$children)
-                    content <- c(content, child$getName())
-            }
-
-            content
+            content <- private$getContentAsCharVector(fullPath)
+            content[order(tolower(content))]
         },
 
         getSizeInBytes = function()
@@ -250,8 +279,55 @@ Subcollection <- R6::R6Class(
             parent$remove(private$name)
 
             parent$setCollection(parentsCollection, setRecursively = FALSE)
+        },
+
+        getContentAsCharVector = function(fullPath = TRUE)
+        {
+            content <- NULL
+
+            if(fullPath)
+            {
+                for(child in private$children)
+                    content <- c(content, child$getFileListing())
+
+                if(private$name != "")
+                    content <- unlist(paste0(private$name, "/", content))
+            }
+            else
+            {
+                for(child in private$children)
+                    content <- c(content, child$getName())
+            }
+
+            content
         }
     ),
-    
+
     cloneable = FALSE
 )
+
+#' print.Subcollection
+#'
+#' Custom print function for Subcollection class
+#'
+#' @param x Instance of Subcollection class
+#' @param ... Optional arguments.
+#' @export
+print.Subcollection = function(x, ...)
+{
+    collection   <- NULL
+    relativePath <- x$getRelativePath()
+
+    if(!is.null(x$getCollection()))
+    {
+        collection <- x$getCollection()$uuid
+
+        if(!x$getName() == "")
+            relativePath <- paste0("/", relativePath)
+    }
+
+    cat(paste0("Type:          ", "\"", "Arvados Subcollection", "\""), sep = "\n")
+    cat(paste0("Name:          ", "\"", x$getName(),             "\""), sep = "\n")
+    cat(paste0("Relative path: ", "\"", relativePath,            "\""), sep = "\n")
+    cat(paste0("Collection:    ", "\"", collection,              "\""), sep = "\n")
+}