Module: ActiveModel::Serializable

Included in:
ArraySerializer, Serializer
Defined in:
lib/active_model/serializable.rb

Overview

Enable classes to Classes including this module to serialize themselves by implementing a serialize method and an options method.

Example:

require 'active_model_serializers'

class MySerializer
  include ActiveModel::Serializable

  def initialize
    @options = {}
  end

  attr_reader :options

  def serialize
    { a: 1 }
  end
end

puts MySerializer.new.to_json

Instance Method Summary collapse

Instance Method Details

#as_json(args = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_model/serializable.rb', line 26

def as_json(args={})
  if root = args[:root] || options[:root]
    options[:hash] = hash = {}
    options[:unique_values] = {}

    hash.merge!(root => serialize)
    include_meta hash
    hash
  else
    serialize
  end
end