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