Extract start function for options
This commit is contained in:
@@ -7,7 +7,7 @@ defmodule Mix.Tasks.Meditate do
|
||||
Code.compiler_options(ignore_module_conflict: true)
|
||||
Watcher.start
|
||||
|
||||
Options.parse(args)
|
||||
Options.start(args)
|
||||
Runner.run
|
||||
|
||||
:timer.sleep(:infinity)
|
||||
|
||||
@@ -3,12 +3,8 @@ defmodule Options do
|
||||
clear_screen: false
|
||||
}
|
||||
|
||||
def parse(args) do
|
||||
options = Enum.reduce(args, @defaults, fn(arg, acc) ->
|
||||
Map.merge(acc, parse_argument(arg))
|
||||
end)
|
||||
|
||||
Agent.start_link(fn -> options end, name: __MODULE__)
|
||||
def start(args) do
|
||||
Agent.start_link(fn -> parse(args) end, name: __MODULE__)
|
||||
end
|
||||
|
||||
def clear_screen? do
|
||||
@@ -17,6 +13,12 @@ defmodule Options do
|
||||
end)
|
||||
end
|
||||
|
||||
defp parse(args) do
|
||||
Enum.reduce(args, @defaults, fn(arg, acc) ->
|
||||
Map.merge(acc, parse_argument(arg))
|
||||
end)
|
||||
end
|
||||
|
||||
def parse_argument("--clear-screen"), do: %{ clear_screen: true}
|
||||
def parse_argument(_), do: %{}
|
||||
end
|
||||
|
||||
@@ -2,17 +2,17 @@ defmodule OptionTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
test "has default options" do
|
||||
Options.parse([])
|
||||
Options.start([])
|
||||
refute Options.clear_screen?
|
||||
end
|
||||
|
||||
test "override clearing of screen" do
|
||||
Options.parse(["--clear-screen"])
|
||||
Options.start(["--clear-screen"])
|
||||
assert Options.clear_screen?
|
||||
end
|
||||
|
||||
test "ignores unknown options" do
|
||||
options = Options.parse(["--foo"])
|
||||
Options.start(["--foo"])
|
||||
refute Options.clear_screen?
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user