Skip to content

Repository files navigation

MethodObject

VersionLicenseBuild StatusMaintainability

TL; DR

The method object pattern implies that you have one Ruby class for one single thing you want to perform.

This gem helps you to do just that with minimal overhead. The convention is to use the .call class method like so:

classSaySometingincludeMethodObject# Inputoption:text# OutputdefcallputstextendendSaySometihng.call(text: 'Hi there!')# => 'Hi there!'

Rationale

A minimal implementation of the method object pattern would probably look like this. This is sometimes also referred to as "service class".

classSaySometingdefself.call(*args, &block)new.call(*args, &block)enddefcall(text)putstextendend

Fun fact: previously that was actually the implementation of this gem.

Basically everything passed to MyClass.call(...) would be passed on to MyClass.new.call(...).

Even better still, it should be passed on to MyClass.new(...).call so that your implementation becomes cleaner:

classSaySometingdefself.call(*args, &block)new(*args, &block).callenddefinitialize(text:)@text=textenddefcallputs@textendend

People implemented that, but in doing so reinvented the wheel. Because now you not only have the method object pattern (i.e. call), now you also have to deal with initialization (i.e. new).

That's where the popular dry-initializer gem comes in. It is a battle-tested way to initialize objects with mandatory and optional attributes.

The method_object gem (you're looking at it right now), combines both the method object pattern and dry initialization.

Installation

# Add this to your Gemfile
gem 'method_object`

Usage

If you only have one mandatory, obvious argument, this is what your implementation most likely would look like:

classCalculateTaxincludeMethodObjectparam:productdefcallproduct.price * 0.1endendbike=Bike.new(price: 50)CalculateTax.call(bike)# => 5

If you prefer to use named keywords, use this instead:

classCalculateTaxincludeMethodObjectoption:productdefcallproduct.price * 0.1endendbike=Bike.new(price: 50)CalculateTax.call(product: bike)# => 5

You can also use both params and options. They are all mandatory.

classCalculateTaxincludeMethodObjectparam:productoption:dutyfreedefcallreturn0ifdutyfreeproduct.price * 0.1endendbike=Bike.new(price: 50)CalculateTax.call(bike,dutyfree: true)# => 0

You can make options optional by defining a default value in a proc:

classCalculateTaxincludeMethodObjectparam:productoption:dutyfree,default: ->{false}defcallreturn0ifdutyfreeproduct.price * 0.1endendbike=Bike.new(price: 50)CalculateTax.call(bike)# => 5

That's it!

Caveats

  • params cannot be optional (or have default values). This is because there can be several params in a row, which leads to confusion when they are optional.

Thanks

  • A big thank you to Jared who was so kind to give us the method_object gem name for our implementation.
  • The dry-rb team for their sense of beauty.

License

MIT License, see LICENSE.md

About

Combining the method object pattern with dry-initializer

Topics

Resources

Stars

3 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages