# Mastering data mapping in Rails with dry-transformer

*Tutorial · 2024-04-25 · 2 min read · https://guilherme-andrade.com/writing/mastering-data-mapping-in-ruby-on-rails-with-dry-transformer/*

## 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

```ruby
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:

```ruby
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:

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

### Example 2: Multiplying Values

```ruby
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

```ruby
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:

```ruby
{
  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!
