Hashformer: Transform data in Ruby

Hashformer on GitHub

Let me tell you about Hashformer, another open source Ruby Gem I wrote for my previous employer. Like ClassyHash, Hashformer was written as part of the “middleware” interface between a Solidus ecommerce site and an Oracle E-business Suite backend. Hashformer defines a Ruby DSL for basic data transformations, like renaming keys, merging values, etc.

Hashformer is a bit more niche than ClassyHash — most projects probably wouldn’t have a use for it, but when you need it, you need it. It’s similar in spirit to that old four-letter word, XSLT, but simpler and Ruby-centric. Read on for an example.

An example

The Hashformer DSL is built around Ruby’s Hash, just like ClassyHash. Here’s an example (based on examples from the Hashformer README):

1
2
3
4
5
6
7
data = { full_name: 'Hashformer Example' }
xform = {
  name: :full_name, # Simple renaming
  up_name: HF[:full_name].upcase.__end, # Chained method calls
}
Hashformer.transform(data, xform)
# => { name: 'Hashformer Example', up_name: 'HASHFORMER EXAMPLE' }

When to use Hashformer

So should you use Hashformer? Most of the time, the answer is no. If your transformations are pretty simple, just write plain Ruby code to build the new Hash directly. Use Hashformer if you need to rearrange a lot of keys when data transits from one system to another.

In the project for which it was written it transformed data Hashes from a defined message bus format (in ClassyHash schemas) to whatever format was required by a backend system (PL/SQL procedure calls, REST API calls, data warehouse UPSERTs, etc.). If we extended the repo API example from my ClassyHash post, we could have used Hashformer to convert responses from a new API version to an old API version.

I’m not going to post a full tutorial for Hashformer like I did for ClassyHash, so check out the README if you think you might want to use it.