A light weight, no dependency, plain old ruby objects on steroids.
require 'yukata'
require 'pp'
class Author < Yukata::Base
attribute :name, String
end
class Book < Yukata::Base
attribute :title, String
attribute :author, Author, coerce: false
attribute :created_at, Date, default: -> { Date.today }, writer: false
def author=(value)
@author = Author.new(value.to_h)
end
end
book = Book.new({
title: 'Hunt For Red October',
author: {
name: 'Tom Clancy'
}
})
pp book
This library comes with a custom coercion library. It's fairly straight forward.
Yukata.coercer.register(String, URI) do |string, type|
#
# Maybe do some fancy string cleanup before you coerce it
#
URI(string)
end
Then require that coercion to make sure it's loaded and all you simply have to do now is just do the following.
require 'pp'
class Website < Yukata::Base
attribute :uri, URI
end
website = Website.new({uri: 'http://example.com'})
pp website
- Fork it
- 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 new Pull Request