Skip to content

Commit

Permalink
Fix #1008
Browse files Browse the repository at this point in the history
Works exactly the same as add_fct() and add_utils().

- add argument 'r6' to add_module()
- add add_r6() helper function calling add_r_files with slightly different parameters
- adjust append_roxygen_comment() to create R6 template
- add man files and NAMESPACE exports
  • Loading branch information
ilyaZar committed May 25, 2023
1 parent 402506b commit 89e7f7c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export(add_js_input_binding)
export(add_js_output_binding)
export(add_module)
export(add_partial_html_template)
export(add_r6)
export(add_resource_path)
export(add_rstudioconnect_file)
export(add_sass_file)
Expand Down
38 changes: 35 additions & 3 deletions R/add_r_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,26 @@ add_utils <- function(
)
}

#' @rdname file_creation
#' @export
add_r6 <- function(
name,
module = NULL,
pkg = get_golem_wd(),
open = TRUE,
dir_create = TRUE,
with_test = FALSE
) {
add_r_files(
name,
module,
ext = "class",
pkg = pkg,
open = open,
dir_create = dir_create,
with_test = with_test
)
}
#' Append roxygen comments to `fct_` and `utils_` files
#'
#' This function add boilerplate roxygen comments
Expand All @@ -164,16 +184,21 @@ append_roxygen_comment <- function(

if (ext == "utils") {
file_type <- "utility"
} else {
} else if (ext == "fct") {
file_type <- "function"
} else {
ext <- paste(ext, "generator")
file_type <- "R6"
}

write_there(sprintf("#' %s ", name))
write_there("#'")
write_there(sprintf("#' @description A %s function", ext))
write_there("#'")
write_there(sprintf("#' @return The return value, if any, from executing the %s.", file_type))
write_there("#'")
if (!(file_type == "R6")) {
write_there(sprintf("#' @return The return value, if any, from executing the %s.", file_type))
write_there("#'")
}
if (export) {
write_there("#' @export")
} else {
Expand All @@ -183,4 +208,11 @@ append_roxygen_comment <- function(
write_there(paste(name, "<- function() {"))
write_there("}")
}
if (file_type == "R6") {
write_there(paste0(name, " <- R6::R6Class("))
write_there(paste0(" classname = '", name, "',"))
write_there(" public = list(")
write_there(" )")
write_there(")")
}
}
6 changes: 6 additions & 0 deletions R/modules_fn.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' @param dir_create Creates the directory if it doesn't exist, default is `TRUE`.
#' @param fct If specified, creates a `mod_fct` file.
#' @param utils If specified, creates a `mod_utils` file.
#' @param r6 If specified, creates a `mod_class` file.
#' @param js,js_handler If specified, creates a module related JavaScript file.
#' @param export Should the module be exported? Default is `FALSE`.
#' @param module_template Function that serves as a module template.
Expand All @@ -31,6 +32,7 @@ add_module <- function(
dir_create = TRUE,
fct = NULL,
utils = NULL,
r6 = NULL,
js = NULL,
js_handler = NULL,
export = FALSE,
Expand Down Expand Up @@ -125,6 +127,10 @@ add_module <- function(
)
}

if (!is.null(r6)) {
add_r6(r6, module = name, open = open)
}

if (with_test) {
use_module_test(
name = name,
Expand Down
3 changes: 3 additions & 0 deletions man/add_module.Rd

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

10 changes: 10 additions & 0 deletions man/file_creation.Rd

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

0 comments on commit 89e7f7c

Please sign in to comment.