Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ... to set functions #91

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# almanac (development version)

* `runion()`, `rintersect()`, and `rsetdiff()` have all gained `...` which
allows you to provide the relevant rschedules on creation of these rschedules.
This is now the preferred way to create these set-based rschedules (#91).

* New `rcustom()` for creating an rschedule from manually defined event dates
(#90).

Expand Down
26 changes: 9 additions & 17 deletions R/rbundle-set.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@
#' For `rsetdiff()`, the event set is created "from left to right" and depends
#' on the order that the rschedules were added to the bundle.
#'
#' @param ... `[rschedules]`
#'
#' rschedule objects to add to the bundle.
#'
#' @return
#' An empty rbundle.
#' A runion, rintersect, or rsetdiff.
#'
#' @name rbundle-set
#' @seealso [add_rschedule()]
Expand All @@ -43,31 +47,19 @@
#' recur_on_day_of_month(25)
#'
#' # On weekends OR the 25th of the month
#' ru <- runion() %>%
#' add_rschedule(on_weekends) %>%
#' add_rschedule(on_25th)
#'
#' ru <- runion(on_weekends, on_25th)
#' alma_events(ru)
#'
#' # On weekends AND the 25th of the month
#' ri <- rintersect() %>%
#' add_rschedule(on_weekends) %>%
#' add_rschedule(on_25th)
#'
#' ri <- rintersect(on_weekends, on_25th)
#' alma_events(ri)
#'
#' # On weekends AND NOT the 25th of the month
#' rsd1 <- rsetdiff() %>%
#' add_rschedule(on_weekends) %>%
#' add_rschedule(on_25th)
#'
#' rsd1 <- rsetdiff(on_weekends, on_25th)
#' alma_events(rsd1)
#'
#' # On the 25th of the month AND NOT the weekend
#' rsd2 <- rsetdiff() %>%
#' add_rschedule(on_25th) %>%
#' add_rschedule(on_weekends)
#'
#' rsd2 <- rsetdiff(on_25th, on_weekends)
#' alma_events(rsd2)
NULL

Expand Down
4 changes: 0 additions & 4 deletions R/rbundle.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ new_rbundle <- function(rschedules = list(),
class = character()) {
vec_check_list(rschedules)

for (i in seq_along(rschedules)) {
check_rschedule(rschedules[[i]], arg = cli::format_inline("rschedules[[{i}]]"))
}

check_date(rdates)
check_no_missing(rdates)
check_finite(rdates)
Expand Down
6 changes: 4 additions & 2 deletions R/rintersect.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#' @rdname rbundle-set
#' @export
rintersect <- function() {
new_rintersect()
rintersect <- function(...) {
rschedules <- list2(...)
list_check_all_rschedules(rschedules)
new_rintersect(rschedules = rschedules)
}

# ------------------------------------------------------------------------------
Expand Down
15 changes: 15 additions & 0 deletions R/rschedule.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,18 @@ check_rschedule <- function(x,
call = call
)
}

list_check_all_rschedules <- function(x,
...,
arg = caller_arg(x),
call = caller_env()) {
for (i in seq_along(x)) {
check_rschedule(
x = x[[i]],
arg = cli::format_inline("{arg}[[{i}]]"),
call = call
)
}
invisible(NULL)
}

6 changes: 4 additions & 2 deletions R/rsetdiff.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#' @rdname rbundle-set
#' @export
rsetdiff <- function() {
new_rsetdiff()
rsetdiff <- function(...) {
rschedules <- list2(...)
list_check_all_rschedules(rschedules)
new_rsetdiff(rschedules = rschedules)
}

# ------------------------------------------------------------------------------
Expand Down
6 changes: 4 additions & 2 deletions R/runion.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#' @rdname rbundle-set
#' @export
runion <- function() {
new_runion()
runion <- function(...) {
rschedules <- list2(...)
list_check_all_rschedules(rschedules)
new_runion(rschedules = rschedules)
}

# ------------------------------------------------------------------------------
Expand Down
33 changes: 13 additions & 20 deletions man/rbundle-set.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions tests/testthat/_snaps/rbundle.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@
Error in `new_rbundle()`:
! `rschedules` must be a list, not the number 1.

---

Code
new_rbundle(list(1))
Condition
Error in `new_rbundle()`:
! `rschedules[[1]]` must be a <rschedule>, not the number 1.

# validates rdates

Code
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-rbundle.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ test_that("validates rschedules", {
expect_snapshot(error = TRUE, {
new_rbundle(1)
})
expect_snapshot(error = TRUE, {
new_rbundle(list(1))
})
})

test_that("validates rdates", {
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-rintersect.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ test_that("can create an empty rintersect()", {
expect_identical(x$exdates, new_date())
})

test_that("can add rschedules directly into an rintersect()", {
x <- yearly()
y <- daily()
z <- rintersect(x, y)
expect_identical(z$rschedules, list(x, y))
})

test_that("rintersect() generates informative output", {
expect_snapshot({
"# Empty rintersect"
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-rsetdiff.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ test_that("can create an empty rsetdiff()", {
expect_identical(x$exdates, new_date())
})

test_that("can add rschedules directly into an rsetdiff()", {
x <- yearly()
y <- daily()
z <- rsetdiff(x, y)
expect_identical(z$rschedules, list(x, y))
})

test_that("rsetdiff() generates informative output", {
expect_snapshot({
"# Empty rsetdiff"
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-runion.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ test_that("can create an empty runion()", {
expect_identical(x$exdates, new_date())
})

test_that("can add rschedules directly into an runion()", {
x <- yearly()
y <- daily()
z <- runion(x, y)
expect_identical(z$rschedules, list(x, y))
})

test_that("runion() generates informative output", {
expect_snapshot({
"# Empty runion"
Expand Down