$30 off During Our Annual Pro Sale. View Details »

Hanami 2.0 and You

Hanami 2.0 and You

Let’s chat about the new release of Hanami 2.0 Web Framework and what you can use it for. Let's learn how you can start with it, and some examples from an application that is already making use of Hanami in production.

Christine

March 28, 2023
Tweet

More Decks by Christine

Other Decks in Technology

Transcript

  1. Hanami 2.0 and You
    WNB.rb March 2023 Meetup

    View Slide

  2. @[email protected]
    Continuing on our dry-rb journey
    github.com/timriley github.com/solnic github.com/jodosha
    All in dry-rb + Hanami

    View Slide

  3. @[email protected]
    What is it?
    Modern web framework for Ruby
    - Focused on maintainability, testability, and scaling
    - Create applications that are systems of well-formed components
    with clear dependencies.
    - Anyone work on a really big Rails app?
    - Let’s dive in on what a Hanami project looks like, and it’s benefits

    View Slide

  4. @[email protected]
    Hanami 2.X != Hanami 1.X

    View Slide

  5. @[email protected]
    Getting started project structure

    View Slide

  6. @[email protected]
    Location Purpose
    Gemfile The application’s gem dependencies, installed using bundler.
    Guardfile Supports code reloading in development.
    README.md The application’s readme document.
    Rakefile Support for running rake tasks.
    app/ This is the directory where you’ll put the majority of your application’s
    code.
    config/ A directory for your application’s configuration, including things like
    routes, settings and Puma configuration.
    config.ru A Rack config file.
    lib/ A directory for supporting code.
    spec/ The application’s RSpec test suite.

    View Slide

  7. @[email protected]
    Maintainability

    View Slide

  8. @[email protected]
    Maintainability - Hanami Container
    ● Zeitwork for loading
    ● Code autoloading helps you work with minimal fuss
    ● Autoload not just application code but dependencies which means
    faster boot times
    ○ Doesn’t have to eager load every constant ever at boot time…unlike Rails
    ● App core itself is special
    ○ Can use the app object itself to load and return any individual component
    ○ Hanami.app["actions.food.index"]
    ● Hanami console, like a supercharged Rails console

    View Slide

  9. @[email protected]
    Testability

    View Slide

  10. @[email protected]
    Helps remove rigid dependencies (so Sandi Metz approved ™)
    - Dependency injection is easy
    - include Deps["key"], the Deps mixin allows a component to use
    any other component in its container as a dependency
    - Easier to code up tests too! Just inject your mocks
    Deps - friendly dependency injection helper

    View Slide

  11. @[email protected]
    Deps code example
    No dependency injection == tightly coupled

    View Slide

  12. @[email protected]
    Using the Deps mixin, no attr_reader or initializer boilerplate needed

    View Slide

  13. @[email protected]
    You don’t have to go
    all in on Hanami…

    View Slide

  14. @[email protected]
    Hanami-api HTTP framework
    Extremely fast, lightweight Ruby framework for HTTP APIs
    Ideal mini framework to build HTTP APIs and microservices

    View Slide

  15. @[email protected]
    Want to try it for yourself?
    Use the Getting Started guide or check
    out Picnic, our example project

    View Slide

  16. @[email protected]
    See an example Hanami app - Picnic (github.com/cseeman/picnic)

    View Slide

  17. @[email protected]
    See an example Hanami app - Picnic (github.com/cseeman/picnic)

    View Slide

  18. @[email protected]
    See an example Hanami app - Picnic (github.com/cseeman/picnic)

    View Slide

  19. View Slide

  20. @[email protected]
    Scaling - Hanami Slices
    Draw boundaries between its major areas of concern.
    Built-in facility for organizing your code, encouraging maintainable
    boundaries, and creating operational flexibility.
    Just like a miniature Hanami app in your Hanami app!

    View Slide

  21. @[email protected]
    Slices == the way to keep your application from ballooning

    View Slide

  22. @[email protected]
    Slices in action

    View Slide

  23. View Slide

  24. @[email protected]
    Dry::CLI
    ● General-purpose framework for developing Command Line
    Interface (CLI) applications
    ● Commands as objects that can be registered

    View Slide

  25. @[email protected]
    Sometimes you want the DB helpers that rails has…

    View Slide

  26. @[email protected]
    Helpful generators
    ● Create Action
    $ bundle exec hanami generate action
    home.show
    ● Create Database Migration
    $ bundle exec rake
    db:create_migration[create_books]
    ● Create a whole app
    $ hanami new picnic
    ● Or a smaller part of the app, a slice
    $ hanami generate slice api
    ● And in the slice
    $ hanami generate action food.show --slice api

    View Slide

  27. @[email protected]
    My favorite things about Hanami
    ● Blazing fast console
    ● Slices can deploy out as their own containers (mini contained
    apps…)
    ● Great for APIs
    ● Deps really is a great way to make it easy to create reusable
    components that are easy to test
    ● Dry::CLI is a joy to compose together developer centric tasks

    View Slide

  28. @[email protected]
    Room for improvement with Hanami
    ● ROM is a learning curve vs
    ActiveRecord
    ● Frontend integration is not
    there yet…dry-view integration
    is not available yet
    ● Dry-rb and understanding the
    functional programming is still
    necessary to get all the benefits
    of Hanami

    View Slide

  29. @[email protected]
    Hanami in action
    DRY::CLI + Hanami makes creating tools to help devs easy

    View Slide

  30. @[email protected]
    Hanami in action
    Boot Time
    ● No noticeable difference between brand new hanami app vs
    production app
    Full app > < Example App

    View Slide

  31. @[email protected]
    Hanami in action
    - Build Time
    - Averaging 5-6 minutes
    - How it has sped up new
    feature development
    - Deployed 15 actions
    spanning 3 different
    slices
    - Test Time
    - Running each slice full
    test suite is quick

    View Slide

  32. @[email protected]
    Who am I?
    Christine Seeman
    🌽 lifetime learner from Omaha, NE
    📚 likes to read too much
    🥙🍜🍛 eats food that probably took too long to prepare
    Professionally solving problems on the Identity team at WP Engine
    christine-seeman.com

    View Slide

  33. @[email protected]
    Thank you to my awesome teammates
    For all you taught me about Hanami and vim (yes I can exit now)
    Adam Lassek
    Andy Neely
    Jim Finn
    Miguel Tapia

    View Slide

  34. @[email protected]
    References
    Hanami Official Site: https://hanamirb.org/
    Hanami 2.0 Announcement: https://hanamirb.org/blog/2022/11/22/announcing-hanami-200/
    Introducing Hanami::API https://hanamirb.org/blog/2020/02/26/introducing-hanami-api/
    Hanami API GitHub: https://github.com/hanami/api
    Highlights of Hanami 2.0: https://discourse.hanamirb.org/t/highlights-of-hanami-2-0/728
    Hanami 2.0 Guide: https://guides.hanamirb.org/v2.0/introduction/getting-started/
    Hanami 2.0 Container and Components:
    https://guides.hanamirb.org/v2.0/app/container-and-components/
    Hanami 2.0.3 Release: https://hanamirb.org/blog/2023/02/01/hanami-203/
    Dry-rb Home: https://dry-rb.org/
    Hanami Discourse: https://discourse.hanamirb.org/
    Introducing dry-files: https://dry-rb.org/news/2021/05/04/introducing-dry-files/
    Hanami 2.0 and You Slides: https://christine-seeman.com/2023-talks/

    View Slide

  35. @[email protected]
    https://coolors.co/palette/ffcdb2-ffb4a2-e5989b-b5838d-6d6875
    https://www.clker.com/cliparts/y/u/g/3/b/E/sakura-blossom-alice-md.png

    View Slide