Skip to content
Browse files

Update README

  • Loading branch information...
1 parent 1d9ad2a commit 47fb699416a07ac5f1207a564b9e201d2ec0f647 @solnic committed Feb 16, 2013
Showing with 25 additions and 2 deletions.
  1. +25 −2 README.md
View
27 README.md
@@ -357,11 +357,12 @@ venue_other = Venue.new(
venue.location === venue_other.location # => true
```
-### Custom Attribute Writers
+### Custom Coercions
``` ruby
require 'json'
+# With a custom writer class
class JsonWriter < Virtus::Attribute::Writer::Coercible
def coerce(value)
value.is_a?(Hash) ? value : JSON.parse(value)
@@ -377,6 +378,28 @@ end
user = User.new
user.info = '{"email":"john@domain.com"}' # => {"email"=>"john@domain.com"}
user.info.class # => Hash
+
+# With a custom attribute encapsulating coercion-specific configuration
+class NoisyString < Virtus::Attribute::String
+ class UpperCase < Virtus::Attribute::Writer::Coercible
+ def coerce(value)
+ super.upcase
+ end
+ end
+
+ def self.writer_class(*)
+ UpperCase
+ end
+end
+
+class User
+ include Virtus
+
+ attribute :scream, NoisyString
+end
+
+user = User.new(:scream => 'hello world!')
+user.scream # => "HELLO WORLD!"
```
Credits
@@ -404,7 +427,7 @@ Contributing
License
-------
-Copyright (c) 2011-2012 Piotr Solnica
+Copyright (c) 2011-2013 Piotr Solnica
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

0 comments on commit 47fb699

Please sign in to comment.
Something went wrong with that request. Please try again.