window.pipedriveLeadboosterConfig = { base: 'leadbooster-chat.pipedrive.com', companyId: 11580370, playbookUuid: '22236db1-6d50-40c4-b48f-8b11262155be', version: 2, } ;(function () { var w = window if (w.LeadBooster) { console.warn('LeadBooster already exists') } else { w.LeadBooster = { q: [], on: function (n, h) { this.q.push({ t: 'o', n: n, h: h }) }, trigger: function (n) { this.q.push({ t: 't', n: n }) }, } } })() Ruby on Rails modularization with Packwerk Episode I - The Codest
The Codest
  • About us
  • Services
    • Software Development
      • Frontend Development
      • Backend Development
    • Staff Augmentation
      • Frontend Developers
      • Backend Developers
      • Data Engineers
      • Cloud Engineers
      • QA Engineers
      • Other
    • It Advisory
      • Audit & Consulting
  • Industries
    • Fintech & Banking
    • E-commerce
    • Adtech
    • Healthtech
    • Manufacturing
    • Logistics
    • Automotive
    • IOT
  • Value for
    • CEO
    • CTO
    • Delivery Manager
  • Our team
  • Case Studies
  • Know How
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
  • About us
  • Services
    • Software Development
      • Frontend Development
      • Backend Development
    • Staff Augmentation
      • Frontend Developers
      • Backend Developers
      • Data Engineers
      • Cloud Engineers
      • QA Engineers
      • Other
    • It Advisory
      • Audit & Consulting
  • Value for
    • CEO
    • CTO
    • Delivery Manager
  • Our team
  • Case Studies
  • Know How
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
Back arrow GO BACK
2022-01-05
Software Development

Ruby on Rails modularization with Packwerk Episode I

Nicolas Nisoria

Humans find it difficult to see the big picture of a problem without devoting a lot of time and effort. This happens especially while working with large and complex applications. What are the side effects of my changes? Why is this line here affecting the tests of a remote part of the codebase? A perfect or complete solution does not exist, but Shopify came out with a tool that will probably help you and your team.

Introduction

In order to talk about Packwerk, we need to introduce a few concepts first.

  • Cohesion: refers to the measure of how much elements in a module or class belong together.
  • Coupling: refers to the level of dependency between modules or classes.
  • Boundaries: refers to barriers between code. In this case, a code boundary refers to different domains of concern within the same codebase.
  • Modularization: the process of dividing a software system into multiple separate modules where each module works independently.

Problems

As we know, Ruby doesn’t provide a good solution to enforce code boundaries. We can specify the visibility but all the dependencies will be loaded into the global namespace. In large or monolith applications, this lack of boundaries produces the following problems.

  • Low cohesion,
  • High coupling,
  • Spaghetti code.

In an attempt to modularize Shopify’s monolith and enforce boundaries, they tried different solutions without achieving the expected results:
– Setting private constants,
– Establishing boundaries through gems,
– Using tests to prevent cross-component associations,
– Using Ruby‘s Modulation gem,
– Creating microservices.

With all the knowledge from previous attempts, they decided to create their own tool: Packwerk.

Packwerk

What is Packwerk?

Packwerk is a static analysis tool used to enforce boundaries between groups of Ruby files called packages.

What is a package?

A package is a folder containing autoloaded code. Shopify’s team encourages to use the best design practices while creating packages.
– We should pack together things that have high functional cohesion,

  • Packages should be relatively loosely coupled with each other.

Types of boundary checks

We can enforce privacy and dependency boundaries, check violations of the boundaries and cyclic dependencies.

Packwerk into practice

There is not a single specific way to structure or re-structure your application while creating packages. In this article, we are going to follow the approach suggested by
Stephan Hagemann in Gradual Modularization for Ruby on Rails.

Choose the project

You can create a new project or choose one of your projects. I decided to use an open-source project called CodeTriage. It is important to mention that we need a Rails 6 application since Packwerk uses Zeitwerk.

Initialize Packwerk

First, we need to add the gem to our Gemfile like gem 'packwerk' and then run bundlein the console. Then we are ready to initialize the gem running packwerk init.

