Class: Transmutation::Serializer

Inherits:
Object
  • Object
show all
Extended by:
ClassAttributes
Includes:
Serialization
Defined in:
lib/transmutation/serializer.rb

Overview

Base class for your serializers.

Examples:

Basic usage

class UserSerializer < Transmutation::Serializer
  attributes :first_name, :last_name

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

  belongs_to :organization

  has_many :posts
end

Direct Known Subclasses

ObjectSerializer

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassAttributes

class_attribute, class_attribute_reader, class_attribute_writer

Methods included from Serialization

cache, #lookup_serializer, #namespace, #serialize

Constructor Details

#initialize(object, depth: 0, max_depth: 1) ⇒ Serializer

Returns a new instance of Serializer.



23
24
25
26
27
# File 'lib/transmutation/serializer.rb', line 23

def initialize(object, depth: 0, max_depth: 1)
  @object = object
  @depth = depth
  @max_depth = max_depth
end

Class Method Details

.association(association_name, namespace: nil, serializer: nil) {|object| ... } ⇒ Object

Note:

By default, the serializer for the association is looked up in the same namespace as the serializer

Define an association to be serialized

Examples:

class UserSerializer < Transmutation::Serializer
  association :posts
  association :comments, namespace: "Nested", serializer: "User::CommentSerializer"
  association :archived_posts do
    object.posts.archived
  end
end

Parameters:

  • association_name (Symbol)

    The name of the association to serialize

  • namespace (String, Symbol, Module) (defaults to: nil)

    The namespace to lookup the association’s serializer in

  • serializer (String, Symbol, Class) (defaults to: nil)

    The serializer to use for the association’s serialization

Yields:

  • (object)

    The block to call to get the value of the association

    • The block is called in the context of the serializer instance

    • The return value from the block is automatically serialized



81
82
83
84
85
86
87
88
89
# File 'lib/transmutation/serializer.rb', line 81

def association(association_name, namespace: nil, serializer: nil, &custom_block)
  block = lambda do
    association_instance = custom_block ? instance_exec(&custom_block) : object.send(association_name)

    serialize(association_instance, namespace:, serializer:, depth: @depth + 1, max_depth: @max_depth)
  end

  attributes_config[association_name] = { block:, association: true }
end

.associations(*association_names) ⇒ Object Also known as: belongs_to, has_one, has_many

Shorthand for defining multiple associations

Examples:

class UserSerializer < Transmutation::Serializer
  associations :posts, :comments
end

Parameters:

  • association_names (Array<Symbol>)

    The names of the associations to serialize



113
114
115
116
117
# File 'lib/transmutation/serializer.rb', line 113

def associations(*association_names, **, &)
  association_names.each do |association_name|
    association(association_name, **, &)
  end
end

.attribute(attribute_name) {|object| ... } ⇒ Object

Define an attribute to be serialized

Examples:

class UserSerializer < Transmutation::Serializer
  attribute :first_name

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

Parameters:

  • attribute_name (Symbol)

    The name of the attribute to serialize

Yields:

  • (object)

    The block to call to get the value of the attribute

    • The block is called in the context of the serializer instance



58
59
60
# File 'lib/transmutation/serializer.rb', line 58

def attribute(attribute_name, &block)
  attributes_config[attribute_name] = { block: }
end

.attributes(*attribute_names) ⇒ Object

Shorthand for defining multiple attributes

Examples:

class UserSerializer < Transmutation::Serializer
  attributes :first_name, :last_name
end

Parameters:

  • attribute_names (Array<Symbol>)

    The names of the attributes to serialize



99
100
101
102
103
# File 'lib/transmutation/serializer.rb', line 99

def attributes(*attribute_names, **, &)
  attribute_names.each do |attribute_name|
    attribute(attribute_name, **, &)
  end
end

Instance Method Details

#as_json(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/transmutation/serializer.rb', line 33

def as_json(options = {})
  attributes_config.each_with_object({}) do |(attr_name, attr_options), hash|
    if attr_options[:association]
      hash[attr_name.to_s] = instance_exec(&attr_options[:block]).as_json(options) if @depth + 1 <= @max_depth
    else
      hash[attr_name.to_s] = attr_options[:block] ? instance_exec(&attr_options[:block]) : object.send(attr_name)
    end
  end
end

#to_json(options = {}) ⇒ Object



29
30
31
# File 'lib/transmutation/serializer.rb', line 29

def to_json(options = {})
  as_json(options).to_json
end