Hanami applications support a prepared state and a booted state.

Whether your app is prepared or booted determines whether its components are lazily loaded on demand (for a prepared app), or eagerly loaded up front (for a booted app).

This difference may not sound like much, but the ability of a prepared app to load just enough of the app to undertake a particular task can be a huge performance advantage when it comes to testing, worker processes and other use cases where your app doesn’t need every component to perform the job at hand. A rake task that precompiles assets or migrates the database can execute quickly when is not delayed by loading the entire application.

Conversely, a booted app is fully initialized, with every component loaded and ready to do its work. This is ideal whenever you want to incur initialization costs at boot time, such as when serving web requests.

Hanami.prepare

When you call Hanami.prepare (or use require "hanami/prepare"), Hanami will prepare your app for use.

This process makes the Ruby source files in app/ autoloadable and makes your components (from the same classes defined in app/) lazily loadable.

This approach keeps load time to a minimum. As such, it’s the default mode in the Hanami console and when running tests.

A prepared application will do everything a booted one can, it will just lazily load only the components it needs to perform a particular task on demand.

Hanami.boot

When you call Hanami.boot (or use require "hanami/boot") Hanami will do everything it does when Hanami.prepare is called, but then go further, starting each of your app’s providers, while also eagerly loading all of your app’s components up front.

This is useful when you want to incur all initialization costs at boot time.

Booting is the approach taken in Hanami’s standard Puma setup. Thus, in Hanami’s config.ru file you will see:

  1. require "hanami/boot"
  2. run Hanami.app

Stepping through the boot process

Purely as an exercise, we can explore both preparing and booting by starting irb in a directory containing a Hanami project. This is not something that’s needed in day to day development with Hanami - it’s useful here as a demonstration of the booting behaviour.

Assuming we have run hanami new bookshelf to generate a new app (see Getting started for a full guide to creating your first Hanami application), let’s create a hello world component in app/hello_world.rb:

  1. # app/hello_world.rb
  2. module Bookshelf
  3. class HelloWorld
  4. def say_hello
  5. "Hello world!"
  6. end
  7. end
  8. end

And now, in our project directory, let’s run irb:

  1. bundle exec irb
  2. irb(main)>

Typing Hanami here will result in a NameError:

  1. irb(main)> Hanami
  2. (irb):1:in `<main>': uninitialized constant Hanami (NameError)

Let’s address that by running require "hanami".

Once the Hanami module is available, calling Hanami.setup will load the Hanami app defined in config/app.rb:

  1. irb(main)> require "hanami"
  2. => true
  3. irb(main)> Hanami.setup
  4. => Bookshelf::App
  5. irb(main)> Hanami.app
  6. => Bookshelf::App

We can now ask our app whether it’s prepared or booted:

  1. irb(main)> Hanami.app.prepared?
  2. => false
  3. irb(main)> Hanami.app.booted?
  4. => false

We can also see what components are registered with the app by calling #keys:

  1. irb(main)> Hanami.app.keys
  2. => []

This makes sense, as the app hasn’t even been prepared yet, meaning component registration hasn’t begun!

Let’s prepare the app now, then ask what component keys are registered:

  1. irb(main)> Hanami.prepare
  2. => Bookshelf::App
  3. irb(main)> Hanami.app.prepared?
  4. => true
  5. irb(main)> Hanami.app.keys
  6. => ["settings", "notifications"]

Two components are now present: settings and notifications.

notifications is an instance of Dry::Monitor::Notifications that’s registered early in the prepare process to support inter-component notifications (for now mostly a framework-internal concern).

The settings component has been loaded in order to ensure that necessary settings are present. If a setting wasn’t satisfied, the app would have raised an invalid settings error at this point.

You’ll notice that our hello world component does not appear under the key "hello_world". If we called Hanami.boot at this point, the eager component loading would register it for us.

Rather than do that though, let’s see what happens if we just try to use our component.

  1. irb(main):011:0> Hanami.app["hello_world"].say_hello
  2. => "Hello world!"

Success! Even though the component wasn’t yet registered, it was lazily loaded when we used it!

If we now check what components have been registered in the app container, we’ll see "hello_world".

  1. irb(main)> Hanami.app.keys
  2. => ["settings", "notifications", "hello_world"]

From here, calling Hanami.boot will register the remainder of our application’s components, which in this case are just the components registered by Hanami’s default providers: "routes", "inflector", "logger" and "rack.monitor".

  1. irb(main)> Hanami.boot
  2. => Bookshelf::App
  3. irb(main)> Hanami.app.keys
  4. ["settings",
  5. "notifications",
  6. "hello_world",
  7. "routes",
  8. "inflector",
  9. "logger",
  10. "rack.monitor"]

You can read more about components and containers in more detail in the container and components guide. Providers are covered in the providers guide.