Ruby SuperMapper

SuperMapper is a quick and simple mapper between Ruby object.
Define a mapping between attribute readers and writers and automatically convert classes.


Add this line to your application’s Gemfile:

gem 'super_mapper'

And then execute:

$ bundle

Or install it yourself as:

$ gem install super_mapper


Defining mapping

Create a new Mapper instance, then start describing mapping to a specific class

mapper =

# Define a mapping from a User to a UserStruct

mapper.define_mapping User, UserStruct  do |user, user_struct|
  user_struct.first_name = user.first_name
# Apply transformations
  user_struct.username = user.username.downcase
# Generate new values 
  user_struct.created_at =

Apply conversions

user = User.first

# With target classes
user_struct = user, UserStruct

# With existing target objects (target is modified in place) user, user_struct

# Multiple mappings can be applied to the same target. 
# Later ones override previously set value if conflicts occur.

some_other_user_representation = context[:current_user] user, user_struct some_other_user_representation, user_struct

# +user_struct+ now has fields from both User and the other representation
If using target classes, they MUST implement a no-args constructor because new instances are created via


The gem is available as open source under the terms of the MIT License.