Current section
7 Versions
Jump to
Current section
7 Versions
Compare versions
6
files changed
+31
additions
-18
deletions
| @@ -3,6 +3,7 @@ | |
| 3 3 | [](https://travis-ci.org/i22-digitalagentur/delx) |
| 4 4 | [](https://hex.pm/packages/delx) |
| 5 5 | |
| 6 | + [Defdelegate](https://hexdocs.pm/elixir/Kernel.html#defdelegate/2) on steroids! |
| 6 7 | An Elixir library to make function delegation testable. |
| 7 8 | |
| 8 9 | ## Prerequisites |
| @@ -18,7 +19,7 @@ by adding `delx` to your list of dependencies in `mix.exs`: | |
| 18 19 | ```elixir |
| 19 20 | def deps do |
| 20 21 | [ |
| 21 | - {:delx, "~> 2.0.1"} |
| 22 | + {:delx, "~> 2.1.0"} |
| 22 23 | ] |
| 23 24 | end |
| 24 25 | ``` |
| @@ -131,8 +132,9 @@ defmodule GreeterTest do | |
| 131 132 | fn {Greeter, :hello}, |
| 132 133 | {Greeter.StringGreeter, :hello}, |
| 133 134 | ["Tobi"] -> |
| 134 | - :ok |
| 135 | - end) |
| 135 | + :ok |
| 136 | + end |
| 137 | + ) |
| 136 138 | |
| 137 139 | Greeter.hello("Tobi") |
| 138 140 | end |
| @@ -142,3 +144,6 @@ end | |
| 142 144 | |
| 143 145 | For more information on how to implement your own delegator, refer to the |
| 144 146 | docs of the `Delx.Delegator` behavior. |
| 147 | + |
| 148 | + Note that the configuration is only applied at compile time, so you are unable |
| 149 | + to stub or replace the delegator module at runtime. |
| @@ -12,7 +12,7 @@ | |
| 12 12 | <<"LICENSE">>]}. |
| 13 13 | {<<"licenses">>,[<<"MIT">>]}. |
| 14 14 | {<<"links">>, |
| 15 | - [{<<"Source">>,<<"https://github.com/i22-digitalagentur/delx">>}]}. |
| 15 | + [{<<"GitHub">>,<<"https://github.com/i22-digitalagentur/delx">>}]}. |
| 16 16 | {<<"name">>,<<"delx">>}. |
| 17 17 | {<<"requirements">>,[]}. |
| 18 | - {<<"version">>,<<"2.0.1">>}. |
| 18 | + {<<"version">>,<<"2.1.0">>}. |
| @@ -101,8 +101,9 @@ defmodule Delx do | |
| 101 101 | fn {Greeter, :hello}, |
| 102 102 | {Greeter.StringGreeter, :hello}, |
| 103 103 | ["Tobi"] -> |
| 104 | - :ok |
| 105 | - end) |
| 104 | + :ok |
| 105 | + end |
| 106 | + ) |
| 106 107 | |
| 107 108 | Greeter.hello("Tobi") |
| 108 109 | end |
| @@ -111,6 +112,9 @@ defmodule Delx do | |
| 111 112 | |
| 112 113 | For more information on how to implement your own delegator, refer to the |
| 113 114 | docs of the `Delx.Delegator` behavior. |
| 115 | + |
| 116 | + Note that the configuration is only applied at compile time, so you are unable |
| 117 | + to stub or replace the delegator module at runtime. |
| 114 118 | """ |
| 115 119 | |
| 116 120 | defmacro __using__(opts) do |
| @@ -119,15 +123,14 @@ defmodule Delx do | |
| 119 123 | opts[:otp_app] || |
| 120 124 | raise ArgumentError, "expected otp_app: to be given as argument" |
| 121 125 | |
| 122 | - @doc false |
| 123 | - @spec __delegator__() :: module |
| 124 | - def __delegator__ do |
| 125 | - config = Application.get_env(unquote(otp_app), Delx, []) |
| 126 | + config = Application.get_env(otp_app, Delx, []) |
| 126 127 | |
| 127 | - case Keyword.fetch(config, :stub) do |
| 128 | - {:ok, true} -> Delx.Delegator.Stub |
| 129 | - _ -> Keyword.get(config, :delegator, Delx.Delegator.Common) |
| 130 | - end |
| 128 | + case Keyword.fetch(config, :stub) do |
| 129 | + {:ok, true} -> |
| 130 | + @delegator Delx.Delegator.Stub |
| 131 | + |
| 132 | + _ -> |
| 133 | + @delegator Keyword.get(config, :delegator, Delx.Delegator.Common) |
| 131 134 | end |
| 132 135 | |
| 133 136 | import Delx.Defdel |
| @@ -30,7 +30,7 @@ defmodule Delx.Defdel do | |
| 30 30 | |
| 31 31 | @doc delegate_to: {target, as, length(as_args)} |
| 32 32 | def unquote(name)(unquote_splicing(args)) do |
| 33 | - __MODULE__.__delegator__().apply( |
| 33 | + @delegator.apply( |
| 34 34 | {unquote(__MODULE__), unquote(name)}, |
| 35 35 | {unquote(target), unquote(as)}, |
| 36 36 | unquote(as_args) |
| @@ -1,4 +1,8 @@ | |
| 1 1 | defmodule Delx.StubbedDelegationError do |
| 2 | + @moduledoc """ |
| 3 | + An exception that raises when when a delegated function is stubbed. |
| 4 | + """ |
| 5 | + |
| 2 6 | import Exception, only: [format_mfa: 3] |
| 3 7 | |
| 4 8 | defexception [:source, :target, :args] |
Loading more files…