Skip to content

Commit

Permalink
Add str_to_lower
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Oct 18, 2024
1 parent 2015a19 commit 2864a87
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 6 additions & 1 deletion R/experimental.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ model_field <- function(fn, default = NA, ...) {


# ---
model_config <- function(allow_extra = FALSE, ...) {
model_config <- function(allow_extra = FALSE,
str_to_lower = FALSE, ...) {
return(c(as.list(environment()), list(...)))
}

Expand Down Expand Up @@ -55,6 +56,10 @@ base_model2 <- function(fields = list(), ...,

obj <- validate_fields(obj, .validators_after)

if (isTRUE(.model_config$str_to_lower)) {
obj <- purrr::map_depth(obj, -1, str_to_lower)
}

if (is.environment(obj)) {
return(invisible(obj))
}
Expand Down
11 changes: 8 additions & 3 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ validate_fields <- function(obj, validators) {
}

# ---
str_to_lower <- function(x) {
ifelse(is.character(x), tolower(x), x)
}

# ---
# TODO: Use 'structure' instead
set_attributes <- function(x, ...) {
attributes(x) <- c(attributes(x), list(...))
return(x)
}

# ---
add_class <- function(x, cls = "base_model") {
class(x) <- c(class(x), cls)
return(x)
set_class <- function(x, cls = "base_model") {
structure(x, class = c(class(x), cls))
}

# ---
Expand Down

0 comments on commit 2864a87

Please sign in to comment.