Skip to content

Commit 5182c0c

Browse files
committed
use factory pattern
1 parent 5e91780 commit 5182c0c

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

R/fread.R

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
# nocov start
2-
# S3 generic for reopening connections in binary mode
3-
reopen_connection = function(con, description, ...) {
4-
UseMethod("reopen_connection")
2+
# S3 generic that returns a function to open connections in binary mode
3+
connection_opener = function(con, ...) {
4+
UseMethod("connection_opener")
55
}
66

7-
reopen_connection.default = function(con, description, ...) {
7+
connection_opener.default = function(con, ...) {
88
con_class = class1(con)
99
stopf("Don't know how to reopen connection type '%s'. Need a connection opened in binary mode to continue.", con_class)
1010
}
1111

12-
reopen_connection.file = function(con, description, ...) {
13-
file(description, "rb")
12+
connection_opener.file = function(con, ...) {
13+
function(description) file(description, "rb", ...)
1414
}
1515

16-
reopen_connection.gzfile = function(con, description, ...) {
17-
gzfile(description, "rb")
16+
connection_opener.gzfile = function(con, ...) {
17+
function(description) gzfile(description, "rb", ...)
1818
}
1919

20-
reopen_connection.bzfile = function(con, description, ...) {
21-
bzfile(description, "rb")
20+
connection_opener.bzfile = function(con, ...) {
21+
function(description) bzfile(description, "rb", ...)
2222
}
2323

24-
reopen_connection.url = function(con, description, ...) {
25-
url(description, "rb")
24+
connection_opener.url = function(con, ...) {
25+
function(description) url(description, "rb", ...)
2626
}
2727

28-
reopen_connection.unz = function(con, description, ...) {
29-
unz(description, "rb")
28+
connection_opener.unz = function(con, ...) {
29+
function(description) unz(description, "rb", ...)
3030
}
3131

32-
reopen_connection.pipe = function(con, description, ...) {
33-
pipe(description, "rb")
32+
connection_opener.pipe = function(con, ...) {
33+
function(description) pipe(description, "rb", ...)
3434
}
3535
# nocov end
3636

@@ -146,7 +146,7 @@ yaml=FALSE, tmpdir=tempdir(), tz="UTC")
146146

147147
if (needs_reopen) {
148148
close(input)
149-
input = reopen_connection(input, con_summary$description)
149+
input = connection_opener(input)(con_summary$description)
150150
close_con = input
151151
} else if (!con_open) {
152152
open(input, "rb")

man/connection_opener.Rd

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
\name{connection_opener}
2+
\alias{connection_opener}
3+
\title{ Create a function to open connections in binary mode }
4+
\description{
5+
S3 generic that returns a function to open a connection in binary read mode. Used internally by \code{fread}. Exported so packages with custom connection classes can define methods.
6+
}
7+
\usage{
8+
connection_opener(con, ...)
9+
}
10+
\arguments{
11+
\item{con}{ A connection object. }
12+
\item{...}{ Additional arguments passed to the connection constructor. }
13+
}
14+
\details{
15+
Returns a function that accepts a description argument and opens a connection in binary read mode (\code{"rb"}). Methods are provided for \code{file}, \code{gzfile}, \code{bzfile}, \code{url}, \code{unz} and \code{pipe} connections.
16+
17+
To support custom connection types with \code{fread}, define a method for your connection class that returns an opener function.
18+
}
19+
\value{
20+
A function that accepts a description argument and returns a connection object opened in binary read mode.
21+
}
22+
\examples{
23+
\dontrun{
24+
# Define a method for a custom connection class
25+
connection_opener.my_con = function(con, ...) {
26+
function(description) my_con(description, mode = "rb", ...)
27+
}
28+
}
29+
}
30+
\seealso{
31+
\code{\link{fread}}
32+
}
33+
\keyword{ data }

man/reopen_connection.Rd

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)