Ruby

Hanami - New Ruby Web Framework

Hanami - New Ruby Web Framework

by Anton Davydov

In this presentation titled "Hanami - A New Ruby Web Framework," given by Anton Davydov at RubyKaigi 2017, the speaker introduces Hanami, a modern framework for developing web applications in Ruby. Despite its youth and the fact that it is less complex than other frameworks like Rails, Hanami brings a fresh perspective to web development, emphasizing modularity, simplicity, and maintainability.

Key Points Discussed:
- Introduction to Japan: Anton shares his experiences visiting Japan, including local customs like daily ramen consumption and unique tax-free shopping policies, which serves as an icebreaker for the presentation.
- Overview of Kanami Framework: Introduced as a project he is passionate about, Kanami is a web framework created to enhance long-term maintenance of applications. The speaker elaborates on Kanami's origins and developer contributions.
- Modular Structure: Hanami comprises ten distinct gems, allowing developers to select the specific components they need without unnecessary features.
- Separation of Logic: The framework effectively separates business logic from other application logic, utilizing repositories and modular components to maintain clarity in development.
- Thread-Safety: Kanami ensures full thread-safety, enabling reliable performance when used with popular servers and background job processors like Sidekiq.
- Rack Compatibility: Hanami's architecture facilitates the creation of Rack applications and allows for straightforward routing definitions similar to Sinatra.
- Business Logic Handling: Discussion on how Kanami uses the repository pattern rather than ActiveRecord, promoting cleaner, more manageable code with immutability for entities.
- Educational Value: Emphasizes how the framework encourages best practices such as dependency injection and test-driven development (TDD), which can lead to better programming habits.
- Community Support & Resources: The growing community around Kanami and its contributions to open-source projects provide ample resources for learners and developers.
- Real-World Applications: Examples of Kanami's practical use in production include companies like DNS Simple and various banking institutions in South America.
- Future Outlook: With ongoing development and community engagement, Hanami is evolving to offer more integrations and maintain compatibility with multiple Ruby versions.

In conclusion, Anton Davydov encapsulates the essence and potential of the Kanami framework, appealing to developers interested in a lightweight alternative that prioritizes clear structure, maintainability, and modern coding practices as they engage with web application development.

