1
2which_collate <- function(x) {
3  collate_fields[x]
4}
5
6
7idesc_set_collate <- function(self, private, files, which) {
8  stopifnot(is.character(files), is_collate_field(which))
9  if (length(files) == 0) warning("No files in 'Collate' field")
10
11  idesc_really_set_collate(self, private, files, which_collate(which))
12}
13
14
15idesc_really_set_collate <- function(self, private, files, field) {
16  if (!identical(self$get_collate(), files)) {
17    self$set(field, deparse_collate(files))
18  }
19}
20
21
22idesc_get_collate <- function(self, private, which) {
23  stopifnot(is_collate_field(which))
24  coll <- unname(self$get(which_collate(which)))
25  if (identical(coll, NA_character_)) character() else parse_collate(coll)
26}
27
28
29idesc_del_collate <- function(self, private, which) {
30  stopifnot(is_collate_field_or_all(which))
31
32  if (which == "all") {
33    self$del(collate_fields)
34
35  } else {
36    self$del(collate_fields[which])
37  }
38
39  invisible(self)
40}
41
42
43idesc_add_to_collate <- function(self, private, files, which) {
44  stopifnot(is.character(files), is_collate_field_or_all_or_default(which))
45
46  if (which == "default") {
47    ex_coll <- intersect(collate_fields, self$fields())
48    if (length(ex_coll) == 0) {
49      real_add_to_collate(self, private, which_collate("main"), files)
50    } else {
51      for (ex in ex_coll) real_add_to_collate(self, private, ex, files)
52    }
53
54  } else if (which == "all") {
55    for (coll in collate_fields) {
56      real_add_to_collate(self, private, coll, files)
57    }
58
59  } else {
60    real_add_to_collate(self, private, which_collate(which), files)
61  }
62
63}
64
65## TODO: better order, and support dependencies
66
67real_add_to_collate <- function(self, private, field, files) {
68  ex <- if (!self$has_fields(field)) {
69    character()
70  } else {
71    parse_collate(self$get(field))
72  }
73
74  files <- unique(c(ex, files))
75  idesc_really_set_collate(self, private, files, field)
76}
77
78
79idesc_del_from_collate <- function(self, private, files, which) {
80  stopifnot(is.character(files), is_collate_field_or_all(which))
81
82  if (which == "all") {
83    for (coll in collate_fields) {
84      real_del_from_collate(self, private, coll, files)
85    }
86
87  } else {
88    real_del_from_collate(self, private, which_collate(which), files)
89  }
90}
91
92real_del_from_collate <- function(self, private, field, files) {
93  if (self$has_fields(field)) {
94    coll <- setdiff(parse_collate(self$get(field)), files)
95    idesc_really_set_collate(self, private, coll, field)
96  } else {
97    invisible(self)
98  }
99}
100
101
102parse_collate <- function(str) {
103  scan(
104    text = gsub("\n", " ", str),
105    what = "",
106    strip.white = TRUE,
107    quiet = TRUE
108  )
109}
110
111
112deparse_collate <- function(list) {
113  paste0(
114    "\n",
115    paste0(
116      "    '",
117      list,
118      "'",
119      collapse = "\n"
120    )
121  )
122}
123