r.modelEnum.mustache Maven / Gradle / Ivy
#' @docType class
#' @title {{classname}}
#' @description {{classname}} Class
#' @format An \code{R6Class} generator object
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
{{#allowableValues}}
{{classname}} <- R6::R6Class(
"{{classname}}",
public = list(
#' Initialize a new {{{classname}}} class.
#'
#' @description
#' Initialize a new {{{classname}}} class.
#'
#' @param ... Optional arguments.
#' @export
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
},
#' To JSON string
#'
#' @description
#' To JSON String
#'
#' @return {{{classname}}} in JSON format
#' @export
toJSON = function() {
jsonlite::toJSON(private$value, auto_unbox = TRUE)
},
#' Deserialize JSON string into an instance of {{{classname}}}
#'
#' @description
#' Deserialize JSON string into an instance of {{{classname}}}
#'
#' @param input_json the JSON input
#' @return the instance of {{{classname}}}
#' @export
fromJSON = function(input_json) {
private$value <- jsonlite::fromJSON(input_json,
simplifyVector = FALSE)
self
},
#' To JSON string
#'
#' @description
#' To JSON String
#'
#' @return {{{classname}}} in JSON format
#' @export
toJSONString = function() {
as.character(jsonlite::toJSON(private$value,
auto_unbox = TRUE))
},
#' Deserialize JSON string into an instance of {{{classname}}}
#'
#' @description
#' Deserialize JSON string into an instance of {{{classname}}}
#'
#' @param input_json the JSON input
#' @return the instance of {{{classname}}}
#' @export
fromJSONString = function(input_json) {
private$value <- jsonlite::fromJSON(input_json,
simplifyVector = FALSE)
self
}
),
private = list(
value = NULL
)
)
# add to utils.R
.parse_{{name}} <- function(vals) {
res <- gsub("^\\[|\\]$", "", "{{{values}}}")
unlist(strsplit(res, ", "))
}
{{/allowableValues}}