-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDryRunMemory.R
115 lines (94 loc) · 2.22 KB
/
DryRunMemory.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# nocov start
# implicitely tested via utils-fs
DryRunMemory <- R6::R6Class(
"DryRunMemory",
public = list(
initialize = function(){
self$reset()
self$active <- FALSE
self
},
# activate/deactivate
active = NULL,
activate = function(...){
self$active <- TRUE
self$reset()
self
},
deactivate = function(){
self$active <- FALSE
self$reset()
self
},
list = function(path){
x <- path_standardize(list.files(path, full.names = TRUE, all.files = TRUE, no.. = TRUE))
unique(self$fake(x))
},
# file operations
exists = function(...){
files <- c(...)
if (path_standardize(files) %in% self$memory$file){
path_standardize(files) %in% self$fake(files)
} else {
file.exists(files)
}
},
fake = function(x){
xt <- path_standardize(x)
for (i in seq_len(nrow(self$memory))){
op <- self$memory[i, ]
if (identical(op$op, "create")){
x <- c(x, op$file)
xt <- path_standardize(x)
} else if (identical(op$op, "remove")){
x <- x[!xt %in% op$file]
xt <- x
}
}
x
},
memory = NULL,
reset = function(){
self$memory <- data.frame(
op = character(),
file = character(),
stringsAsFactors = FALSE
)
},
create = function(file){
assert(is.character(file))
file <- path_standardize(file)
self$memory <- rbind(
self$memory,
data.frame(
op = "create",
file = file,
stringsAsFactors = FALSE
)
)
rep(TRUE, length(file))
},
remove = function(file){
assert(is.character(file))
file <- path_standardize(file)
self$memory <- rbind(
self$memory,
data.frame(
op = "remove",
file = file,
stringsAsFactors = FALSE
)
)
rep(TRUE, length(file))
},
move = function(from, to){
assert(is.character(from))
assert(is.character(to))
from <- path_standardize(from)
to <- path_standardize(to)
self$remove(from)
self$create(to)
rep(TRUE, length(from))
}
)
)