No commit activity in last 3 years
No release in over 3 years
Object to Hash serializer
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

ActiveSerializer

Active Serializer is a simple objects to hash serializer.

Usage

Here's an example serializer:

class ContactSerializer
  include ActiveSerializer::SerializableObject

  serialization_rules do |contact, home_address, contact_emails|
    attributes :first_name, :last_name, contact

    attribute :full_name do
      "#{contact.first_name} #{contact.last_name}"
    end

    resource :address, home_address do |address|
      attributes :country, :city, :street, address
    end

    resources :emails, contact_emails do |email|
      attributes :email
      attribute :type do
        'home'
      end
    end
  end
end

Let's use it:

ContactSerializer.serialize(contact, home_address, contact_emails)

And we get a result:

{
  first_name: "John",
  last_name: "Smith",
  full_name: "John Smith",
  address: {
    country: "Russia",
    city: "Kazan",
    street: "Kosmonavton"
  },
  emails: [
    { email: "test@test.com", type: "home" },
    { email: "test2@test.com", type: "home" }
  ]
}

Installation

Add this line to your application's Gemfile:

gem 'active_serializer'

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_serializer

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