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:
And then execute:
Or install it yourself as:
$ gem install super_mapper
Create a new Mapper instance, then start describing mapping to a specific class
mapper = SuperMapper.new # 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 = Time.now end
user = User.first # With target classes user_struct = mapper.map user, UserStruct # With existing target objects (target is modified in place) mapper.map 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] mapper.map user, user_struct mapper.map some_other_user_representation, user_struct # +user_struct+ now has fields from both User and the other representation
The gem is available as open source under the terms of the MIT License.