Skip to content

Commit

Permalink
Allow start date to be provided in rules map
Browse files Browse the repository at this point in the history
  • Loading branch information
pbogut committed Feb 10, 2018
1 parent 37ebffe commit 58b6016
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions lib/recurring_events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ defmodule RecurringEvents do
[iCal Recurrence rule specification](http://www.kanzaki.com/docs/ical/rrule.html)
[RFC 2445](https://tools.ietf.org/html/rfc2445).
Currently, it is ignoring time but if DateTime or NaiveDateTime structure
is used time will be preserved (see below).
iex> RecurringEvents.take(~D[2016-12-07], %{freq: :daily}, 3)
iex> RecurringEvents.take(%{date_start: ~D[2016-12-07],freq: :daily}, 3)
[~D[2016-12-07], ~D[2016-12-08], ~D[2016-12-09]]
iex> RecurringEvents.take(~N[2016-01-17 12:21:06], %{freq: :weekly}, 2)
[~N[2016-01-17 12:21:06], ~N[2016-01-24 12:21:06]]
Currently supported rules
Supported rules
- `:date_start` - start date can be provided directly in rules map
- `:count` - how many occurences should be return
- `:interval` - how often recurrence rule repeats
- `:freq` - this is the only required rule, possible values: `:yearly`,
Expand Down Expand Up @@ -87,6 +85,20 @@ defmodule RecurringEvents do
do_unfold(date, listify(rules))
end

@doc """
Returns stream of recurring events based on rules
# Example
iex> RecurringEvents.unfold(%{date_start: ~D[2014-06-07], freq: :yearly})
...> |> Enum.take(3)
[~D[2014-06-07], ~D[2015-06-07], ~D[2016-06-07]]
"""
def unfold(%{date_start: date} = rules) do
unfold(date, rules)
end

@doc """
Returns list of recurring events based on date and rules
Expand All @@ -100,6 +112,19 @@ defmodule RecurringEvents do
date |> unfold(rules) |> Enum.take(count)
end

@doc """
Returns list of recurring events based on rules
# Example
iex> RecurringEvents.take(%{date_start: ~D[2015-09-13], freq: :monthly}, 4)
[~D[2015-09-13], ~D[2015-10-13], ~D[2015-11-13], ~D[2015-12-13]]
"""
def take(%{date_start: date} = rules, count) do
take(date, rules, count)
end

defp do_unfold(date, %{freq: freq} = rules) do
date
|> get_freq_module(freq).unfold(rules)
Expand Down

0 comments on commit 58b6016

Please sign in to comment.