Skip to content

Commit

Permalink
update_metadata() gains a stage and force argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ThierryO committed Oct 11, 2024
1 parent e099674 commit 3297092
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
21 changes: 16 additions & 5 deletions R/update_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,41 @@
#' @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.
#' @param ... parameters used in some methods
#' @family storage
#' @export
#' @importFrom assertthat assert_that has_name
update_metadata <- function(
file, root = ".", field_description, name, title, description
file, root = ".", field_description, name, title, description, ...
) {
UseMethod("update_metadata", root)
}

#' @export
update_metadata.default <- function(
file, root = ".", field_description, name, title, description
file, root = ".", field_description, name, title, description, ...
) {
stop("a 'root' of class ", class(root), " is not supported", call. = FALSE)
}

#' @export
#' @importFrom assertthat assert_that is.string noNA
#' @importFrom git2r add
#' @inheritParams git2r::add
update_metadata.git_repository <- function(
file, root = ".", field_description, name, title, description
file, root = ".", field_description, name, title, description, ...,
stage = FALSE, force = FALSE
) {
update_metadata(
assert_that(is.flag(stage), is.flag(force), noNA(stage), noNA(force))
file <- update_metadata(
file = file, root = workdir(root), name = name, title = title,
description = description, field_description = field_description
)
if (!stage) {
return(invisible(file))
}
add(root, path = file, force = force)
return(invisible(file))
}

#' @export
Expand Down Expand Up @@ -83,7 +94,7 @@ update_metadata.character <- function(
as.character() -> old[["..generic"]][["git2rdata"]]
metadata_hash(old) -> old[["..generic"]][["hash"]]
write_yaml(old, file["meta_file"])
return(invisible(NULL))
return(invisible(file["meta_file"]))
}

#' @importFrom assertthat assert_that is.string
Expand Down
4 changes: 2 additions & 2 deletions R/write_vc.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ setOldClass("git_repository")
#' @inheritParams git2r::add
#' @export
#' @importFrom git2r workdir add
#' @importFrom assertthat assert_that is.flag
#' @importFrom assertthat assert_that is.flag noNA
write_vc.git_repository <- function(
x, file, root, sorting, strict = TRUE, optimize = TRUE, na = "NA", ...,
stage = FALSE, force = FALSE
) {
assert_that(is.flag(stage), is.flag(force))
assert_that(is.flag(stage), is.flag(force), noNA(stage), noNA(force))
hashes <- write_vc(
x = x, file = file, root = workdir(root), sorting = sorting,
strict = strict, optimize = optimize, na = na, ...
Expand Down
12 changes: 11 additions & 1 deletion man/update_metadata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions tests/testthat/test_d_description.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ test_that("description", {
"character"
)

expect_null(
expect_type(
update_metadata(
file = "test", root = root, field_description = c(
test_character = "Some information", test_factor = "Some information",
test_integer = "Some information"
)
)
),
"character"
)

expect_is({
Expand All @@ -48,12 +49,13 @@ test_that("description", {
git2r::add(root, ".gitignore")
commit(root, "initial commit")

expect_null(
expect_type(
update_metadata(
file = "test", root = root, name = "my_table", title = "My Table",
description = "This is description for the unit tests",
field_description = c(test_character = NA, test_factor = "")
)
),
"character"
)
expect_is({
output <- read_vc("test", root = root)
Expand Down

0 comments on commit 3297092

Please sign in to comment.