hassox / fuel

Useful functionality for consistent applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fuel

Provides some useful modules that help with building consistent applications.

Ctx

Provides a context object similar to golangs context.Context that provides a way to thread data through a call-stack or even across process boundaries.

This is very useful for dependency injection especially when testing.

For example

setup do
  ctx = 
    Fuel.Ctx.new()
    |> Fuel.Ctx.with_impl(MyBehaviour, MyBehaviourMock)
  
  {:ok, %{ctx: ctx}}
end

test "test my thing", ctx do
  options = Fuel.Ctx.ctx_into(ctx.ctx, [])

  MyBehaviourMock
  |> expect(:some_call, fn _, _ -> :ok end)

  assert :ok == MyThing.call_a_function("yay", options)
end

In your code:

  def call_a_function(arg1, options) do
    with ctx <- Fuel.Ctx.ctx_from(options),
         my_behaviour <- Fuel.Ctx.fetch_impl(ctx, MyBehaviour) do
      
      my_behaviour.some_call(arg1, options)
    end
  end

Contexts are useful for more than just DI.

Installation

If available in Hex, the package can be installed by adding fuel to your list of dependencies in mix.exs:

def deps do
  [
    {:fuel, "~> 0.3.0"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/fuel.

About

Useful functionality for consistent applications

License:MIT License


Languages

Language:Elixir 100.0%