Skip to content

Commit

Permalink
Changes in apply_fn
Browse files Browse the repository at this point in the history
  • Loading branch information
msoedov committed May 14, 2017
1 parent 46186a1 commit 682b5e2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ if Mix.env == :dev do
tasks: [
"test",
"credo",
"dogma"
]

end
31 changes: 19 additions & 12 deletions lib/firex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,10 @@ defmodule Firex do
case parsed do
{opts, [^cmd_name], _} ->
fn_args = opts |> Enum.map(fn {k, v} -> v end)
try do
Kernel.apply(__MODULE__, name, fn_args)
%{state | exausted: true}
rescue
UndefinedFunctionError ->
help_fn.()
state
e in _ ->
%{message: msg} = e
[:red, "Error: #{msg}"] |> Bunt.puts
state
end
apply_fn(__MODULE__, name, fn_args, state)
{_, plain, []} when is_list(plain) ->
[name|fn_args] = plain
apply_fn(__MODULE__, name |> String.to_atom, fn_args, state)
{_, [_], _} ->
state
end
Expand All @@ -72,6 +64,21 @@ defmodule Firex do
state
end

defp apply_fn(module, fun, fn_args, %{help_fn: help_fn} = state) when is_atom(fun) do
try do
Kernel.apply(module, fun, fn_args)
%{state | exausted: true}
rescue
UndefinedFunctionError ->
help_fn.()
state
e in _ ->
%{message: msg} = e
[:red, "Error: #{msg}"] |> Bunt.puts
state
end
end

defp help(cm, help_info) do
msg = cm |> Enum.map(fn {name, params} ->
signature = params
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ defp deps do
[
{:credo, only: ~w(test dev)a},
{:bunt, "~> 0.1.0"},
{:dogma, only: ~w(test dev)a},
{:mix_test_watch, "~> 0.2.5", only: ~w(dev test)a},
]
end
Expand Down
4 changes: 3 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
%{"bunt": {:hex, :bunt, "0.1.6", "5d95a6882f73f3b9969fdfd1953798046664e6f77ec4e486e6fafc7caad97c6f", [:mix], [], "hexpm"},
"credo": {:hex, :credo, "0.5.3", "0c405b36e7651245a8ed63c09e2d52c2e2b89b6d02b1570c4d611e0fcbecf4a2", [:mix], [{:bunt, "~> 0.1.6", [hex: :bunt, repo: "hexpm", optional: false]}], "hexpm"},
"dogma": {:hex, :dogma, "0.1.15", "5bceba9054b2b97a4adcb2ab4948ca9245e5258b883946e82d32f785340fd411", [:mix], [{:poison, ">= 2.0.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
"fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], [], "hexpm"},
"mix_test_watch": {:hex, :mix_test_watch, "0.2.6", "9fcc2b1b89d1594c4a8300959c19d50da2f0ff13642c8f681692a6e507f92cab", [:mix], [{:fs, "~> 0.9.1", [hex: :fs, repo: "hexpm", optional: false]}], "hexpm"}}
"mix_test_watch": {:hex, :mix_test_watch, "0.2.6", "9fcc2b1b89d1594c4a8300959c19d50da2f0ff13642c8f681692a6e507f92cab", [:mix], [{:fs, "~> 0.9.1", [hex: :fs, repo: "hexpm", optional: false]}], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"}}

0 comments on commit 682b5e2

Please sign in to comment.