Skip to content

Commit 52fc626

Browse files
committed
Add accessor methods
1 parent c6e7359 commit 52fc626

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method("[",blob)
4+
S3method("[<-",blob)
5+
S3method("[[<-",blob)
36
S3method(as.blob,character)
47
S3method(as.blob,integer)
58
S3method(as.blob,list)

R/accessors.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#' @export
2+
`[.blob` <- function(x, i, ...) {
3+
new_blob(NextMethod())
4+
}
5+
6+
#' @export
7+
`[<-.blob` <- function(x, i, ..., value) {
8+
if (!is_raw_list(value)) {
9+
stop("RHS must be list of raw vectors", call. = FALSE)
10+
}
11+
12+
NextMethod()
13+
}
14+
15+
#' @export
16+
`[[<-.blob` <- function(x, i, ..., value) {
17+
if (!is.raw(value)) {
18+
stop("RHS must be raw vector", call. = FALSE)
19+
}
20+
21+
NextMethod()
22+
}

R/blob.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ blob <- function(...) {
2222
#' @export
2323
#' @rdname blob
2424
new_blob <- function(x) {
25-
check_blob(x)
25+
if (!is_raw_list(x)) {
26+
stop("`x` must be a list of raw vectors", call. = FALSE)
27+
}
2628
structure(x, class = "blob")
2729
}
2830

R/util.R

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
check_blob <- function(x) {
2-
if (!is.list(x) || !all(vapply(x, is.raw, logical(1)))) {
3-
stop("`x` must be a list of raw vectors", call. = FALSE)
4-
}
1+
is_raw_list <- function(x) {
2+
if (!is.list(x))
3+
return(FALSE)
4+
5+
all_raw <- all(vapply(x, is.raw, logical(1)))
6+
if (!all_raw)
7+
return(FALSE)
8+
9+
TRUE
510
}

tests/testthat/test-accessors.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
context("accessors")
2+
3+
test_that("subsetting blob returns blob", {
4+
x <- as.blob(1:5)
5+
expect_s3_class(x[1], "blob")
6+
})
7+
8+
test_that("can't insert objects of incorrect type", {
9+
x <- as.blob(1:5)
10+
11+
expect_error(x[[1]] <- 1, "must be raw vector")
12+
expect_error(x[1] <- 1, "must be list of raw vectors")
13+
})

0 commit comments

Comments
 (0)