Skip to content

Commit

Permalink
Credo Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen committed Jan 20, 2017
1 parent 55ecca3 commit 4a3d00c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
23 changes: 14 additions & 9 deletions lib/crontab/cron_expression.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Crontab.CronExpression do
This is the Crontab.CronExpression module / struct.
"""

alias Crontab.CronExpression.Parser

@type t :: %Crontab.CronExpression{}
@type interval :: :minute | :hour | :day | :month | :weekday | :year
@type min_max :: {:-, time_unit, time_unit}
Expand Down Expand Up @@ -75,8 +77,8 @@ defmodule Crontab.CronExpression do
"""
@spec sigil_e(binary, charlist) :: t
def sigil_e(cron_expression, options)
def sigil_e(cron_expression, [?e]), do: Crontab.CronExpression.Parser.parse!(cron_expression, true)
def sigil_e(cron_expression, _options), do: Crontab.CronExpression.Parser.parse!(cron_expression, false)
def sigil_e(cron_expression, [?e]), do: Parser.parse!(cron_expression, true)
def sigil_e(cron_expression, _options), do: Parser.parse!(cron_expression, false)

@doc """
Convert Crontab.CronExpression struct to Tuple List
Expand All @@ -103,19 +105,22 @@ defmodule Crontab.CronExpression do
{:year, [6]}]
"""
@spec to_condition_list(t) :: condition_list
def to_condition_list(interval = %Crontab.CronExpression{extended: false}) do
def to_condition_list(interval = %__struct__{extended: false}) do
[{:minute, interval.minute},
{:hour, interval.hour},
{:day, interval.day},
{:month, interval.month},
{:weekday, interval.weekday},
{:year, interval.year}]
end
def to_condition_list(interval = %Crontab.CronExpression{}) do
def to_condition_list(interval = %__struct__{}) do
[{:second, interval.second} | to_condition_list(%{interval | extended: false})]
end

defimpl Inspect do
alias Crontab.CronExpression.Composer
alias Crontab.CronExpression

@doc """
Pretty Print Cron Expressions
Expand All @@ -129,12 +134,12 @@ defmodule Crontab.CronExpression do
~e[* * * * * * *]e
"""
@spec inspect(Crontab.CronExpression.t, any) :: String.t
def inspect(cron_expression = %Crontab.CronExpression{extended: false}, _options) do
"~e[" <> Crontab.CronExpression.Composer.compose(cron_expression) <> "]"
@spec inspect(CronExpression.t, any) :: String.t
def inspect(cron_expression = %__struct__{extended: false}, _options) do
"~e[" <> Composer.compose(cron_expression) <> "]"
end
def inspect(cron_expression = %Crontab.CronExpression{extended: true}, _options) do
"~e[" <> Crontab.CronExpression.Composer.compose(cron_expression) <> "]e"
def inspect(cron_expression = %__struct__{extended: true}, _options) do
"~e[" <> Composer.compose(cron_expression) <> "]e"
end
end
end
24 changes: 16 additions & 8 deletions lib/crontab/scheduler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ defmodule Crontab.Scheduler do
~N[2016-12-17 00:02:00]
]}
iex> Crontab.Scheduler.get_next_run_dates(3, %Crontab.CronExpression{year: [2017], month: [1], day: [1], hour: [0], minute: [1]}, ~N[2016-12-17 00:00:00])
iex> Crontab.Scheduler.get_next_run_dates(3,
...> %Crontab.CronExpression{year: [2017], month: [1], day: [1], hour: [0], minute: [1]}, ~N[2016-12-17 00:00:00])
{:error, [~N[2017-01-01 00:01:00]], "No compliant date was found for your interval."}
"""
Expand Down Expand Up @@ -108,7 +109,8 @@ defmodule Crontab.Scheduler do
~N[2016-12-17 00:02:00]
]
iex> Crontab.Scheduler.get_next_run_dates!(3, %Crontab.CronExpression{year: [2017], month: [1], day: [1], hour: [0], minute: [1]}, ~N[2016-12-17 00:00:00])
iex> Crontab.Scheduler.get_next_run_dates!(3,
...> %Crontab.CronExpression{year: [2017], month: [1], day: [1], hour: [0], minute: [1]}, ~N[2016-12-17 00:00:00])
** (RuntimeError) No compliant date was found for your interval.
"""
Expand All @@ -121,7 +123,8 @@ defmodule Crontab.Scheduler do
end
end

