/
as-casette.R
57 lines (49 loc) · 1.42 KB
/
as-casette.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#' Coerce names, etc. to cassettes
#'
#' @export
#' @param x Input, a cassette name (character), or something that
#' can be coerced to a cassette
#' @param ... further arguments passed on to [cassettes()] or
#' [read_cassette_meta()
#' @return a cassette of class `Cassette`
#' @examples \dontrun{
#' vcr_configure(dir = tempfile())
#' insert_cassette("foobar")
#' cassettes(on_disk = FALSE)
#' cassettes(on_disk = TRUE)
#' as.cassette("foobar", on_disk = FALSE)
#' eject_cassette() # eject the current cassette
#'
#' # cleanup
#' unlink(file.path(tempfile(), "foobar.yml"))
#' }
as.cassette <- function(x, ...) UseMethod("as.cassette")
#' @export
as.cassette.default <- function(x, ...) {
stop("no 'as.cassette' method for ", class(x), call. = FALSE)
}
#' @export
as.cassette.cassette <- function(x, ...) x
#' @export
as.cassette.character <- function(x, ...) {
cassettes(...)[[x]]
}
#' @export
as.cassette.cassettepath <- function(x, ...) read_cassette_meta(x, ...)
#' @export
as.cassette.list <- function(x, ...) lapply(x, as.cassette, ...)
#' Coerce to a cassette path
#'
#' @export
#' @rdname as.cassette
as.cassettepath <- function(x) UseMethod("as.cassettepath")
#' @export
as.cassettepath.character <- function(x) {
if (file.exists(x)) {
structure(x, class = "cassettepath")
} else {
stop("Path not found", call. = FALSE)
}
}
#' @export
print.cassettepath <- function(x, ...) cat(paste0("<cassette path>"), x[[1]])