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. View Slide

  2. What's Hanami?

    View Slide

  3. View Slide

  4. 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

    View Slide

  5. 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

    View Slide

  6. Building a blog
    • Hanami 0.9.2
    • 1.0.0.beta just released
    • Creating new project
    • Blog Post
    • Entity
    • Repository
    • Controller
    • Views
    • Templates

    View Slide

  7. 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.

    View Slide

  8. 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

    View Slide

  9. 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

    View Slide

  10. 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

    View Slide

  11. 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

    View Slide

  12. 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

    View Slide

  13. 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.

    View Slide

  14. 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

    View Slide

  15. 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

    View Slide

  16. Posts Controller – New
    • Same as creating the Index action
    • bundle exec hanami g action web posts#new

    View Slide

  17. 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

    View Slide

  18. 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

    View Slide

  19. Resources
    • http://hanamirb.org/guides/
    • https://gitter.im/hanami/chat
    • https://github.com/davydovanton/awesome-hanami
    • https://github.com/gizotti/hanami_blog

    View Slide