Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jeremyf/mappy
https://github.com/jeremyf/mappy
Last synced: 28 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/jeremyf/mappy
- Owner: jeremyf
- License: other
- Created: 2014-02-11T20:13:03.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2016-03-24T18:44:12.000Z (almost 9 years ago)
- Last Synced: 2024-03-15T04:46:57.949Z (10 months ago)
- Language: Ruby
- Homepage: https://github.com/jeremyf/mappy
- Size: 38.1 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Mappy [![Build Status](https://travis-ci.org/jeremyf/mappy.png?branch=master)](https://travis-ci.org/jeremyf/mappy)
Map one object to another.
## Installation
Add this line to your application's Gemfile:
gem 'mappy'
And then execute:
$ bundle
Or install it yourself:
$ gem install mappy
## Usage
You can certainly read the documentation. In particular the [Mappy module](lib/mappy.rb).
But if you are like me, you won't trust the documentation.
Instead I would recommend reading the specs, especially [mappy_spec](spec/mappy_spec.rb)### TL;DR
```ruby
class Book
attr_accessor :name, :authors
def initialize(attributes = {})
attributes.each {|key, value| send("#{key}=", value) if respond_to?("#{key}=") }
end
endclass Orcid::Work
attr_accessor :title, :author
def initialize(attributes = {})
attributes.each {|key, value| send("#{key}=", value) if respond_to?("#{key}=") }
end
endMappy.configure do |config|
config.register(
source: 'book',
target: 'orcid/work',
legend: [
[:name, :title]
[lambda{|book| book.authors.first}, :author]
]
)
endbook = Book.new(title: 'Hello World', authors: ["George", "Paul", "John", "Ringo"])
orcid_work = Mappy.map(book, target: 'orcid/work')assert_equal orcid_work.title, book.name
assert_equal orcid_work.author, "George"
```## Why?
Because I have discovered that I am regularly working with a heterogenious set of
objects that need to be handled via some other service. And that service has its
expected data structure. And there are quite a few services.That is to say, I'm attempting to submit a work to my Orcid Profile. I need some
method for defining how to map my home grown "Article" object to the attributes
that are needed for a work in my Orcid Profile. And I don't want to infect my Article class
with a #to_orcid_profile method.Or perhaps I want to create a DOI for my Article. To successfully mint a DOI request, I need to have a valid payload. But what are the odds my Article can be passed directly to the DOI minting service.
In essence, Mappy provides a configurable converter for an application.
## TODO
Perhaps there is a DSL to be found in this.
Maybe I should look to [Lotus::Model::Mapper](https://github.com/lotus/model/blob/master/lib/lotus/model/mapper.rb) and its corresponding Coercer and Collection.