README.md in virtus-1.0.2 vs README.md in virtus-1.0.3

- old
+ new

@@ -4,18 +4,18 @@ [![Gem Version](https://badge.fury.io/rb/virtus.png)][gem] [![Build Status](https://secure.travis-ci.org/solnic/virtus.png?branch=master)][travis] [![Dependency Status](https://gemnasium.com/solnic/virtus.png)][gemnasium] [![Code Climate](https://codeclimate.com/github/solnic/virtus.png)][codeclimate] [![Coverage Status](https://coveralls.io/repos/solnic/virtus/badge.png?branch=master)][coveralls] -[![Inline docs](http://inch-pages.github.io/github/solnic/virtus.png)][inchpages] +[![Inline docs](http://inch-ci.org/github/solnic/virtus.png)][inchpages] [gem]: https://rubygems.org/gems/virtus [travis]: https://travis-ci.org/solnic/virtus [gemnasium]: https://gemnasium.com/solnic/virtus [codeclimate]: https://codeclimate.com/github/solnic/virtus [coveralls]: https://coveralls.io/r/solnic/virtus -[inchpages]: http://inch-pages.github.io/github/solnic/virtus +[inchpages]: http://inch-ci.org/github/solnic/virtus This is a partial extraction of the DataMapper [Property API](http://rubydoc.info/github/datamapper/dm-core/master/DataMapper/Property) with various modifications and improvements. The goal is to provide a common API for defining attributes on a model so all ORMs/ODMs could use it instead of @@ -271,10 +271,26 @@ package = Package.new(:dimensions => { 'width' => "2.2", :height => 2, "length" => 4.5 }) package.dimensions # => { :width => 2.2, :height => 2.0, :length => 4.5 } ``` +### IMPORTANT note about Boolean type + +Be aware that some libraries may do a terrible thing and define a global Boolean +constant which breaks virtus' constant type lookup, if you see issues with the +boolean type you can workaround it like that: + +``` ruby +class User + include Virtus.model + + attribute :admin, Axiom::Types::Boolean +end +``` + +This will be improved in Virtus 2.0. + ### IMPORTANT note about member coercions Virtus performs coercions only when a value is being assigned. If you mutate the value later on using its own interfaces then coercion won't be triggered. @@ -381,11 +397,11 @@ end class User include Virtus.model - attribute :info, Json + attribute :info, Json, default: {} end user = User.new user.info = '{"email":"john@domain.com"}' # => {"email"=>"john@domain.com"} user.info.class # => Hash @@ -427,10 +443,35 @@ user.set_unique_id('1234-1234') user.unique_id # => '1234-1234' ``` +### Overriding setters + +``` ruby +class User + include Virtus.model + + attribute :name, String + + def name=(new_name) + custom_name = nil + if new_name == "Godzilla" + custom_name = "Can't tell" + end + super custom_name || new_name + end +end + +user = User.new(name: "Frank") +user.name # => 'Frank' + +user = User.new(name: "Godzilla") +user.name # => 'Can't tell' + +``` + ## Strict Coercion Mode By default Virtus returns the input value even when it couldn't coerce it to the expected type. If you want to catch such cases in a noisy way you can use the strict mode in which Virtus raises an exception when it failed to coerce an input value. @@ -451,11 +492,11 @@ You can also build Virtus modules that contain their own configuration. ```ruby YupNopeBooleans = Virtus.model { |mod| mod.coerce = true - mod.string.boolean_map = { 'yup' => true, 'nope' => false } + mod.coercer.config.string.boolean_map = { 'nope' => false, 'yup' => true } } class User include YupNopeBooleans @@ -498,9 +539,20 @@ # constants will be resolved: Blog.attribute_set[:posts].member_type.primitive # => Post Post.attribute_set[:blog].type.primitive # => Blog ``` + +Ruby version support +-------------------- + +Virtus is known to work correctly with the following rubies: + +* 1.9.3 +* 2.0.0 +* 2.1.2 +* jruby +* (probably) rbx Credits ------- * Dan Kubb ([dkubb](https://github.com/dkubb))