Deep Dive: Hanami vs. Ruby on Rails

Introduction

Hanami and Ruby on Rails are two popular web frameworks for Ruby, each with its own philosophy, design principles, and use cases. Understanding the differences between Hanami and Rails can help developers choose the right framework for their projects based on specific requirements and preferences.

Core Philosophy

  1. Ruby on Rails:

    • Rails follows the “Convention over Configuration” principle, which means it provides a lot of default settings and structures to streamline development.
    • It emphasizes quick development and ease of use, making it an excellent choice for startups and rapid prototyping.
    • Rails is designed to be monolithic, where the entire application, including models, views, and controllers, is tightly integrated.
  2. Hanami:

    • Hanami aims for simplicity, lightweight architecture, and performance.
    • It follows the “Explicit over Implicit” principle, giving developers more control and requiring more configuration than Rails.
    • Hanami encourages a modular architecture, where different parts of the application can be developed and maintained independently.

Structure and Architecture

  1. Ruby on Rails:

    • Rails uses a monolithic architecture with a single application directory structure.
    • The typical Rails structure includes directories like models, views, controllers, helpers, mailers, and assets.
    • Rails applications follow a standard Model-View-Controller (MVC) pattern.
  2. Hanami:

    • Hanami promotes a modular architecture with the concept of “apps” within a single project.
    • Each app can have its own models, views, controllers, and configurations.
    • Hanami’s directory structure is more flexible and can be customized to fit the project’s needs.

Models: Active Record vs. Entities

  1. Active Record (Rails):

    • Rails uses Active Record, an Object-Relational Mapping (ORM) system that ties the model objects directly to database tables.
    • Active Record provides a rich set of methods to interact with the database, making database operations straightforward.
    • Example of an Active Record model:
      class User < ApplicationRecord
        # This model is tied to the 'users' table in the database
      end
  2. Entities and Repositories (Hanami):

    • Hanami separates the database layer from the domain layer using entities and repositories.
    • Entities: Represent the core business objects with attributes and business logic but no direct database interaction.
    • Repositories: Handle the persistence and retrieval of entities from the database, acting as a bridge between the entities and the database.
    • Example of an Entity and Repository in Hanami:
      # Entity
      class User < Hanami::Entity
        attributes :id, :name, :email
      end
       
      # Repository
      class UserRepository < Hanami::Repository
        def find_by_email(email)
          users.where(email: email).one
        end
      end

Configuration and Customization

  1. Ruby on Rails:

    • Rails provides a lot of default configurations, making it quick to start but sometimes harder to customize.
    • Many of the configurations are implicit, based on conventions that Rails follows.
  2. Hanami:

    • Hanami requires more explicit configuration, offering greater flexibility and control over the application’s behavior.
    • This explicit approach can lead to a steeper learning curve but provides more power and customization for complex applications.

Performance

  1. Ruby on Rails:

    • Rails is optimized for developer productivity, which can sometimes come at the cost of performance.
    • It is well-suited for applications that prioritize rapid development and ease of use.
  2. Hanami:

    • Hanami focuses on being lightweight and performant, aiming to reduce memory consumption and increase speed.
    • Its modular architecture allows developers to build high-performance applications by loading only the necessary components.

Community and Ecosystem

  1. Ruby on Rails:

    • Rails has a large and active community with a vast ecosystem of gems, plugins, and resources.
    • It benefits from extensive documentation, tutorials, and third-party tools.
  2. Hanami:

    • Hanami has a smaller but growing community.
    • It is supported by a set of official gems (e.g., hanami-router, hanami-controller, hanami-view) that are designed to be lightweight and modular.

Summary

  • Ruby on Rails: Ideal for rapid development with a strong emphasis on convention over configuration. It uses Active Record for database interactions and is well-suited for monolithic applications.
  • Hanami: Focuses on simplicity, modularity, and performance. It uses entities and repositories for a clear separation between the domain logic and database access, making it suitable for complex, high-performance applications.

By understanding these differences, developers can make an informed choice based on their project’s requirements, whether they need the rapid development capabilities of Rails or the modular, performant architecture of Hanami.