Deps mixin outside of Hanami::Action

Enjoying playing with hanami 2.0 so far. Copied this question from the gitter chat, which seems quite dead.

How can I use include Deps[] outside of an action? I can retrieve things by going to Hanami.app["my.thing"] but it would be nice to get the mixin. Is this possible?

update: I just realised what the error was. I was trying to shorten module names by namespacing like this:

module MyApp::Middleware
  class DoSomething
    include Deps['something']

With this, it would fail with an uninitialized constant Deps.

If I break it up such that each module is listed separately, it suddenly works. I don’t know enough about magic ruby internals to explain what the difference is, but will leave this here just in case it helps someone else.

1 Like

Hi @jtippett! Thanks for trying Hanami, and I’m glad you managed to figure this out :slight_smile:

Like you’ve discovered, the Deps mixin is available for all kinds of classes, not just actions.

If you’d like to keep your collapsed namespace declarations, you can still access Deps inside, but you just need to refer to its name in full, for example:

module MyApp::Middleware
  class DoSomething
    include MyApp::Deps["something"]
  end
end