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

Go Pro with POROs

Ifat
October 09, 2023

Go Pro with POROs

Plain Old Ruby Objects (POROs) are having a moment. Maybe you’ve heard a whisper in the corner about the jack-of-all trades Service Object, or a glimmering echo of advocacy for non-database-backed domain models? Think you’re using them right? Afraid you’re using them wrong? Then this is the talk for you! We’re going to explore the wonderful world of “convention plus choose-your-own configuration” of Rails codebases and the shining role of POROs (with their ride or dies, the Module). Come hear about the diversity of design patterns out in the wild so you too can confidently tell your coworkers “let’s just use a PORO for that”.

Ifat

October 09, 2023
Tweet

More Decks by Ifat

Other Decks in Technology

Transcript

  1. Services Virtual Domain Models Request & Presentation Objects API Wrappers

    Modules Common Ruby on Rails Design Patterns Database Wrappers
  2. • Methods closely tied to the table and domain model

    it represents • Can be used for any database- backed model, in any schema • Classes as part of Object- Relational Mapping (ORM) framework • An interface for working with your database models in Ruby • Supports querying, relationships, and data integrity What are They? How are They Used?
  3. • Model Concerns • Controller Concerns and View Helpers •

    Shared logic between objects • Calculators • Collection of related code • Cannot be instantiated • Can be used directly or to extend other bundles of code What are They? How are They Used?
  4. What is a PORO? A PORO is a “Plain Old

    Ruby Object”, a moniker used to distinguish any other Ruby class not backed by a database table (e.g., ActiveRecord classes)
  5. • Generally exposes only one public method • Often named

    as a “verb as a noun” or directly as a verb • Leverage attr_reader for the key attributes of the class • POROs that encapsulate business logic to perform calculations, transactions, or series of steps • Can leverage attr_* methods and expose public attributes • Can leverage inheritance What are They? How are They Used?
  6. • Reusable classes to interact with external APIs • Interfaces

    for making requests to the API • Abstractions for network request details What are They? How are They Used? • Methods for RESTful routes • Overwritable methods for adjusting header objects or payloads • Boilerplate code or helpers shared across subclasses
  7. • Public methods for model attributes, CRUD actions, and memoized

    calculations • API results wrappers, memoized calculations, and interdependent associations What are They? How are They Used? • POROs to represent domain objects that aren’t database tables • Often used for transient data that will be immediately worked on and does not need to persist
  8. • Presenters, Decorators, and View Components • Form Objects •

    Serializers • Transformers What are They? How are They Used? • POROs that support the request, response, or presentation layers • Reduce controller, model, and view complexity • Support easier testing strategies for layers we otherwise don’t readily test