Get the provider function of a module.

get_provider(name = .Last.name, load = FALSE)

Arguments

name

A string (character vector of lenght one).

A module name can contain letters, figures and some special characters, namely _, -, and /. The latter is a namespace separator.

Names containing /mock/, /mocks/, /test/, /tests/, /example/, or /examples/ have a special meaning related to code testing and examples.

The name "modulr" corresponds to a special module and is therefore reserved.

load

A flag. Should an undefined module be implicitely loaded?

Value

A function identical to the provider function of the module.

Details

For testing purposes, it is often useful for mocks to be able to refer to the provider of a module.

Warning

It is considered a very bad practice to define, touch, undefine, load, make, reset, or perform any other operation from within a module definition that may alterate the internal state of modulr.

See also

define, make, reset, and root_config.

Examples

#> [2018-12-02T17:13:59 CET] Resetting modulr state ... OK
define("foo", NULL, function() "foo")
#> [2018-12-02T17:13:59 CET] Defining 'foo' ... OK
define("bar", list(foo = "foo"), function(foo) paste0(foo, "bar"))
#> [2018-12-02T17:13:59 CET] Defining 'bar' ... OK
define("foo/mock", NULL, function() "foooooo")
#> [2018-12-02T17:13:59 CET] Defining 'foo/mock' ... OK
define("bar/mock", list(foo = "foo/mock"), get_provider("bar"))
#> [2018-12-02T17:13:59 CET] Defining 'bar/mock' ... OK
make("bar/mock")
#> [2018-12-02T17:13:59 CET] Making 'bar/mock' ... #> [2018-12-02T17:13:59 CET] * Visiting and defining dependencies ... #> [2018-12-02T17:13:59 CET] * Constructing dependency graph ... OK #> [2018-12-02T17:13:59 CET] * Evaluating #1/1 (layer #1/1): 'foo/mock' ... #> [2018-12-02T17:13:59 CET] DONE ('bar/mock' in 0.046 secs)
#> [1] "foooooobar"
#> [2018-12-02T17:13:59 CET] Resetting modulr state ... OK
tmp_dir <- tempfile("modulr_") dir.create(tmp_dir) tmp_file <- file.path(tmp_dir, "foo.R") cat('define("foo", NULL, function() "Hello World!")', file = tmp_file) root_config$set(tmp_dir) ## Not run: get_provider("foo", load = FALSE) get_provider("foo", load = TRUE)
#> [2018-12-02T17:13:59 CET] Defining 'foo' ... OK
#> function() "Hello World!" #> <environment: 0x9577d68>
unlink(tmp_dir, recursive = TRUE)