Skip to content

Commit

Permalink
Cron Expression Sigil (#23)
Browse files Browse the repository at this point in the history
* EronExpression Sigil

* Make Tests Async
  • Loading branch information
maennchen committed Jan 20, 2017
1 parent 2156b30 commit 73ddf1b
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 19 deletions.
94 changes: 80 additions & 14 deletions lib/crontab/cron_expression.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ defmodule Crontab.CronExpression do
This is the Crontab.CronExpression module / struct.
"""

@type t :: %Crontab.CronExpression{}
@type interval :: :minute | :hour | :day | :month | :weekday | :year
@type min_max :: {:-, time_unit, time_unit}
@type value :: time_unit | :* | :L | {:L, value} | {:/, time_unit | :*
| min_max, pos_integer} | min_max | {:W, time_unit | :L}
@type minute :: 0..59
@type hour :: 0..23
@type day :: 0..31
@type month :: 1..12
@type weekday :: 0..7
@type year :: integer
@type time_unit :: minute | hour | day | month | weekday | year
@type condition :: {interval, [value]}
@type condition_list :: [condition]

@doc """
Defines the Cron Interval
Expand All @@ -20,20 +35,48 @@ defmodule Crontab.CronExpression do
"""
defstruct extended: false, second: [:*], minute: [:*], hour: [:*], day: [:*], month: [:*], weekday: [:*], year: [:*]

@type t :: %Crontab.CronExpression{}
@type interval :: :minute | :hour | :day | :month | :weekday | :year
@type min_max :: {:-, time_unit, time_unit}
@type value :: time_unit | :* | :L | {:L, value} | {:/, time_unit | :*
| min_max, pos_integer} | min_max | {:W, time_unit | :L}
@type minute :: 0..59
@type hour :: 0..23
@type day :: 0..31
@type month :: 1..12
@type weekday :: 0..7
@type year :: integer
@type time_unit :: minute | hour | day | month | weekday | year
@type condition :: {interval, [value]}
@type condition_list :: [condition]
@doc """
Create a `%Crontab.CronExpression{}` via sigil.
### Examples
iex> ~e[*]
%Crontab.CronExpression{
extended: false,
second: [:*],
minute: [:*],
hour: [:*],
day: [:*],
month: [:*],
weekday: [:*],
year: [:*]}
iex> ~e[*]e
%Crontab.CronExpression{
extended: true,
second: [:*],
minute: [:*],
hour: [:*],
day: [:*],
month: [:*],
weekday: [:*],
year: [:*]}
iex> ~e[1 2 3 4 5 6 7]e
%Crontab.CronExpression{
extended: true,
second: [1],
minute: [2],
hour: [3],
day: [4],
month: [5],
weekday: [6],
year: [7]}
"""
@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)

@doc """
Convert Crontab.CronExpression struct to Tuple List
Expand Down Expand Up @@ -71,4 +114,27 @@ defmodule Crontab.CronExpression do
def to_condition_list(interval = %Crontab.CronExpression{}) do
[{:second, interval.second} | to_condition_list(%{interval | extended: false})]
end

defimpl Inspect do
@doc """
Pretty Print Cron Expressions
### Examples:
iex> IO.inspect %Crontab.CronExpression{}
~e[* * * * * *]
iex> import Crontab.CronExpression
iex> IO.inspect %Crontab.CronExpression{extended: true}
~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) <> "]"
end
def inspect(cron_expression = %Crontab.CronExpression{extended: true}, _options) do
"~e[" <> Crontab.CronExpression.Composer.compose(cron_expression) <> "]e"
end
end
end
2 changes: 1 addition & 1 deletion test/crontab/cron_expression/composer_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Crontab.CronExpression.ComposerTest do
use ExUnit.Case
use ExUnit.Case, async: true
doctest Crontab.CronExpression.Composer
end
2 changes: 1 addition & 1 deletion test/crontab/cron_expression/parser_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Crontab.CronExpression.ParserTest do
use ExUnit.Case
use ExUnit.Case, async: true
doctest Crontab.CronExpression.Parser
import Crontab.CronExpression.Parser

Expand Down
7 changes: 7 additions & 0 deletions test/crontab/cron_expression_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Crontab.CronExpressionTest do
use ExUnit.Case, async: true
doctest Crontab.CronExpression, import: true

# I'd like to doctest that one, but I can't get the sigil working
# doctest Inspect.Crontab.CronExpression, import: true
end
2 changes: 1 addition & 1 deletion test/crontab/date_checker_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Crontab.DateCheckerTest do
use ExUnit.Case
use ExUnit.Case, async: true
doctest Crontab.DateChecker
import Crontab.DateChecker

Expand Down
2 changes: 1 addition & 1 deletion test/crontab/functional_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Crontab.FunctionalTest do
use ExUnit.Case
use ExUnit.Case, async: true

tests_find_date = [
###########################################################################################################################################
Expand Down
2 changes: 1 addition & 1 deletion test/crontab/scheduler_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Crontab.SchedulerTest do
use ExUnit.Case
use ExUnit.Case, async: true
doctest Crontab.Scheduler
import Crontab.Scheduler

Expand Down

0 comments on commit 73ddf1b

Please sign in to comment.