Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
195: Hanami Architecture Philosophy
If you've spent any time in the Ruby ecosystem, you've likely felt the gravitational pull of Ruby on Rails. It's powerful, but if you've worked on a professional Rails project for more than two years, you know the pain: the "Fat Model" syndrome, where a single 2,000-line file handles everything from database queries to email notifications. Hanami was built specifically to solve that architectural rot.
Why should I care about Hanami if Rails already does everything?
Look, Rails is fantastic for getting a prototype off the ground. But Hanami isn't trying to be a "faster" Rails; it's trying to be a more sustainable way to write software. The core philosophy here is decoupling. In Hanami, the framework is just a delivery mechanism. Your business logic doesn't "live" inside the framework; it lives in plain old Ruby objects (POROs) that just happen to be called by the framework.
I like to think of it this way: in Rails, your app is Rails. In Hanami, your app is a set of Ruby services, and Hanami is simply the web-facing shell that routes requests to them. This means when you want to test a piece of logic, you don't have to load the entire heavy framework environment just to check if a calculation is correct.
What's the deal with "Slices" and how do they actually help?
In a typical app, things get messy because the "User" model is used by the admin panel, the public API, and the customer dashboard. Everything is intertwined. Hanami introduces Slices to carve your application into distinct components.
Imagine you're building a Book Inventory System. Instead of one giant app, you'd have an Admin slice and a Catalog slice. Each slice has its own actions, views, and components. They are logically isolated. If the Admin slice needs to change how it handles book uploads, it doesn't risk breaking the Catalog slice's public-facing search page.
# A conceptual look at the directory structure
app/
slices/
admin/
actions/
templates/
catalog/
actions/
templates/
It's similar to microservices, but without the nightmare of managing network calls between five different servers. You get the organizational benefits of microservices while staying in a single codebase (a "modular monolith").
If the Action isn't for logic, where does the actual work happen?
This is where most people stumble when switching to Hanami. In other frameworks, you're tempted to put your database calls and validation logic right in the controller. In Hanami, an Action has one job: handle the HTTP request and return a response. That's it.
For the actual "work," you create a separate operation class. Let's say we're marking a book as "Loaned" in our inventory system. You wouldn't put that logic in the Action. You'd create a dedicated operation:
# app/slices/catalog/operations/loan_book.rb
module Catalog
module Operations
class LoanBook
def call(book_id:, user_id:)
# 1. Validate availability
# 2. Create loan record
# 3. Update book status
# 4. Trigger notification
end
end
end
end
Then, your Action simply calls this operation. This makes your code incredibly easy to test because you can test LoanBook in complete isolation from the web request.
Why split Entities and Repositories instead of just having a Model?
This is the "secret sauce" of Hanami's persistence layer. In most Ruby apps, the Model is a god-object: it knows how to validate data, how to save itself to SQL, and how to calculate business totals. Hanami splits this into Entities and Repositories.
- Entities: These are simple data objects. They don't know the database exists. They just hold attributes (like
titleorisbn). - Repositories: These are the only objects allowed to talk to the database.
Why do this? Because it prevents your business logic from becoming leaked SQL. If you decide to switch from Postgres to a NoSQL store or a third-party API for your book data, you only have to change the Repository. Your Entities and Operations remain untouched. I've seen this save weeks of refactoring time in legacy projects.
📋 Practical Task
Refactor a "Fat Action" into a Hanami Operation
You've inherited a piece of code in a Book Inventory system where the developer ignored the Hanami philosophy and crammed all the logic into the action. Your task is to decouple this.
The current messy action:
# app/slices/catalog/actions/books/create.rb
module Catalog
module Actions
module Books
class Create < Catalog::Action
def handle(request, response)
book_params = request.params[:book]
# PROBLEM: Business logic and persistence in the action!
if book_params[:isbn].nil?
response.status = 422
response.body = "ISBN is required"
else
repo = BookRepository.new
repo.create(book_params)
response.status = 201
response.body = "Book created successfully"
end
end
end
end
end
end
Your Goal:
- Create a new class
Catalog::Operations::CreateBookthat handles the ISBN validation and the repository call. - Refactor the
Catalog::Actions::Books::Createaction to instantiate the operation and call it, removing all business logic from the action itself. - Ensure the Action still returns the correct HTTP status codes based on the outcome of the operation.
There are no comments for now.