Mastering Data Mapping in Ruby on Rails with Dry-Transformer

Data mapping is a crucial aspect of Ruby on Rails development, allowing developers to transform and manipulate data seamlessly across different layers of an application. `dry-transformer` is a powerful gem designed to simplify and optimize data mapping tasks by providing a flexible and composable framework for defining and applying transformations. In this comprehensive guide, we'll explore the capabilities of `dry-transformer` and demonstrate its usage through practical examples, illustrating how it can streamline data mapping in Ruby on Rails applications.

Mastering Data Mapping in Ruby on Rails with Dry-Transformer
GUILHERME ANDRADE
April 26, 2024
/
Tutorials

Understanding Dry-Transformer

dry-transformer offers a versatile toolkit for defining and applying data transformations in Ruby on Rails applications. By leveraging composable functions known as transformers, developers can easily define complex mappings between different data structures. With a declarative syntax and modular approach, dry-transformer enables clean and efficient data manipulation, making it an invaluable tool for optimizing data mapping tasks.

Usage Examples

Example 1: Renaming Keys and Nesting Data

require 'dry/transformer'

class DataMapper
  extend Dry::Transformer::Registry
  import Dry::Transformer::HashTransformations

  # Define transformations
  map_keys rename_keys: { user_name: :name, user_age: :age }
  rename_keys user_email: :email
  nest :address, [:street, :city, :zip_code]
end

Usage: Let's say we have input data like this:

input_data = {
  user_name: 'John',
  user_age: 30,
  user_email: 'john@example.com',
  street: '123 Main St',
  city: 'New York',
  zip_code: '10001'
}

When we call DataMapper.new.call(input_data), it transforms the data as follows:

{
  name: 'John',
  age: 30,
  email: 'john@example.com',
  address: {
    street: '123 Main St',
    city: 'New York',
    zip_code: '10001'
  }
}

Example 2: Multiplying Values

require 'dry/transformer'

class DataMapper
  extend Dry::Transformer::Registry

  # Define custom transformation
  map_value price: ->(value) { value * 10 }
end

Usage: Suppose we have an input value of { price: 20 }. When we call DataMapper.new.call(input_data), it transforms the data to { price: 200 }.

Example 3: Composing Transformers

require 'dry/transformer'

module CustomTransformers
  extend Dry::Transformer::Registry
  import Dry::Transformer::HashTransformations

  # Define custom transformations
  map_keys rename_keys: { first_name: :name, user_age: :age }
  rename_keys user_email: :email
end

class DataMapper
  extend Dry::Transformer::Pipe
  import CustomTransformers
end

Usage: Let's say we have input data like { first_name: 'John', user_age: 30, user_email: 'john@example.com' }. When we call DataMapper.new.call(input_data), it transforms the data as follows:

{
  name: 'John',
  age: 30,
  email: 'john@example.com'
}

Conclusion

By leveraging `dry-transformer`, Ruby on Rails developers can achieve clean, efficient, and modular data mapping in their applications. Whether you need to rename keys, nest data, apply custom transformations, or compose multiple transformers, `dry-transformer` offers a flexible and powerful toolkit to meet your data mapping needs. Incorporate `dry-transformer` into your development workflow to streamline data manipulation and unlock new possibilities for data transformation in Ruby on Rails applications.

Happy mapping!