Project

nagare

0.0
No commit activity in last 3 years
No release in over 3 years
Serialize your ruby objects
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 3.0

Runtime

 Project Readme

nagare 流れ

Build Status

Nagare is a simple serializer for your Rails API.

Installation

gem 'nagare', github: 'oestrich/nagare'

Usage

Create your context

Create a specialized context for your application. Anything you place in here will be available inside of the serializers.

class ApplicationController < ActionController::Base
  private
  
  def nagare_context
    @nagare_context ||= Nagare::Context.new({
      current_user: current_user,
    })
  end
end

Create your serializers

There are item serializers and collection serializers.

class OrderSerializer < Nagare::Item
  attributes :id, :user_id
end

You can access the object being serialized by the object method.

class OrdersSerializer < Nagare::Collection
  attributes :count, :href
  
  def count
    collection.count
  end
end

You can access the collection being serialized by the collection method.

Use your serializers

You can extend the context per route very easily by specifying a context key with a new hash. Hash keys will be available as methods inside of the serializer.

class OrdersController < ApplicationController
  def index
    render({
      json: orders,
      serializers: { collection: OrdersSerializer, item: OrderSerializer },
      context: { href: orders_url },
    })
  end
  
  def show
    render({
      json: order,
      serializer: { item: OrderSerializer },
      context: { href: order_url(order) },
    })
  end
end