|
| 1 | +# nocov start |
| 2 | + |
| 3 | +.onLoad <- function(libname, pkgname) { |
| 4 | + s3_register("broom::tidy", "model_fit") |
| 5 | +} |
| 6 | + |
| 7 | + |
| 8 | +# vctrs:::s3_register() |
| 9 | +s3_register <- function(generic, class, method = NULL) { |
| 10 | + stopifnot(is.character(generic), length(generic) == 1) |
| 11 | + stopifnot(is.character(class), length(class) == 1) |
| 12 | + |
| 13 | + pieces <- strsplit(generic, "::")[[1]] |
| 14 | + stopifnot(length(pieces) == 2) |
| 15 | + package <- pieces[[1]] |
| 16 | + generic <- pieces[[2]] |
| 17 | + |
| 18 | + caller <- parent.frame() |
| 19 | + |
| 20 | + get_method_env <- function() { |
| 21 | + top <- topenv(caller) |
| 22 | + if (isNamespace(top)) { |
| 23 | + asNamespace(environmentName(top)) |
| 24 | + } else { |
| 25 | + caller |
| 26 | + } |
| 27 | + } |
| 28 | + get_method <- function(method, env) { |
| 29 | + if (is.null(method)) { |
| 30 | + get(paste0(generic, ".", class), envir = get_method_env()) |
| 31 | + } else { |
| 32 | + method |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + method_fn <- get_method(method) |
| 37 | + stopifnot(is.function(method_fn)) |
| 38 | + |
| 39 | + # Always register hook in case package is later unloaded & reloaded |
| 40 | + setHook( |
| 41 | + packageEvent(package, "onLoad"), |
| 42 | + function(...) { |
| 43 | + ns <- asNamespace(package) |
| 44 | + |
| 45 | + # Refresh the method, it might have been updated by `devtools::load_all()` |
| 46 | + method_fn <- get_method(method) |
| 47 | + |
| 48 | + registerS3method(generic, class, method_fn, envir = ns) |
| 49 | + } |
| 50 | + ) |
| 51 | + |
| 52 | + # Avoid registration failures during loading (pkgload or regular) |
| 53 | + if (!isNamespaceLoaded(package)) { |
| 54 | + return(invisible()) |
| 55 | + } |
| 56 | + |
| 57 | + envir <- asNamespace(package) |
| 58 | + |
| 59 | + # Only register if generic can be accessed |
| 60 | + if (exists(generic, envir)) { |
| 61 | + registerS3method(generic, class, method_fn, envir = envir) |
| 62 | + } |
| 63 | + |
| 64 | + invisible() |
| 65 | +} |
| 66 | + |
| 67 | +# nocov end |
| 68 | + |
| 69 | + |
| 70 | + |
1 | 71 | #' ## nocov start
|
2 | 72 | #'
|
3 | 73 | #' data_obj <- ls(pattern = "_data$")
|
|
0 commit comments