R/AssociatedTaxon.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
#' # AssociatedTaxon$new()
#'
#' @format
#' R6 class
#'
#' @title AssociatedTaxon 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 AssociatedTaxon objects.
#'
#'
#' @field name  character
#'
#' @field relationType  character
#'
#'
#'
#' @section Methods:
#' \describe{
#'
#' \item{\code{$new()}}{
#'
#'   Constructor AssociatedTaxon object.
#'
#' }
#' \item{\code{$fromList(AssociatedTaxonList)}}{
#'
#'   Create AssociatedTaxon object from list.
#'
#' }
#'
#' \item{\code{$toList()}}{
#'
#'   Get list representation of AssociatedTaxon.
#'
#' }
#' \item{\code{fromJSONString(AssociatedTaxonJson)}}{
#'
#'   Create AssociatedTaxon object from JSON.
#'
#' }
#' \item{\code{toJSONString(pretty=TRUE)}}{
#'
#'   Get JSON representation of AssociatedTaxon.
#'
#' }
#' }
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
AssociatedTaxon <- R6::R6Class(
  "AssociatedTaxon",
  public = list(
    `name` = NULL,
    `relationType` = NULL,
    initialize = function(
                              `name`,
                              `relationType`) {
      if (!missing(`name`)) {
        stopifnot(
          is.character(`name`),
          length(`name`) == 1
        )
        self[["name"]] <- `name`
      }
      if (!missing(`relationType`)) {
        stopifnot(
          is.character(`relationType`),
          length(`relationType`) == 1
        )
        self[["relationType"]] <- `relationType`
      }
    },
    toList = function() {
      AssociatedTaxonList <- list()
      if (!is.null(self[["name"]])) {
        AssociatedTaxonList[["name"]] <-
          self[["name"]]
      }
      if (!is.null(self[["relationType"]])) {
        AssociatedTaxonList[["relationType"]] <-
          self[["relationType"]]
      }
      ## omit empty nested lists in returned list
      AssociatedTaxonList[vapply(
        AssociatedTaxonList,
        length,
        FUN.VALUE = integer(1)
      ) > 0]
    },
    fromList = function(AssociatedTaxonList,
                            typeMapping = NULL) {
      self[["name"]] <-
        AssociatedTaxonList[["name"]]
      self[["relationType"]] <-
        AssociatedTaxonList[["relationType"]]
      invisible(self)
    },
    toJSONString = function(pretty = TRUE) {
      jsonlite::toJSON(
        self$toList(),
        simplifyVector = TRUE,
        auto_unbox = TRUE,
        pretty = pretty
      )
    },
    fromJSONString = function(AssociatedTaxonJson,
                                  typeMapping = NULL) {
      AssociatedTaxonList <- jsonlite::fromJSON(
        AssociatedTaxonJson,
        simplifyVector = FALSE
      )
      self <- self$fromList(AssociatedTaxonList)
      invisible(self)
    },
    print = function(...) {
      ## print class name
      cat("<AssociatedTaxon>\n")
      ## print all members with values
      cat("Fields:\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")
      }
      if (typeof(self$relationType) == "environment") {
        cat("\trelationType:\tobject of class", paste0("<", class(self$relationType)[1], ">"), "\n")
      }
      else if (typeof(self$relationType) == "list") {
        cat("\trelationType:\tlist of length", length(self$relationType), "\n")
      }
      else {
        cat("\trelationType:\t", self$relationType, "\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.