Inspired by a java library dozer
, this ruby gem is used to convert hash from one schema to another schema. It provides a convenient way to mapping data from one system to another systems.
Add this line to your application's Gemfile:
gem 'dozer'
And then execute:
$ bundle
Or install it yourself as:
$ gem install dozer
- include the module
Dozer::Mapperable
- use
mapping
method to mapping the data schema between source(:from) and destination(:to)
# adp_mapper.rb
class AdpMapper
include Dozer::Mapperable
mapping from: 'adp/firstName', to: :first_name
mapping from: 'adp/lastName', to: :last_name
mapping from: 'adp/gender', to: :gender
end
How to transform the value when mapping? You can do it in two ways: proc or method.
Usage example of Proc
class AdpMapper
include Dozer::Mapperable
mapping from: 'adp/gender', to: :gender, func: -> (value) { {male: 'M', female: 'F'}.fetch(value) }
mapping from: 'marital_status', to: :marriage,
mapping from: 'adp/gender', to: :gender
end
Usage example of method
class AdpMapper
include Dozer::Mapperable
mapping from: 'gender', to: :gender, func: :gender_to_integer
mapping from: 'marital_status', to: :marriage,
def gender_to_integer(val)
new_value = {male: 1, female: 2}.fetch(val, nil)
new_value
end
end
If you have multiple integrations, you can define a set of mapper classes in your Rails application folder.
mappers
- indeed_mapper.rb
- salesforce_mapper.rb
- adp_mapper.rb
data = { 'adp/firstName' => 'Ryan', 'adp/lastName' => 'Lyu' }
Dozer.map(data, AdpMapper)
=> {first_name: 'Ryan', last_name: 'Lyu'}
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dozer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Dozer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.