All Downloads are FREE. Search and download functionalities are using the official Maven repository.

r.modelEnum.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
#' @export
{{#allowableValues}}
{{classname}} <- R6::R6Class(
    "{{classname}}",
    public = list(
        initialize = function(...) {
            local.optional.var <- list(...)
            val <- unlist(local.optional.var)
            enumvec <- .parse_{{name}}()

            stopifnot(length(val) == 1L)

            if (!val %in% enumvec)
                stop("Use one of the valid values: ",
                    paste0(enumvec, collapse = ", "))
            private$value <- val
        },
        toJSON = function() {
            jsonlite::toJSON(private$value, auto_unbox = TRUE)
        },
        fromJSON = function({{classname}}Json) {
            private$value <- jsonlite::fromJSON({{classname}}Json,
                simplifyVector = FALSE)
            self
        },
        toJSONString = function() {
            as.character(jsonlite::toJSON(private$value,
                auto_unbox = TRUE))
        },
        fromJSONString = function({{classname}}Json) {
            private$value <- jsonlite::fromJSON({{classname}}Json,
                simplifyVector = FALSE)
            self
        }
    ),
    private = list(
        value = NULL
    )
)

# add to utils.R
.parse_{{name}} <- function(vals) {
    res <- gsub("^\\[|\\]$", "",
        "{{{values}}}"
    )
    unlist(strsplit(res, ", "))
}
{{/allowableValues}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy