Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/timfjord/two-way-mapper
Two way data mapping
https://github.com/timfjord/two-way-mapper
Last synced: about 1 month ago
JSON representation
Two way data mapping
- Host: GitHub
- URL: https://github.com/timfjord/two-way-mapper
- Owner: timfjord
- License: mit
- Created: 2014-07-30T18:27:01.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2020-07-27T16:04:31.000Z (over 4 years ago)
- Last Synced: 2024-04-24T22:20:35.477Z (8 months ago)
- Language: Ruby
- Size: 46.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Two Way Mapper [![Build Status](https://travis-ci.org/timsly/two-way-mapper.svg?branch=master)](https://travis-ci.org/timsly/two-way-mapper)
Two way data mapping
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'two-way-mapper'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install two-way-mapper
## Usage
First, we need to define mapping
```ruby
TwoWayMapper.register :customer do |mapping|
mapping.left :object # set left plugin to object
mapping.right :hash, stringify_keys: true # set right plugin to hash# define transformation rules
mapping.rule 'first_name', 'FirstName'
mapping.rule 'last_name', 'LastName'
mapping.rule 'gender', 'sex',
map: {
'M' => 'male',
'F' => 'female'
}, default: ''
end
```For simple rules use
```ruby
TwoWayMapper.register :customer do |mapping|
mapping.left :object # set left plugin to object
mapping.right :hash, stringify_keys: true # set right plugin to hashmapping.rules(
'first_name' => 'FirstName',
'last_name' => 'LastName'
)
end
```Mapping can be defined explicitly without registration
```ruby
mapping = TwoWayMapper::Mapping.new
mapping.left :object
mapping.right :hash, stringify_keys: true
# ...
```Once the mapping is defined it can be used to convert one object to another and vice versa
```ruby
Customer = Struct.new :first_name, :last_name, :gendercustomer = Customer.new
api_response = { 'FirstName' => 'Evee', 'LastName' => 'Fjord', 'sex' => 'female' }TwoWayMapper[:customer].from_right_to_left(customer, api_response)
puts customer.first_name # => 'Evee'
puts customer.last_name # => 'Fjord'
puts customer.gender # => 'F'request_data = {}
another_customer = Customer.new
another_customer.first_name = 'Step'
another_customer.last_name = 'Bander'
another_customer.gender = 'M'TwoWayMapper[:customer].from_left_to_right(another_customer, request_data)
puts request_data # => { 'FirstName' => 'Step', 'LastName' => 'Bander', sex: 'male' }
```In rails, mappings can be defined in `app/mappings` folder
### Available plugins
* hash
* object
* active_record (same as `object`, but for keys like `user.email`, it builds `user` before updating `email` on write)## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request