Skip to content

Commit

Permalink
Lazily register slider_plus() and slider_minus() methods (#80)
Browse files Browse the repository at this point in the history
* Lazily register `slider_plus()` and `slider_minus()` methods

* NEWS bullet

* Just use `vctrs::s3_register()`

Since it is fully up to date now in vctrs 0.5.0

* Update slider information now that it is on CRAN
  • Loading branch information
DavisVaughan committed Mar 30, 2023
1 parent 9746bdb commit 9bb3a3b
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Suggests:
covr,
knitr,
rmarkdown,
slider (>= 0.3.0),
testthat (>= 2.1.0)
VignetteBuilder:
knitr
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# almanac (development version)

* almanac steppers created with `stepper()` now work as `.before` and
`.after` arguments of `slider::slide_index()` and friends (#80).

* R >=3.4.0 is now required, which is in line with tidyverse standards.

# almanac 0.1.1
Expand Down
3 changes: 3 additions & 0 deletions R/aaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
rrule_js_path <- system.file("js/rrule.js", package = pkgname)
almanac_global_context$source(rrule_js_path)

vctrs::s3_register("slider::slider_plus", "Date.almanac_stepper", slider_plus.Date.almanac_stepper)
vctrs::s3_register("slider::slider_minus", "Date.almanac_stepper", slider_minus.Date.almanac_stepper)

.Call(export_almanac_init)
}

Expand Down
13 changes: 13 additions & 0 deletions R/stepper.R
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,19 @@ vec_cast.almanac_stepper.almanac_stepper <- function(x, to, ..., x_arg = "", to_
x
}

# ------------------------------------------------------------------------------
# slider_plus() / slider_minus()

# @export - .onLoad()
slider_plus.Date.almanac_stepper <- function(x, y) {
vec_arith("+", x, y)
}

# @export - .onLoad()
slider_minus.Date.almanac_stepper <- function(x, y) {
vec_arith("-", x, y)
}

# ------------------------------------------------------------------------------

stepper_rschedule <- function(x) {
Expand Down
76 changes: 76 additions & 0 deletions tests/testthat/test-stepper.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,79 @@ test_that("steppers are coercible if from the same rschedule", {
expect_error(vec_cast(x, new_stepper()), class = "vctrs_error_incompatible_type")
})

# ------------------------------------------------------------------------------
# slider_plus() / slider_minus()

test_that("`slider_plus()` method is registered", {
skip_if_not_installed("slider", minimum_version = "0.3.0")

y <- workdays(1)

# friday
x <- as.Date("1970-01-09")

expect_identical(
slider::slider_plus(x, y),
as.Date("1970-01-12")
)

# monday
x <- as.Date("1970-01-12")

expect_identical(
slider::slider_plus(x, y),
as.Date("1970-01-13")
)
})

test_that("`slider_minus()` method is registered", {
skip_if_not_installed("slider", minimum_version = "0.3.0")

y <- workdays(1)

# monday
x <- as.Date("1970-01-12")

expect_identical(
slider::slider_minus(x, y),
as.Date("1970-01-09")
)

# friday
x <- as.Date("1970-01-09")

expect_identical(
slider::slider_minus(x, y),
as.Date("1970-01-08")
)
})

test_that("`slide_index()` works with steppers", {
skip_if_not_installed("slider", minimum_version = "0.3.0")

# wednesday -> wednesday
x <- 1:8
i <- as.Date("1970-01-07") + 0:7

out <- slider::slide_index(
.x = x,
.i = i,
.f = identity,
.before = workdays(1),
.after = workdays(1)
)

expect_identical(
out,
list(
1:2, # On Wed, Bounds Tue-Thu
1:3, # On Thu, Bounds Wed-Fri
2:6, # On Fri, Bounds Thu-Mon
3:6, # On Sat, Bounds Fri-Mon
3:6, # On Sun, Bounds Fri-Mon
3:7, # On Mon, Bounds Fri-Tue
6:8, # On Tue, Bounds Mon-Wed
7:8 # On Wed, Bounds Tue-Thu
)
)
})

0 comments on commit 9bb3a3b

Please sign in to comment.