-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update descriptions with dedicated function
- Loading branch information
Showing
22 changed files
with
239 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#' Update the description of a `git2rdata` object | ||
#' | ||
#' Allows to update the description of the fields, the table name, the title, | ||
#' and the description of a `git2rdata` object. | ||
#' All arguments are optional. | ||
#' Setting an argument to `NA` or an empty string will remove the corresponding | ||
#' field from the metadata. | ||
#' | ||
#' @inheritParams is_git2rmeta | ||
#' @param field_description a named character vector with the new descriptions | ||
#' for the fields. | ||
#' The names of the vector must match the variable names. | ||
#' @param name a character string with the new table name of the object. | ||
#' @param title a character string with the new title of the object. | ||
#' @param description a character string with the new description of the object. | ||
#' @family storage | ||
#' @export | ||
#' @importFrom assertthat assert_that has_name | ||
update_description <- function( | ||
file, root = ".", field_description, name, title, description | ||
) { | ||
root <- normalizePath(root, winslash = "/", mustWork = TRUE) | ||
file <- clean_data_path(root = root, file = file) | ||
is_git2rmeta( | ||
file = remove_root(file = file["meta_file"], root = root), root = root, | ||
message = "error" | ||
) | ||
old <- read_yaml(file["meta_file"]) | ||
class(old) <- "meta_list" | ||
if (!missing(field_description)) { | ||
assert_that( | ||
is.character(field_description), length(field_description) > 0, | ||
!has_name(field_description, "..generic") | ||
) | ||
stopifnot( | ||
"names in `field_description` don't match variable names" = | ||
all(names(field_description) %in% names(old)) | ||
) | ||
for (name in names(field_description)) { | ||
old[[name]][["description"]] <- update_or_drop(field_description[[name]]) | ||
} | ||
} | ||
|
||
if (!missing(name)) { | ||
old[["..generic"]][["name"]] <- update_or_drop(name) | ||
} | ||
|
||
if (!missing(title)) { | ||
old[["..generic"]][["title"]] <- update_or_drop(title) | ||
} | ||
|
||
if (!missing(description)) { | ||
old[["..generic"]][["description"]] <- update_or_drop(description) | ||
} | ||
|
||
packageVersion("git2rdata") |> | ||
as.character() -> old[["..generic"]][["git2rdata"]] | ||
metadata_hash(old) -> old[["..generic"]][["hash"]] | ||
write_yaml(old, file["meta_file"]) | ||
return(invisible(NULL)) | ||
} | ||
|
||
#' @importFrom assertthat assert_that is.string | ||
update_or_drop <- function(x) { | ||
assert_that(is.string(x)) | ||
if (is.na(x) || x == "") { | ||
return(NULL) | ||
} else { | ||
return(x) | ||
} | ||
} |
Oops, something went wrong.