Skip to content

warmwaffles/yukata

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Yukata

A light weight, no dependency, plain old ruby objects on steroids.

Example

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

Creating A Coercion

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

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

About

A quick and lightweight ruby attribute library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages