Skip to content
Elixir "direct" injection for functions
Elixir
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
config
lib
test
.formatter.exs
.gitignore
.tool-versions
LICENSE
README.md
mix.exs
mix.lock

README.md

Diin

Master Hex.pm Version

Diin is a simple convenience wrapper for directly injecting dependencies into Elixir functions.

Installation

Add diin to your list of dependencies in mix.exs:

def deps do
  [
    {:diin, "~> 0.1.0"}
  ]
end

Usage

def get_object(id, opts \\ []) do
  # Define the default dependencies this function uses to do it's work
  deps = Diin.parse(opts, %{http: HTTPoison})
  # Use dependencies to do work
  deps.http.get!("https://foo.bar/#{id}")
end

# Now you can easily change dependencies when calling the function
get_object(1, deps: %{http: TestHTTPClient})

Things get more interesting when you pass dependencies to nested functions.

def nested_do_work(raw, opts \\ []) do
  # Set the default HTML parser function
  deps = Diin.parse(opts, %{
    html_parser: &MochiWeb.parse/1,
    transformer: fn val -> val end,
  })
  # Do the deed
  raw
  |> deps.html_parser.()
  |> deps.transformer.()
end

def do_work(raw, opts \\ []) do
  # Change some dependencies
  opts =
    [deps: %{transformer: &do_transformation/1}] ++ opts
  # Now, `nested_do_work` will use the transformer function passed above instead of the default one.
  nested_do_work(raw, opts)
end

# Passing in a different dependency for `html_parser` still works!
do_work(raw, deps: %{html_parser: &Html5Ever.parse/1})
You can’t perform that action at this time.