Skip to content

Commit

Permalink
Docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereckmezquita committed Jul 28, 2024
1 parent 12827e1 commit b0bd7c5
Showing 1 changed file with 72 additions and 5 deletions.
77 changes: 72 additions & 5 deletions R/enum.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
#' Create an enumerated type
#'
#' @description
#' Creates an enumerated type with a fixed set of possible values.
#' Creates an enumerated type with a fixed set of possible values. This function returns an enum generator,
#' which can be used to create enum objects with values restricted to the specified set.
#'
#' @param ... The possible values for the enumerated type.
#' @return A function that creates enum objects of the defined type.
#' @param ... The possible values for the enumerated type. These should be unique character strings.
#' @return A function (enum generator) that creates enum objects of the defined type.
#' @export
#'
#' @examples
#' # Create an enum type for colors
#' Colors <- enum("red", "green", "blue")
#'
#' # Create enum objects
#' my_color <- Colors("red")
#' print(my_color)
#' print(my_color) # Output: Enum: red
#'
#' # Trying to create an enum with an invalid value will raise an error
#' try(Colors("yellow"))
#'
#' # Enums can be used in interfaces
#' ColoredShape <- interface(
#' shape = character,
#' color = Colors
#' )
#'
#' my_shape <- ColoredShape(shape = "circle", color = "red")
#'
#' # Modifying enum values
#' my_shape$color$value <- "blue" # This is valid
#' try(my_shape$color$value <- "yellow") # This will raise an error
#'
#' @seealso \code{\link{interface}} for using enums in interfaces
enum <- function(...) {
values <- c(...)

Expand All @@ -28,18 +49,38 @@ enum <- function(...) {

#' Print method for enum objects
#'
#' @description
#' Prints a human-readable representation of an enum object.
#'
#' @param x An enum object
#' @param ... Additional arguments (not used)
#' @export
#'
#' @examples
#' Colors <- enum("red", "green", "blue")
#' my_color <- Colors("red")
#' print(my_color) # Output: Enum: red
print.enum <- function(x, ...) {
cat("Enum:", x$value, "\n")
}

#' Equality comparison for enum objects
#'
#' @description
#' Compares two enum objects or an enum object with a character value.
#'
#' @param e1 First enum object
#' @param e2 Second enum object or value
#' @param e2 Second enum object or a character value
#' @return Logical value indicating whether the two objects are equal
#' @export
#'
#' @examples
#' Colors <- enum("red", "green", "blue")
#' color1 <- Colors("red")
#' color2 <- Colors("blue")
#' color1 == color2 # FALSE
#' color1 == Colors("red") # TRUE
#' color1 == "red" # TRUE
`==.enum` <- function(e1, e2) {
if (inherits(e2, "enum")) {
e1$value == e2$value
Expand All @@ -50,9 +91,18 @@ print.enum <- function(x, ...) {

#' Get value from enum object
#'
#' @description
#' Retrieves the value of an enum object.
#'
#' @param x An enum object
#' @param name The name of the field to access (should be "value")
#' @return The value of the enum object
#' @export
#'
#' @examples
#' Colors <- enum("red", "green", "blue")
#' my_color <- Colors("red")
#' my_color$value # "red"
`$.enum` <- function(x, name) {
if (name == "value") {
x[["value"]]
Expand All @@ -63,10 +113,20 @@ print.enum <- function(x, ...) {

#' Set value of enum object
#'
#' @description
#' Sets the value of an enum object. The new value must be one of the valid enum values.
#'
#' @param x An enum object
#' @param name The name of the field to set (should be "value")
#' @param value The new value to set
#' @return The updated enum object
#' @export
#'
#' @examples
#' Colors <- enum("red", "green", "blue")
#' my_color <- Colors("red")
#' my_color$value <- "blue" # Valid assignment
#' try(my_color$value <- "yellow") # This will raise an error
`$<-.enum` <- function(x, name, value) {
if (name != "value") {
stop("Cannot add new fields to an enum")
Expand All @@ -80,9 +140,16 @@ print.enum <- function(x, ...) {

#' Print method for enum generators
#'
#' @description
#' Prints a human-readable representation of an enum generator, showing all possible values.
#'
#' @param x An enum generator function
#' @param ... Additional arguments (not used)
#' @export
#'
#' @examples
#' Colors <- enum("red", "green", "blue")
#' print(Colors) # Output: Enum generator: red, green, blue
print.enum_generator <- function(x, ...) {
cat("Enum generator:", paste(attr(x, "values"), collapse = ", "), "\n")
}

0 comments on commit b0bd7c5

Please sign in to comment.