Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Hanami

Introduction to Hanami

Hanami is a great Ruby MVC framework and this is a brief intro on the M, V, and C of Hanami

Gabriel Gizotti

February 21, 2017
Tweet

More Decks by Gabriel Gizotti

Other Decks in Programming

Transcript

  1. What's Hanami in our context? • A Ruby Web MVC

    Framework • Create by Luca Guidi - @jodosha • Simple API • Minimal DSL • PORO over magic classes • Lightweight • Threadsafe
  2. Architecture • Clean Architecture • https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean- architecture.html • Enforces separation

    of concerns • Monolith First • https://martinfowler.com/bliki/MonolithFirst.html • Build as one app at first, separate the modules later
  3. Building a blog • Hanami 0.9.2 • 1.0.0.beta just released

    • Creating new project • Blog Post • Entity • Repository • Controller • Views • Templates
  4. Creating a new project • gem install hanami • hanami

    new hanami_blog –database=postgres –template=haml – test=rspec • Will build the skeleton of the app and basic config based on the options you passed. Very similar to Rails.
  5. Project Structure • Apps folder • Where the code for

    each "service" is located • Holds specific, assets, controllers, views, templates routes, and config for a 'service' • Config folder • Holds initializers and environment configuration • DB folder • Holds migration files and schema file
  6. Project Structure • Lib folder • Holds models, mailers, and

    any other piece if cold shared among apps • Spec folder • Hold test files for either Rspec or minitest
  7. Post Model • bundle exec hanami g model post •

    Creates Post entity and Post repository • Bundle exec hanami g migration create_posts • Creates a migration file to create the posts table • Manually write migration and run hanami db prepare • From Hanami 1.0 migration will be created as part of the model generator
  8. Hanami Model • Entity • The domain domain object •

    Responsible for the domain logic • The core of the application • Repository • An object that mediates between entities and persistence layer • Storage independent • Exposes just a few query methods
  9. Post Controller - Index • bundle exec hanami g action

    web posts#index • Creates Posts#index action class and spec • Creates Posts index view class • Creates posts/index.html.haml template • Inserts get '/posts', to: 'posts#index' to routes.rb • Everything is created in the context of the Web app
  10. Hanami Controller • Hanami Controller is a Ruby module •

    Each action is its own class with a call method • Sets status code and the headers for the response • Differently from Rails, the router instantiates the action itself and send the call method to it.
  11. Hanami View • The output of the view is the

    body of the action response • Is used as a context for the templates. All methods declared in the view are available to the template • It exposes helpers, like the form, and routes • Can be used to simplify templates
  12. Template • What we call a View in Rails •

    It describes the body of a response • ERB is the default engine, but a number of engines are supported
  13. Posts Controller – New • Same as creating the Index

    action • bundle exec hanami g action web posts#new
  14. Posts Controller - Create • bundle exec hanami g action

    web posts#create –method=post • Will append a post route to the web routes file • Will create the action and view classes, and their specs
  15. Validations • Validations can be added to the entity •

    Validations can also be added to controllers to validate the passed params • Useful when an entity has 2 or more different validation, depending on workflow