Module: Transmutation

Defined in:
lib/transmutation.rb,
lib/transmutation/version.rb,
lib/transmutation/serializer.rb,
lib/transmutation/serialization.rb,
lib/transmutation/class_attributes.rb,
lib/transmutation/object_serializer.rb,
lib/transmutation/serialization/lookup.rb,
lib/transmutation/serialization/rendering.rb,
lib/transmutation/serialization/lookup/serializer_not_found.rb

Overview

A performant and expressive solution for serializing Ruby objects into JSON, with a touch of opinionated “magic” ✨.

Examples:

Basic usage

# Define a data class.
class User
  attr_reader :name, :email

  def initialize(name:, email:)
    @name = name
    @email = email
  end
end

# Define a serializer.
class UserSerializer < Transmutation::Serializer
  attribute :name
end

# Create an instance of the data class.
user = User.new(name: "John", email: "[email protected]")

# Serialize the data class instance.
UserSerializer.new(user).to_json # => "{\"name\":\"John\"}"

Within a Rails controller

class UsersController < ApplicationController
  include Transmutation::Serialization

  def show
    user = User.find(params[:id])

    # Automatically lookup the UserSerializer
    # Serialize the data class instance using the UserSerializer
    # Render the result as JSON to the client
    render json: user # => "{\"name\":\"John\"}"
  end
end

Defined Under Namespace

Modules: ClassAttributes, Serialization Classes: Error, ObjectSerializer, Serializer

Constant Summary collapse

VERSION =
"0.5.1"