/
cash-flow-class.R
63 lines (57 loc) · 1.58 KB
/
cash-flow-class.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
#' Create a CashFlow
#'
#' This allows you to create a `CashFlow` object.
#'
#' @param dates a [Date][Date()] vector with either the same length as `monies`
#' or a vector of length one that is recycled
#' @param monies a [MultiCurrencyMoney][MultiCurrencyMoney()] object
#' @return a `CashFlow` object that extends [tibble::tibble()]
#' @export
#' @examples
#' CashFlow(as.Date("2017-11-15"),
#' MultiCurrencyMoney(list(SingleCurrencyMoney(1, AUD())))
#' )
#' @family money functions
CashFlow <- function(dates, monies) {
assertthat::assert_that(is.MultiCurrencyMoney(monies))
validate_CashFlow(new_CashFlow(dates, monies))
}
new_CashFlow <- function(dates, monies) {
structure(tibble::add_column(
monies,
dates = dates,
.before = 1),
class = c("CashFlow", "tbl_df", "tbl", "data.frame")
)
}
validate_CashFlow <- function(x) {
assertthat::assert_that(
lubridate::is.Date(x$dates),
length(x$dates) == length(x$values) || length(x$dates) == 1
)
x
}
#' Inherits from CashFlow
#'
#' Checks whether object inherits from `CashFlow` class
#'
#' @param x an R object
#' @return `TRUE` if `x` inherits from the `CashFlow` class; otherwise `FALSE`
#' @export
#' @examples
#' is.CashFlow(CashFlow(as.Date("2017-11-15"),
#' MultiCurrencyMoney(list(SingleCurrencyMoney(1, AUD())))))
#' @family money functions
is.CashFlow <- function(x) {
inherits(x, "CashFlow")
}
#' @export
as_tibble.CashFlow <- function(x, ...) {
class(x) <- utils::tail(class(x), -1)
x
}
#' @importFrom tibble tbl_sum
#' @export
tbl_sum.CashFlow <- function(x) {
paste("<CashFlow> of length", nrow(x))
}