Repository: whitfin/cachex (elixir)

Issue:
Starting `Cachex` via `Supervisor.start_link` 
Hello there,

I *really* hope I'm not wasting anyone's time here, but I simply went and read the "welcome page/guide" of this (awesome!) package and I've got myself a papercut.

[I read](https://hexdocs.pm/cachex/overview.html#setting-up) from the docs, in regards of starting `Cachex` in a supervision tree:
```ex
children = [
  {Cachex, [:my_cache]},     # with default options
  {Cachex, [:my_cache, []]}  # with custom options
]
```

So I try this in my phoenix application; I copy-paste the first line in my list of `children`:
```ex
children = [
  ...,
  {Cachex, [:my_cache]},
]
```

But, `Supervisor.start_link` gives this error:
```
** (Mix) Could not start application my_app: MyApp.Application.start(:normal, []) returned an error: shutdown: failed to start child: Cachex
    ** (EXIT) :invalid_name
```

Whereas, if I write my cache name like below, no error is raised on startup:
```ex
{Cachex, name: :my_cache},
```

Which makes sense, AFAIK supervisor wants a keyword list as a second argument of the tuple.

But... am I doing this right? Am I missing something?

Pull request:
Add support for startup matching documentation
Fixes #383.

The current documentation is incorrect, but rather than just change the docs we can make it correct! This just adds some grace handling to some `child_spec` formats. 

It matches how you'd call it interactively, so I think it's quite beginner friendly to support these new formats. I kinda thought they were already there anyway... 

Patch:
diff --git a/lib/cachex.ex b/lib/cachex.ex
index 3ebd7a1..118e214 100644
--- a/lib/cachex.ex
+++ b/lib/cachex.ex
@@ -286,6 +286,12 @@ defmodule Cachex do
   #
   # Without this, Cachex is not compatible per Elixir's documentation.
   @spec start_link(atom | Keyword.t()) :: {atom, pid} | {:error, :invalid_name}
+  def start_link([name]) when is_atom(name),
+    do: start_link(name)
+
+  def start_link([name, options]) when is_atom(name) and is_list(options),
+    do: start_link(name, options)
+
   def start_link(options) when is_list(options) do
     case Keyword.fetch(options, :name) do
       {:ok, name} ->
@@ -297,7 +303,7 @@ defmodule Cachex do
   end
 
   def start_link(name) when is_atom(name),
-    do: start_link(name: name)
+    do: start_link(name, [])
 
   @doc """
   Creates a new Cachex cache service tree.


Test patch:
diff --git a/test/cachex_test.exs b/test/cachex_test.exs
index 093de20..7f590a3 100644
--- a/test/cachex_test.exs
+++ b/test/cachex_test.exs
@@ -225,12 +225,30 @@ defmodule CachexTest do
   # This test validates `Cachex.start_link/1` mtaintains compatibility
   # with `Supervisor.child_spec/2` and handles the name as an option.
   test "cache start with child_spec/1 compatibility" do
-    # create two caches using `Cachex.start_link/1`
+    # check the default ways of starting a cache
     {:ok, _pid} = Cachex.start_link(:child_spec1)
-    {:ok, _pid} = Cachex.start_link(name: :child_spec2)
+    {:ok, _pid} = Cachex.start_link(:child_spec2, transactions: true)
 
-    # verify that both are at least alive in some way
-    {:ok, _cache1} = Cachex.inspect(:child_spec1, :cache)
-    {:ok, _cache2} = Cachex.inspect(:child_spec2, :cache)
+    # check the format {Cachex, [:child_spec3]}
+    {:ok, _pid} = Cachex.start_link([:child_spec3])
+
+    # check the format {Cachex, [:child_spec4, []]}
+    {:ok, _pid} = Cachex.start_link([:child_spec4, [transactions: true]])
+
+    # check the format {Cachex, [name: :child_spec5]}
+    {:ok, _pid} = Cachex.start_link(name: :child_spec5)
+
+    # check the format {Cachex, [name: :child_spec6, transactions: true]}
+    {:ok, _pid} = Cachex.start_link(name: :child_spec6, transactions: true)
+
+    # verify the caches that are created only from a name
+    {:ok, cache()} = Cachex.inspect(:child_spec1, :cache)
+    {:ok, cache()} = Cachex.inspect(:child_spec3, :cache)
+    {:ok, cache()} = Cachex.inspect(:child_spec5, :cache)
+
+    # double check those with options provided by double checking the value
+    {:ok, cache(transactions: true)} = Cachex.inspect(:child_spec2, :cache)
+    {:ok, cache(transactions: true)} = Cachex.inspect(:child_spec4, :cache)
+    {:ok, cache(transactions: true)} = Cachex.inspect(:child_spec6, :cache)
   end
 end