Simple wrapper around decorator$new(object, exists)

decorate(object, decorators, exists = c("skip", "error", "overwrite"), ...)

Arguments

object

[R6::R6Class]
R6 class to decorate.

decorators

([DecorateClass]|character())
One or more decorators (by name or class) to decorate with.

exists

(character(1)
Expected behaviour if method exists in object and decorator. One of: 1. exists = "error" (default) - This will throw an error and prevent the object being decorated. 2. exists = "skip" - This will decorate the object with all fields/methods that don't already exist. 3. exists = "overwrite" - This will decorate the object with all fields/methods from the decorator and overwrite ones with the same name if they already exist.

...

ANY
Additional arguments passed to get.

See also

Examples

library(R6)

## Define decorators
dec1 <- DecoratorClass("dec1", public = list(goodbye = "Goodbye World"))
dec2 <- DecoratorClass("dec2", public = list(goodbye2 = "Goodbye World 2"))

oop <- ooplah$new()
oop$goodbye
#> NULL
dec_oop <- decorate(oop, c(dec1, dec2))
dec_oop$goodbye
#> [1] "Goodbye World"
dec_oop$goodbye2
#> [1] "Goodbye World 2"

## Equivalently
oop <- ooplah$new()
decorate(oop, c("dec1", "dec2"))
#> Error in get(decorator, ...): object 'dec1' not found