No description or website provided.
Failed to load latest commit information.
lib first commit May 31, 2012
spec first commit May 31, 2012
.gitignore first commit May 31, 2012
License.txt first commit May 31, 2012
README.rdoc added README Jun 20, 2012
Rakefile Version bump to 1.0.5 Jun 6, 2012
VERSION Version bump to 1.0.5 Jun 6, 2012
super_awesome_resource_serializer.gemspec updating contact email address Jun 6, 2012


Super Awesome Resource Serializer

This gem provides flexible serialization to Ruby objects.


Create a serializer by inheriting from SuperAwesomeResourceSerializer. Define the serialized fields using serialize. This method accepts the following options:

  • :element - The name of the field in the serialized output. This can be used to give a object a prettier name on output.

  • :getter - The getter method to get the field value.

  • :setter - The setter method to set the field value.

  • :exclude - Indicate that this field should be excluded unless specifically asked for. Values can be :getter, :setter, or true (for both).

By default, getters and setters will call the field accessor on the serializer (if it is defined) or on the object. This can be overridden by specifying a different method name to call, or a proc that will be yielded to with the object (and value for a setter). Finally, getters and setters can be disabled by setting them to false.

Pass the object that needs to be serialized as the first argument to the serializer initializer. The second argument is an optional options hash. The following options can be passed in:

Note: All of the following options accept a field or an array of fields.

  • :include - Forces the specified fields to be serialized.

  • :exclude - Forces the specified fields to be excluded from the serialization.

  • :only - Only the specified fields are serialized.

The serialization is done by invoking one of the following methods on the serializer object:

  • to_hash

  • to_json

  • to_yaml


> require 'ostruct' # only needed for the example
 => true
> obj = => "Nick", :location => "unknown")
 => #<OpenStruct location="unknown", name="Nick"> 
> class Serializer < SuperAwesomeResourceSerializer
>   serialize :name
>   serialize :location
> end
 => ...
> s =
 => ...
> s.to_hash
 => {"name"=>"Nick", "location"=>"unknown"}
> puts s.to_json
 => nil