Skip to content

Instantly share code, notes, and snippets.

@mpkocher
Last active October 2, 2019 15:22
Show Gist options
  • Save mpkocher/66df08f32a3fbe4c8cf2 to your computer and use it in GitHub Desktop.
Save mpkocher/66df08f32a3fbe4c8cf2 to your computer and use it in GitHub Desktop.
Registry Example
library(methods)
#' @export
ExampleRegistry <- setRefClass("ExampleRegistry", fields=list(namespace="character", driver="character", rtcRunners="list", toolContracts="list"))
registerFunc <- function(r, funcId, func) {
r$rtcRunners[[funcId]] <- func
return(r)
}
registryRunner <- function(r, funcId, name) {
f <- r$rtcRunners[[funcId]]
result <- f(name)
return(result)
}
exampleFunc <- function(name) {
print("Running exampleFunc")
print(paste("User name ", name))
return(0)
}
exampleHello <- function(name) {
print("Runing hello func")
print(paste("My Name is ", name))
return(0)
}
exampleRegistry <- function() {
r <- ExampleRegistry$new(namespace = "my_tool", driver= "driver-base", rtcRunners = list(), toolContracts = list())
registerFunc(r, "f1", exampleFunc)
registerFunc(r, "f2", exampleHello)
return(r)
}
main <- function() {
r <- exampleRegistry()
print(r)
result <- registryRunner(r, "f1", "Func1 Ralph")
print(result)
result <- registryRunner(r, "f2", "Func2 Treefort")
print(result)
return(0)
}
q(status = main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment