Mask
is a protocol of micro service APIs.
Beside object serializer, mask
also provider a way generate ruby objects from hash.
So we can share the object-oriented API between services.
Add this line to your application's Gemfile:
gem 'mask'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mask
class Book
attr_accessor :title
attr_accessor :author
end
module BookPresenter
include Mask::Presenter
property :title
property :author
end
book = Book.new
book.title = "The Well-Grounded Rubyist"
book.author = "David A. Black"
book.extend(BookPresenter)
book.to_hash #=> {"title": "The Well-Grounded Rubyist", "author": "David A. Black" }
module SimpleBookPresenter
include Mask::Presenter
property :title
end
book.extend(SimpleBookPresenter)
book.to_hash #=> {"title": "The Well-Grounded Rubyist" }
book_data = {"title": "The Well-Grounded Rubyist", "author": "David A. Black" }
book = Book.new
book.extend(BookPresenter)
book.from_hash(book_data)
book.title #=> "The Well-Grounded Rubyist"
book.author #=> "David A. Black"
class Author
attr_accessor :name
attr_accessor :twitter
end
module AuthorPresenter
include Mask::Presenter
property :name
property :twitter
end
module BookWithAuthorPresenter
include Mask::Presenter
property :title
property :author, presenter: AuthorPresenter, klass: Author
end
book = Book.new
book.title = "The Well-Grounded Rubyist"
author = Author.new
author.name = "David A. Black"
author.twitter = "@david_a_black"
book.author = author
book.extend(BookWithAuthorPresenter)
book.to_hash #=> {"title": "The Well-Grounded Rubyist", "author": {"name": "David A. Black", "twitter": "@david_a_black" }}
book_data = {"title": "The Well-Grounded Rubyist", "author": {"name": "David A. Black", "twitter": "@david_a_black" }}
book = Book.new
book.extend(BookWithAuthorPresenter)
book.from_hash(book_data)
book.title #=> "The Well-Grounded Rubyist"
book.author.name #=> "David A. Black"
book.author.twitter #=> "@david_a_black"
- Fork it ( https://github.com/[my-github-username]/mask/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request