Skip to content

Commit b21e5ec

Browse files
committed
Add constructors
1 parent 34939ff commit b21e5ec

File tree

7 files changed

+114
-2
lines changed

7 files changed

+114
-2
lines changed

DESCRIPTION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ Encoding: UTF-8
1515
LazyData: true
1616
URL: https://github.com/hadley/blob
1717
BugReports: https://github.com/hadley/blob/issues
18+
Suggests: testthat
19+
RoxygenNote: 5.0.1.9000

NAMESPACE

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
# Generated by roxygen2: fake comment so roxygen2 overwrites silently.
2-
exportPattern("^[^\\.]")
1+
# Generated by roxygen2: do not edit by hand
2+
3+
S3method(as.blob,character)
4+
S3method(as.blob,integer)
5+
S3method(as.blob,list)
6+
S3method(as.blob,raw)
7+
export(as.blob)
8+
export(blob)
9+
export(new_blob)

R/blob.R

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#' Construct a blob object
2+
#'
3+
#' \code{new_blob} is a low-level constructor that takes a list of
4+
#' raw vectors. \code{blob} constructs a blob from individual raw vectors,
5+
#' and \code{as.blob} is a S3 generic that converts existing objects.
6+
#'
7+
#' @param ... Individual raw vectors
8+
#' @param x A list of raw vectors, or other object to coerce
9+
#' @export
10+
#' @examples
11+
#' x1 <- charToRaw("Good morning")
12+
#' x2 <- as.raw(c(0x48, 0x65, 0x6c, 0x6c, 0x6f))
13+
#'
14+
#' new_blob(list(x1, x2))
15+
#' blob(x1, x2)
16+
#'
17+
#' as.blob(c("Good morning", "Good evening"))
18+
blob <- function(...) {
19+
new_blob(list(...))
20+
}
21+
22+
#' @export
23+
#' @rdname blob
24+
new_blob <- function(x) {
25+
check_blob(x)
26+
structure(x, class = "blob")
27+
}
28+
29+
#' @export
30+
#' @rdname blob
31+
as.blob <- function(x, ...) {
32+
UseMethod("as.blob")
33+
}
34+
35+
#' @export
36+
as.blob.list <- function(x, ...) {
37+
new_blob(x)
38+
}
39+
40+
#' @export
41+
as.blob.raw <- function(x, ...) {
42+
new_blob(list(x))
43+
}
44+
45+
#' @export
46+
as.blob.character <- function(x, ...) {
47+
new_blob(lapply(x, charToRaw))
48+
}
49+
50+
#' @export
51+
as.blob.integer <- function(x, ...) {
52+
new_blob(lapply(x, as.raw))
53+
}

R/util.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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+
}
5+
}

man/blob.Rd

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
library(testthat)
2+
library(blob)
3+
4+
test_check("blob")

tests/testthat/test-construction.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
context("construction")
2+
3+
test_that("input must be list of raw blobs", {
4+
expect_error(new_blob(1), "must be a list of raw vectors")
5+
expect_error(new_blob(list(1)), "must be a list of raw vectors")
6+
expect_error(new_blob(list(1, as.raw(1))), "must be a list of raw vectors")
7+
})

0 commit comments

Comments
 (0)