Skip to contents

Create a scene_action specifying a cookie that must be present (or absent) and optionally a check function for that cookie.

Usage

req_has_cookie(cookie_name, validation_fn = NULL, ..., negate = FALSE)

Arguments

cookie_name

The cookie that must be present, as a length-1 character vector.

validation_fn

A function that takes the value of the cookie as the first parameter, and returns TRUE if the cookie is valid, and FALSE otherwise.

...

Additional parameters passed on to validation_fn.

negate

If TRUE, trigger the corresponding scene when this action is not matched.

Value

A scene_action, to be used in set_scene().

Examples

# Specify an action to detect a cookie named "mycookie".
req_has_cookie("mycookie")
#> $check_fn
#> <partialised>
#> function (...) 
#> ~.fn(cookie_name = ~cookie_name, validation_fn = ~validation_fn, 
#>     ...)
#> <environment: 0x55fc8fb28f00>
#> 
#> $methods
#> [1] "GET"
#> 
#> attr(,"class")
#> [1] "scene_action" "list"        

# Specify an action to detect the *lack* of a cookie named "mycookie".
req_has_cookie("mycookie", negate = TRUE)
#> $check_fn
#> function (...) 
#> !f(...)
#> <bytecode: 0x55fc8fbdbcc0>
#> <environment: 0x55fc8fbdba90>
#> 
#> $methods
#> [1] "GET"
#> 
#> attr(,"class")
#> [1] "scene_action" "list"        

# Specify an action to detect a cookie named "mycookie" that has 27
# characters.
req_has_cookie(
  cookie_name = "mycookie",
  validation_fn = function(cookie_value) {
    nchar(cookie_value == 27)
  }
)
#> $check_fn
#> <partialised>
#> function (...) 
#> ~.fn(cookie_name = ~cookie_name, validation_fn = ~validation_fn, 
#>     ...)
#> <environment: 0x55fc8fc4ac30>
#> 
#> $methods
#> [1] "GET"
#> 
#> attr(,"class")
#> [1] "scene_action" "list"        

# Specify an action to detect a cookie named "mycookie" that has a
# variable-defined number of characters.
expect_n_chars <- function(x, N) {
  nchar(x) == N
}
my_N <- 27 # Perhaps set by an environment variable.
req_has_cookie(
  cookie_name = "mycookie",
  validation_fn = expect_n_chars,
  N = my_N
)
#> $check_fn
#> <partialised>
#> function (...) 
#> ~.fn(cookie_name = ~cookie_name, validation_fn = ~validation_fn, 
#>     N = ~my_N, ...)
#> <environment: 0x55fc8fd88998>
#> 
#> $methods
#> [1] "GET"
#> 
#> attr(,"class")
#> [1] "scene_action" "list"