Skip to content

Commit b2b4b60

Browse files
authored
Rename methods_register() to S7_on_load() (#688)
Keeping `methods_register()` around for backward compatibility. Fixes #615
1 parent ed21054 commit b2b4b60

12 files changed

Lines changed: 40 additions & 19 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export(S7_dispatch)
4444
export(S7_inherits)
4545
export(S7_object)
4646
export(S7_on_build)
47+
export(S7_on_load)
4748
export(as_class)
4849
export(check_is_S7)
4950
export(class_Date)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* `S7_data<-()` now preserves attributes (like `names` or `dim`) from the replacement data instead of carrying over the originals, so resizing the underlying data works correctly (#478).
3636
* `S7_error_method_not_found` now has a correct class vector without a duplicate `"error"` entry (@jjjermiah, #604).
3737
* `S7_inherits()` and `check_is_S7()` now accept any class specification (S7 class, S7 union, S3 class, S4 class, or base type wrapper like `class_integer`), not just S7 classes (#556).
38+
* `S7_on_load()` is the new name for `methods_register()`, giving it a nicer symmetry with `S7_on_build()`; `methods_register()` remains available for backward compatibility (#615).
3839
* `str()` on S7 objects that inherit from data.frame (or other S3 classes whose underlying data has a `dim` attribute incompatible with the bare base type) no longer errors (#494).
3940
* `super()` now works with S3 and S4 objects, not just S7 objects (#500).
4041
* `validate()` now signals validation errors with class `S7_error_validation_failed`, so they can be caught with `tryCatch()` (#602, #605).

R/external-generic.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#' dependency, and sometimes you want a soft dependency, only registering the
1111
#' method if the package is already installed. `new_external_generic()` allows
1212
#' you to provide the minimal needed information about a generic so that methods
13-
#' can be registered at run time, as needed, using [methods_register()].
13+
#' can be registered at run time, as needed, using [S7_on_load()].
1414
#'
1515
#' Note that in tests, you'll need to explicitly call the generic from the
1616
#' external package with `pkg::generic()`.

R/hooks.R

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' @description
44
#' When using S7 in a package, add two hooks to your `zzz.R`:
55
#'
6-
#' * Call `methods_register()` from `.onLoad()`. This is S7's way of
6+
#' * Call `S7_on_load()` from `.onLoad()`. This is S7's way of
77
#' registering methods, rather than using `NAMESPACE` directives like S3 and
88
#' S4 do. It ensures that methods for generics (S3, S4, and S7) defined in
99
#' other packages are registered as needed when your package is loaded. This
@@ -15,21 +15,34 @@
1515
#' after all method registration is complete. This avoids embedding copies
1616
#' of external generics in your package when you use `method<-`.
1717
#'
18+
#' `S7_on_load()` was previously known as `methods_register()`. This function is
19+
#' retained for backward compatibility but new code should use `S7_on_load()`.
20+
#'
1821
#' See `vignette("packages")` for more details.
1922
#'
2023
#' @importFrom utils getFromNamespace packageName
2124
#' @returns Nothing; both functions are called for their side-effects.
2225
#' @examples
2326
#' # In zzz.R:
2427
#' .onLoad <- function(...) {
25-
#' S7::methods_register()
28+
#' S7::S7_on_load()
2629
#' }
2730
#' S7::S7_on_build()
2831
#' @export
32+
S7_on_load <- function() {
33+
S7_on_load_(parent.frame())
34+
}
35+
36+
#' @export
37+
#' @rdname S7_on_load
2938
methods_register <- function() {
30-
package <- packageName(parent.frame())
31-
ns <- topenv(parent.frame())
32-
# TODO?: check/enforce that methods_register() is being called from .onLoad()
39+
S7_on_load_(parent.frame())
40+
}
41+
42+
S7_on_load_ <- function(env) {
43+
package <- packageName(env)
44+
ns <- topenv(env)
45+
# TODO?: check/enforce that S7_on_load() is being called from .onLoad()
3346

3447
tbl <- S7_methods_table(package)
3548

@@ -46,7 +59,7 @@ methods_register <- function() {
4659
}
4760

4861
#' @export
49-
#' @rdname methods_register
62+
#' @rdname S7_on_load
5063
S7_on_build <- function() {
5164
strip_generic_sentinels(topenv(parent.frame()))
5265
}

R/method-register.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#' But this is not a general method registration function: at least one of
1313
#' `generic` and `signature` needs to be from S7.
1414
#'
15-
#' Note that if you are writing a package, you must call [methods_register()]
15+
#' Note that if you are writing a package, you must call [S7_on_load()]
1616
#' in your `.onLoad`. This ensures that all methods are dynamically registered
1717
#' when needed.
1818
#'

_pkgdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ reference:
4040
Functions needed when using S7 within a package. See `vignette("packages")`
4141
for more details.
4242
contents:
43-
- methods_register
43+
- S7_on_load
4444
- new_external_generic
4545

4646
- title: Compatibility
Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/method-set.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/new_external_generic.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/t2/R/t2.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ S7::method(another_s3_generic, an_s7_class) <- function(x) "foo"
3535

3636

3737
.onLoad <- function(libname, pkgname) {
38-
S7::methods_register()
38+
S7::S7_on_load()
3939
}

0 commit comments

Comments
 (0)