Solving Backwards Compatibility in Ruby With a Proxy Object
In a previous article, I documented the upcoming public API changes slated for Sucker Punch v2. Because of a poor initial design, these API changes are backwards incompatible.
When I published the previous article, Mike Perham rightly pointed out the opportunity to support the previous versions’s API through an opt-in module. I was hesitant to include support for the old syntax by default, but allowing a developer to require a file to get the old syntax made complete sense to me. My intent was never to abandon existing Sucker Punch users, but it felt necessary for the success of the project going forward.
The following is an example of enqueueing a background job with Sucker Punch using the old syntax:
And with the new syntax:
How do we support the old syntax in the new version?
Let’s step back and reminder ourselves of what a typical job class looks like:
Important points to notice:
- Each job includes the
SuckerPunch::Jobmodule to gain access to asynchronous behavior
- Each job executes its logic using the
- Each job passes arguments needed for its logic as arguments to the
We’ll start with the test:
Note: Some details of this are complex because the job’s code is running in another thread. I’ll walk through those details in a future article.
The basic sequence is:
2. Execute a background job using the
3. Assert changes made in that job were successful
Running the tests above, we get the following error:
Ok, so the file doesn’t exist. Let’s create it and re-run the tests:
Progress! The job doesn’t have an
async method. Let’s add it:
Notice: We’re monkey-patching the
SuckerPunch::Job module. This will allow us to add methods to the background job since it’s included in the job.
The tests now:
async method we added returns nil, and because of the syntax
async.perform, there’s no
perform method on the output of
async. In short, we need to return something from
async that responds to
perform and can run the job.
In its most basic form, suppose we create a proxy object that responds to
We’ll need to do some work in
perform to execute the job, but this’ll do for now. Now, let’s integrate this new proxy to our
async_syntax.rb file and return a new instance of the proxy from the
Running our tests gives us the following:
Now we’re on to something. We see an error related to the number of arguments on the
perform method. Because each job’s argument list will be different, we need to find a way to be flexible for whatever’s passed in, something like…the splat operator! Let’s try it:
The tests now:
At this point, we’ve reached the end of test output suggesting the path forward. This error is saying, “Your assertions failed.”. This is good because it means our syntax implementation will work and it’s just about executing the actual job code in the proxy’s
We want to leverage our new syntax (
perform_async) to run the actual job asynchronously so it passes through the standard code path. To do so, we’ll need a reference to the original job in the proxy object. Let’s pass that to the proxy during instantiation:
Now that the proxy has a reference to the job instance, we can call the
perform_async class method to execute the job:
Lastly, the tests:
Just like that, new users of Sucker Punch will be able to add
require 'sucker_punch/async_syntax' to their projects to use the old syntax. This will allow existing projects using Sucker Punch to take advantage of the reworked internals without the need to make sweeping changes to the enqueueing syntax.
Support for the old syntax will be available for foreseeable future via this include. All new code/applications should use the new syntax going forward.
Before realizing a proxy object would work, I tinkered with
alias_method and a handful of other approaches to latching on to the job’s
perform method and saving it off to execute later. While some combinations of these might have worked, the proxy object solution is simple and elegant. There’s no magic, which means less maintenance going forward. The last thing I want is to make a breaking change, add support for the old syntax and find the support to be bug-ridden.
Ruby is incredibly flexible. Sometimes a 9-line class is enough to get the job done without reaching for an overly complex metaprogramming approach.
Having said all that, Sucker Punch
v2 has been