00:00:00 The title is Hanami - a new Ruby web framework. One week ago, I had a 10-hour flight from Moscow to Tokyo, and it was really nice. During my week in Japan, I can say that Japan is awesome! It's super strange for me because I'm from a different world, and I was super excited about visiting Japan.
00:00:18 This week, I learned some things about Japan. First, it’s common to eat one ramen per day. Also, people here consume so many different types of ramen. Oh, and many people have asked me how many ramen I have eaten, and I can say that it’s definitely more than one!
00:00:36 Another interesting thing I learned is that in Russia we have a special pack for juice. It's shaped differently from what I found here. When I visited a 7-Eleven in Japan, I was super surprised to see it in such a different form than what I'm used to.
00:00:53 Lastly, I learned that in Japan, purchases can be tax-free. Usually, when you buy something tax-free, you pay the price and later get a refund at the airport. However, here you pay without the taxes upfront, and they pin the receipt to your passport. I found that quite strange!
00:01:09 Anyway, my name is Anton. You can find some links about me online, but there are two main things you need to know about me. First, I love stickers! I have Kanami stickers, and if you have other stickers, let’s trade! I think this passion is why I'm a core developer for Kanami.
00:01:27 My commits can be found in various repositories, such as Ruby on Rails, Crystal, Dry-Rb, and others. I also engage in social activities; I am part of a community in Moscow where we hold meetups and drink-ups. If you're planning to visit Moscow or anywhere in Russia, please send me an email or tweet me.
00:01:45 Today, we are going to talk about Kanami. Kanami is a web framework, and every time I discuss it, I try to explain what 'Kanami' means. However, in Japan, I just skip the slides, but for those who don’t know, Kanami means when you watch flowers blossom.
00:02:08 Today, we will explore Ruby and its frameworks. This framework was created by a developer from Italy named Luca Vidia, and the first commit was made four years ago. As you can see, Kanami is a really young framework with a core team of eight people.
00:02:27 Let’s discuss some general ideas. The biggest problem Kanami tries to solve is long-term maintenance. If you have an old project, you might find it very difficult to add new features or refactor some code, which can be a complicated task. Kanami addresses this problem through various solutions.
00:02:45 For example, modularity: Kanami consists of ten different gems that you can use for different functionalities such as models, views, etc. You can drop any gem and use others. For instance, if you want to drop a model, you can simply use Active Record or any other ORM you prefer.
00:03:05 The second idea is that Kanami provides a simple way to separate your business logic from other logics. Additionally, simplicity and lightweight components lead to faster production application development.
00:03:24 A crucial aspect of Kanami is the rejection of monkey-patching. To illustrate this, I found a quiz that challenges you to identify whether a specific method is from Rails or Ruby. Can anyone confirm? Well, it is from Rails.
00:03:41 This situation suggests that there might be confusion as to whether it belongs to Rails, Ruby, or another framework. I think it's important to clarify these distinctions, which we’ll explore further.
00:03:56 Another feature Kanami offers is full thread-safety. You can use Puma or Unicorn with it, and everything will work correctly. Sidekiq is also compatible. However, please remember that Kanami is not Rails. They are completely different frameworks trying to solve sometimes similar problems within web development.
00:04:21 It's somewhat foolish to compare frameworks, but we will make some comparisons later. Now, let’s look at the typical components of web applications. Typical web applications include two types of logic: data flow and business logic.
00:04:37 What does data flow mean? It involves actions dealing with receiving and responding to user inputs. In Kanami, we can change our architecture accordingly, which means that we have one Kanami project where you can find various applications.
00:04:57 For instance, an admin application, a web application, and an API application all share the same project containing different controllers, view logic, and shared business logic.
00:05:16 This is important because sometimes, you may need separate instances for different applications. For example, you might want to keep your admin application private while still offering public applications.
00:05:39 With Kanami, you can boot your project with specific applications, excluding others. This is also beneficial if you want to run an API application alone, without extra bloat from other unnecessary data.
00:05:56 When you explore the applications folder in a Kanami project, you will see different directories for admin and web applications. Within these applications, you can locate their respective controllers and view objects.
00:06:16 Now let's discuss business logic. In Kanami, all business logic can be found in a library folder. The framework uses the repository pattern, which is a distinct approach compared to how ActiveRecord operates.
00:06:31 In the repository pattern, you have entities and repositories. We can dive deeper into this later. You can also find interactors, mailers, service objects, and other components within the library.
00:06:47 Kanami, while being a web framework, is actually a mix of ten different frameworks. The first five mentioned here are modules that provide a common interface for the various gems.
00:07:00 The other five are validations and helpers for views, such as those you would find in standard setups. Initially, all of this can feel overwhelming, but we’ll explore the differences with other frameworks for clarity.
00:07:14 Let's discuss a typical Rack application. It's essentially one class with a public method. This method needs to reload a specific cache or an array with specific data. When we look at Kanami’s routers, we see that it utilizes this idea effectively.
00:07:31 With Kanami, you can create Rack applications and mount them to specific paths within your application.
00:07:46 Regarding Sinatra, Kanami is quite similar in syntax, offering simple block definitions for routes. In the context of Rails, while comparing frameworks can be unproductive since they solve different issues, they both implement similar design patterns.
00:08:03 If we break down the controllers, Rails utilizes a consolidated class structure with various actions.
00:08:20 In contrast, Kanami adopts a different approach. Each action is a separate class, containing a singular public method, and each can have a validation schema for its parameters.
00:08:36 This means you validate your parameters before sending them to the model. In Rails, there’s typically one large class crammed with logic that contains various methods, callbacks, and logical associations.
00:08:52 In Kanami, we utilize the ROM (Ruby Object Mapper), which adopts a repository pattern approach, allowing for more flexibility and less clutter in your models.
00:09:03 For instance, in a Kanami model, the structure consists of two different objects: an entity, which is immutable, and a repository that contains logic specific to the database.
00:09:18 For example, if we create a new entity object, we ensure it carries certain properties, and any attempt to change these results in an error due to its immutable nature.
00:09:34 When we talk about views in Kanami, they are also structured differently compared to traditional views. Kanami employs partials and slim templates, ensuring flexibility without conflating the view logic.
00:09:51 Asset management, unfortunately, is not one of my favorite topics and is left as elective for independent study.
00:10:09 Now, as we discuss the pros and cons of Kanami, one significant advantage is that there is no magical complexity for users. Everything is straightforward and clear.
00:10:28 For example, when testing, you can simply create an instance of an action and call it using parameters to get the expected outcomes.
00:10:44 In Kanami, you don’t have any monkey-patching which is a relief for many developers who face issues due to monkey patches in various frameworks.
00:11:01 In the community, questions arise about compatibility and discrepancies between the framework and the underlying Ruby language.
00:11:16 Another strong point of Kanami is education; it encourages developers to embrace good practices like dependency injection, fostering better development habits overall.
00:11:32 Dependency injection refers to the design principle where you pass dependencies at initialization, allowing for more manageable and testable code.
00:11:48 In Kanami, interactions replace traditional service objects, and these classes return other objects with states indicating success.
00:12:05 This is useful in actions where you might need to create a user or handle sign-ups. You can check if the operation was successful and act accordingly.
00:12:21 Kanami also teaches test-driven development principles through its documentation, leading the way for writing better tests from the start.
00:12:35 However, there are limitations: The current TDD practices rely on the developer's skill and experience.
00:12:49 Documentation is a double-edged sword; while useful, it may sometimes leave room for improvement, especially for newcomers.
00:13:02 There are many integrations available, and while the gem ecosystem is growing, Kanami is still relatively young compared to Rails and Sinatra.
00:13:15 As the community evolves and more developers contribute, we’ll see the framework expanding and offering even more gems.
00:13:31 Now, moving towards community resources, you can find many useful links and projects, both in the Kanami awesome lists and through various open-source contributions.
00:13:46 I encourage everyone to explore open-source projects and contribute where possible. It's a great way to learn and grow.
00:14:02 As we wrap up, remember that Kanami is designed to use fewer resources than other frameworks, making it more efficient.
00:14:20 If you have any questions afterward, please feel free to reach out. Thank you for your attention!
00:14:58 Could you provide examples of businesses using Kanami in production?
00:15:05 Yes, one example is DNS Simple, which utilizes Kanami for their API. Additionally, I know some banks in South America are using Kanami for their banking applications.
00:15:16 You mentioned long-term maintenance as a target. Could you elaborate? Do you have support for various Ruby versions?
00:15:31 Yes, of course! It’s important to support older Ruby versions as well, and Kanami has been compatible since Ruby 2.1.
00:15:43 What does your release cycle look like?
00:15:59 The first version launched in April, and the next version (1.1) is expected in October. You can also check a beta version now!
00:16:12 Thank you! Is there anyone else with questions?
00:16:30 Can you show me the Kanami repository?
00:16:47 Sure! I can demonstrate that later.
00:17:02 Do you find the syntax in the Kanami repository convenient?
00:17:19 Yes, the methods provided by default are effective and simplify database interactions.
00:17:32 In Kanami, it encourages separating logic cleanly without overwhelming your codebase.
00:17:50 This allows you to write code that is easier to maintain in the long run, notably compared to more cumbersome frameworks.
00:18:07 Taking measures to manage complexity helps prevent creating god objects in your applications.
00:18:25 Overall, I love Rails, but you really need to think about architecture when using it to avoid bloat.
00:18:42 Implementing better separation within your code and having clear validations can significantly enhance your development process.
00:19:00 Thank you for participating! If you have any further questions or remarks, let's keep the conversation going!