varvet / serial

Plain old Ruby for generating primitive data structures from object graphs.
108 stars 2 forks source link

Serial

Build Status Dependency Status Code Climate Gem Version Inline docs

Psst, full documentation can be found at rubydoc.info/gems/serial

Serial is a light-weight and simple serialization library. Its primary purpose is to generate primitive datastructures from object graphs, in other words to help you serialize your data.

Serial is sponsored by Elabs.

elabs logo

Installation

Add this line to your application's Gemfile:

gem "serial"

And then execute:

$ bundle

The DSL

Full reference: Serial::HashBuilder, Serial::ArrayBuilder.

Simple attributes

#attribute creates a simple attribute with a value.

ProjectSerializer = Serial::Serializer.new do |h, project|
  h.attribute(:id, project.id)
  h.attribute(:displayName, project.display_name)
end # => { "id" => …, "displayName" => … }

Nested attributes

#attribute supports nesting by giving it a block.

ProjectSerializer = Serial::Serializer.new do |h, project|
  h.attribute(:owner, project.owner) do |h, owner|
    h.attribute(:name, owner.name)
  end
end # => { "owner" => { "name" => … } }

Collections

#map is a convenient method for serializing lists of items.

ProjectSerializer = Serial::Serializer.new do |h, project|
  h.map(:assignments, project.assignments) do |h, assignment|
    h.attribute(:id, assignment.id)
    h.attribute(:duration, assignment.duration)
  end
end # => { "assignments" => [{ "id" => …, "duration" => … }, …] }

The low-level interface powering #map is #collection.

ProjectSerializer = Serial::Serializer.new do |h, project|
  h.collection(:indices) do |l|
    l.element { |h| h.attribute(…)  }
    l.element { |h| h.attribute(…)  }

    l.collection do |l|
      l.element { … }
      l.element { … }
    end
  end
end # => { "indices" => [{…}, {…}, [{…}, {…}]] }

Merging

#merge will let you merge another serializer without introducing a new nesting level.

ProjectSerializer = Serial::Serializer.new do |h, project|
  h.attribute(:name, project.name)
end # => { "name" => … }

FullProjectSerializer = Serial::Serializer.new do |h, project|
  h.merge(project, &ProjectSerializer)
  h.attribute(:description, project.description)
end # { "name" => …, "description" => … }

Composition

You can compose serializers by passing them as blocks to the DSL methods.

PersonSerializer = Serial::Serializer.new do |h, person|
  h.attribute(:name, person.name)
end # => { "name" => … }

ProjectSerializer = Serial::Serializer.new do |h, project|
  h.attribute(:owner, project.owner, &PersonSerializer)
  h.map(:people, project.people, &PersonSerializer)
end # { "owner" => { "name" => … }, "people" => [{ "name" => … }, …] }

Using your serializers

Full reference: Serial::Serializer.

Serializing a single object

project = Project.find(…)
context = self
ProjectSerializer.call(context, project) # => { … }

Serializing a list of objects

projects = Project.all
context = self
ProjectSerializer.map(context, projects) # => [{ … }, …]

Using with Rails

# app/serializers/project_serializer.rb
ProjectSerializer = Serial::Serializer.new do |h, project|
  …
end
# app/controllers/project_controller.rb
class ProjectController < ApplicationController
  include Serial::RailsHelpers

  def show
    project = Project.find(…)

    # 1. Using helper from RailsHelpers.
    render json: serialize(project)

    # 2. Explicitly mentioning serializer using helper method.
    render json: serialize(project, &ProjectSerializer)

    # 3. Explicitly mentioning serializer.
    render json: ProjectSerializer.call(self, project)
  end
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/elabs/serial. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.