R/Monomial.r

# Netherlands Biodiversity API
#
# Access to the digitised Natural History collection at the Naturalis Biodiversity Center
#
# OpenAPI spec version: v2
# Contact: support@naturalis.nl
# Generated by: https://github.com/swagger-api/swagger-codegen.git

#'
#' @docType class
#'
#' @format R6 class
#'
#' @usage
#' # Monomial$new()
#'
#' @format
#' R6 class
#'
#' @title Monomial Class
#'
#' @description
#' For more information on the NBA object model, please refer to the
#' official NBA documentation at
#' \href{https://docs.biodiversitydata.nl}{https://docs.biodiversitydata.nl} and
#' the NBA model and endpoints reference at
#' \href{https://docs.biodiversitydata.nl/endpoints-reference}{https://docs.biodiversitydata.nl/endpoints-reference}.
#'
#' @details Model class for Monomial objects.
#'
#'
#' @field rank  character
#'
#' @field name  character
#'
#'
#'
#' @section Methods:
#' \describe{
#'
#' \item{\code{$new()}}{
#'
#'   Constructor Monomial object.
#'
#' }
#' \item{\code{$fromList(MonomialList)}}{
#'
#'   Create Monomial object from list.
#'
#' }
#'
#' \item{\code{$toList()}}{
#'
#'   Get list representation of Monomial.
#'
#' }
#' \item{\code{fromJSONString(MonomialJson)}}{
#'
#'   Create Monomial object from JSON.
#'
#' }
#' \item{\code{toJSONString(pretty=TRUE)}}{
#'
#'   Get JSON representation of Monomial.
#'
#' }
#' }
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
Monomial <- R6::R6Class(
  "Monomial",
  public = list(
    `rank` = NULL,
    `name` = NULL,
    initialize = function(
                              `rank`,
                              `name`) {
      if (!missing(`rank`)) {
        stopifnot(
          is.character(`rank`),
          length(`rank`) == 1
        )
        self[["rank"]] <- `rank`
      }
      if (!missing(`name`)) {
        stopifnot(
          is.character(`name`),
          length(`name`) == 1
        )
        self[["name"]] <- `name`
      }
    },
    toList = function() {
      MonomialList <- list()
      if (!is.null(self[["rank"]])) {
        MonomialList[["rank"]] <-
          self[["rank"]]
      }
      if (!is.null(self[["name"]])) {
        MonomialList[["name"]] <-
          self[["name"]]
      }
      ## omit empty nested lists in returned list
      MonomialList[vapply(
        MonomialList,
        length,
        FUN.VALUE = integer(1)
      ) > 0]
    },
    fromList = function(MonomialList,
                            typeMapping = NULL) {
      self[["rank"]] <-
        MonomialList[["rank"]]
      self[["name"]] <-
        MonomialList[["name"]]
      invisible(self)
    },
    toJSONString = function(pretty = TRUE) {
      jsonlite::toJSON(
        self$toList(),
        simplifyVector = TRUE,
        auto_unbox = TRUE,
        pretty = pretty
      )
    },
    fromJSONString = function(MonomialJson,
                                  typeMapping = NULL) {
      MonomialList <- jsonlite::fromJSON(
        MonomialJson,
        simplifyVector = FALSE
      )
      self <- self$fromList(MonomialList)
      invisible(self)
    },
    print = function(...) {
      ## print class name
      cat("<Monomial>\n")
      ## print all members with values
      cat("Fields:\n")
      if (typeof(self$rank) == "environment") {
        cat("\trank:\tobject of class", paste0("<", class(self$rank)[1], ">"), "\n")
      }
      else if (typeof(self$rank) == "list") {
        cat("\trank:\tlist of length", length(self$rank), "\n")
      }
      else {
        cat("\trank:\t", self$rank, "\n")
      }
      if (typeof(self$name) == "environment") {
        cat("\tname:\tobject of class", paste0("<", class(self$name)[1], ">"), "\n")
      }
      else if (typeof(self$name) == "list") {
        cat("\tname:\tlist of length", length(self$name), "\n")
      }
      else {
        cat("\tname:\t", self$name, "\n")
      }
      ## print all methods
      cat("Methods:\n")
      cat("\tfromJSONString\n")
      cat("\ttoJSONString\n")
      cat("\tfromList\n")
      cat("\ttoList\n")
      cat("\tprint\n")
      invisible(self)
    }
  )
)
naturalis/nbaR documentation built on Nov. 12, 2023, 4:47 p.m.