After that, we notice that Packwerk generated three files for us:

  • packwerk.yml

  • package.yml

  • inflections.yml

packwerk.yml is the configuration file of Packwerk where we will define included and excluded files, list the load paths, define the inflections file, among other things;

package.yml is the configuration file of a package. In this file, we will add the configuration for the boundaries of our package. Any folder with package.yml will be recognized as a package by Packwerk. That’s it, Packwerk created our first
package and we call it the root package.

inflections.yml is where we will place our custom inflections and acronyms in case we are using them.

You can find more about the files and their configuration in
Packwerk.

Packwerk properties

For modularization to work we need three basic properties: a named container, its content, and explicit dependencies on other containers. So let’s define those properties in Packwerk:

  • Name: The name of a package is its relative path from the root of the
    application.

  • Content: When we place a package.yml into a folder, all the files in the folder are now the content of the package.

  • Dependencies: We can define dependencies on other packages adding dependencies key to the package.yml.

Another file that’s not included by default but is recommended is README. It is important to provide information about the usage of the package.

The End of Episode I

Digital product development consulting

Read More

GraphQL Ruby. What about performance?

Rails and Other Means of Transport

Rails Development with TMUX, Vim, Fzf + Ripgrep

Related articles

Software Development

Build Future-Proof Web Apps: Insights from The Codest’s Expert Team

Discover how The Codest excels in creating scalable, interactive web applications with cutting-edge technologies, delivering seamless user experiences across all platforms. Learn how our expertise drives digital transformation and business...

THECODEST
Software Development

Top 10 Latvia-Based Software Development Companies

Learn about Latvia's top software development companies and their innovative solutions in our latest article. Discover how these tech leaders can help elevate your business.

thecodest
Enterprise & Scaleups Solutions

Java Software Development Essentials: A Guide to Outsourcing Successfully

Explore this essential guide on successfully outsourcing Java software development to enhance efficiency, access expertise, and drive project success with The Codest.

thecodest
Software Development

The Ultimate Guide to Outsourcing in Poland

The surge in outsourcing in Poland is driven by economic, educational, and technological advancements, fostering IT growth and a business-friendly climate.

TheCodest
Enterprise & Scaleups Solutions

The Complete Guide to IT Audit Tools and Techniques

IT audits ensure secure, efficient, and compliant systems. Learn more about their importance by reading the full article.

The Codest
Jakub Jakubowicz CTO & Co-Founder

Subscribe to our knowledge base and stay up to date on the expertise from the IT sector.

    About us

    The Codest – International software development company with tech hubs in Poland.

    United Kingdom - Headquarters

    • Office 303B, 182-184 High Street North E6 2JA
      London, England

    Poland - Local Tech Hubs

    • Fabryczna Office Park, Aleja
      Pokoju 18, 31-564 Kraków
    • Brain Embassy, Konstruktorska
      11, 02-673 Warsaw, Poland

      The Codest

    • Home
    • About us
    • Services
    • Case Studies
    • Know How
    • Careers
    • Dictionary

      Services

    • It Advisory
    • Software Development
    • Backend Development
    • Frontend Development
    • Staff Augmentation
    • Backend Developers
    • Cloud Engineers
    • Data Engineers
    • Other
    • QA Engineers

      Resources

    • Facts and Myths about Cooperating with External Software Development Partner
    • From the USA to Europe: Why do American startups decide to relocate to Europe
    • Tech Offshore Development Hubs Comparison: Tech Offshore Europe (Poland), ASEAN (Philippines), Eurasia (Turkey)
    • What are the top CTOs and CIOs Challenges?
    • The Codest
    • The Codest
    • The Codest
    • Privacy policy
    • Website terms of use

    Copyright © 2025 by The Codest. All rights reserved.

    en_USEnglish
    de_DEGerman sv_SESwedish da_DKDanish nb_NONorwegian fiFinnish fr_FRFrench pl_PLPolish arArabic it_ITItalian jaJapanese ko_KRKorean es_ESSpanish nl_NLDutch etEstonian elGreek en_USEnglish