@spec _get_next_run_dates(pos_integer, CronExpression.t, NaiveDateTime.t, list) :: {:ok | :error, [NaiveDateTime.t], binary}
@spec _get_next_run_dates(pos_integer, CronExpression.t, NaiveDateTime.t, list) ::
{:ok | :error, [NaiveDateTime.t], binary}
defp _get_next_run_dates(0, _, _, list), do: {:ok, Enum.reverse list}
defp _get_next_run_dates(n, cron_expression = %CronExpression{extended: false}, date, head) do
case get_next_run_date(cron_expression, date) do
Expand Down Expand Up @@ -209,11 +212,13 @@ defmodule Crontab.Scheduler do
~N[2016-12-16 23:58:00]
]}
iex> Crontab.Scheduler.get_previous_run_dates(3, %Crontab.CronExpression{year: [2016], month: [1], day: [1], hour: [0], minute: [1]}, ~N[2016-12-17 00:00:00])
iex> Crontab.Scheduler.get_previous_run_dates(3,
...> %Crontab.CronExpression{year: [2016], month: [1], day: [1], hour: [0], minute: [1]}, ~N[2016-12-17 00:00:00])
{:error, [~N[2016-01-01 00:01:00]], "No compliant date was found for your interval."}
"""
@spec get_previous_run_dates(pos_integer, CronExpression.t, NaiveDateTime.t) :: {:ok | :error, [NaiveDateTime.t], binary}
@spec get_previous_run_dates(pos_integer, CronExpression.t, NaiveDateTime.t) ::
{:ok | :error, [NaiveDateTime.t], binary}
def get_previous_run_dates(n, cron_expression, date \\ DateTime.to_naive(DateTime.utc_now))
def get_previous_run_dates(n, cron_expression, date), do: _get_previous_run_dates(n, cron_expression, date, [])

Expand All @@ -223,7 +228,8 @@ defmodule Crontab.Scheduler do
### Examples
iex> Crontab.Scheduler.get_previous_run_dates!(3, %Crontab.CronExpression{extended: true}, ~N[2016-12-17 00:00:00])
iex> Crontab.Scheduler.get_previous_run_dates!(3,
...> %Crontab.CronExpression{extended: true}, ~N[2016-12-17 00:00:00])
[
~N[2016-12-17 00:00:00],
~N[2016-12-16 23:59:59],
Expand All @@ -237,7 +243,8 @@ defmodule Crontab.Scheduler do
~N[2016-12-16 23:58:00]
]
iex> Crontab.Scheduler.get_previous_run_dates!(3, %Crontab.CronExpression{year: [2017], month: [1], day: [1], hour: [0], minute: [1]}, ~N[2016-12-17 00:00:00])
iex> Crontab.Scheduler.get_previous_run_dates!(3,
...> %Crontab.CronExpression{year: [2017], month: [1], day: [1], hour: [0], minute: [1]}, ~N[2016-12-17 00:00:00])
** (RuntimeError) No compliant date was found for your interval.
"""
Expand All @@ -250,7 +257,8 @@ defmodule Crontab.Scheduler do
end
end

@spec _get_previous_run_dates(pos_integer, CronExpression.t, NaiveDateTime.t, list) :: {:ok | :error, [NaiveDateTime.t], binary}
@spec _get_previous_run_dates(pos_integer, CronExpression.t, NaiveDateTime.t, list) ::
{:ok | :error, [NaiveDateTime.t], binary}
defp _get_previous_run_dates(0, _, _, list), do: {:ok, Enum.reverse list}
defp _get_previous_run_dates(n, cron_expression = %CronExpression{extended: false}, date, head) do
case get_previous_run_date(cron_expression, date) do
Expand Down

0 comments on commit 4a3d00c

Please sign in to comment.