Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically detect package name in new_class(package=) #459

Merged
merged 11 commits into from
Oct 16, 2024
8 changes: 8 additions & 0 deletions R/aaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ new_function <- function(args = NULL,
x[[length(x) + 1L]] <- value
x
}

topNamespaceName <- function(env = parent.frame()) {
env <- topenv(env)
if (isNamespace(env))
getNamespaceName(env)
else
NULL
t-kalinowski marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion R/class.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
new_class <- function(
name,
parent = S7_object,
package = NULL,
package = topNamespaceName(parent.frame()),
properties = list(),
abstract = FALSE,
constructor = NULL,
Expand Down
1 change: 0 additions & 1 deletion R/method-register.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ register_S3_method <- function(generic, signature, method, envir = parent.frame(
}

class <- S7_class_name(signature[[1]])
# dbg(generic$name, class, method, envir)
registerS3method(generic$name, class, method, envir)
}

Expand Down
2 changes: 1 addition & 1 deletion R/super.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#' For example, imagine that you have made a subclass of "integer":
#'
#' ```{r}
#' myint <- new_class("myint", parent = class_integer)
#' myint <- new_class("myint", parent = class_integer, package = NULL)
#' ```
#'
#' Now you go to write a custom print method:
Expand Down
3 changes: 3 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' S7_object
S7_object <- new_class(
name = "S7_object",
package = NULL,
parent = NULL,
constructor = function() {
.Call(S7_object_)
Expand Down Expand Up @@ -96,6 +97,7 @@ on_load_define_S7_generic <- function() {
# errors if `@` is not usable.
S7_generic <<- new_class(
name = "S7_generic",
package = NULL,
properties = list(
name = class_character,
methods = class_environment,
Expand All @@ -114,6 +116,7 @@ S7_method <- NULL
on_load_define_S7_method <- function() {
S7_method <<- new_class(
"S7_method",
package = NULL,
parent = class_function,
properties = list(generic = S7_generic, signature = class_list)
)
Expand Down
2 changes: 1 addition & 1 deletion man/new_class.Rd

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

2 changes: 1 addition & 1 deletion man/super.Rd

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

25 changes: 22 additions & 3 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,15 @@ named_list <- function(...) {

`add<-` <- `+`

dbg <- function(..., .display = utils::str) {
dbg <- function(..., .display = utils::str, .file = NULL) {
out <- NULL
exprs <- as.list(substitute(list(...)))[-1L]

if (!is.null(.file)) {
sink(.file, append = TRUE)
on.exit(sink())
}

for (i in seq_len(...length())) {
..i <- as.symbol(sprintf("..%i", i))
if (eval(substitute(missing(..i)))) {
Expand All @@ -111,7 +116,7 @@ dbg <- function(..., .display = utils::str) {
} else {
sprintf("(%s) `%s`", name, expr)
}
cat(label, ": ", sep = "")
cat(label, if (identical(.display, utils::str)) ": " else "\n", sep = "")
.display(out <- eval(..i))
}

Expand Down Expand Up @@ -140,9 +145,23 @@ dbg <- function(..., .display = utils::str) {
cat(loc, "\n")
} else {
cat(sprintf("(from call: %s (srcfile missing))\n", trimws(
deparse1(sys.call(-2), width.cutoff = 60)
deparse1(sys.call(-2) %error% sys.call(-1), width.cutoff = 60)
)))
}

invisible(out)
}

`%error%` <- function(x, y) tryCatch(x, error = function(e) y)

# prevent new_class() from creating `S7::` prefixed S3 class names in tests.
local({
t-kalinowski marked this conversation as resolved.
Show resolved Hide resolved
ns <- asNamespace("S7")
unlockBinding("topNamespaceName", ns)
.topNamespaceName <- ns$topNamespaceName
ns$topNamespaceName <- function(env = parent.frame) {
name <- .topNamespaceName(env)
if (is.null(name) || name == "S7") NULL else name
}
lockBinding("topNamespaceName", ns)
})
Loading