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

Instance Attribute Summary collapse

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, context: nil) ⇒ Serializer

Returns a new instance of Serializer.



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

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

Instance Attribute Details

#contextObject, ... (readonly)

Returns:

  • (Object)

    the object being serialized

  • (Integer)

    depth the current nesting depth

  • (Integer)

    max_depth the maximum nesting depth to serialize

  • (Object, nil)

    context the caller-supplied context (defaults to the caller of serialize)



126
127
128
# File 'lib/transmutation/serializer.rb', line 126

def context
  @context
end

#depthObject, ... (readonly)

Returns:

  • (Object)

    the object being serialized

  • (Integer)

    depth the current nesting depth

  • (Integer)

    max_depth the maximum nesting depth to serialize

  • (Object, nil)

    context the caller-supplied context (defaults to the caller of serialize)



126
127
128
# File 'lib/transmutation/serializer.rb', line 126

def depth
  @depth
end

#max_depthObject, ... (readonly)

Returns:

  • (Object)

    the object being serialized

  • (Integer)

    depth the current nesting depth

  • (Integer)

    max_depth the maximum nesting depth to serialize

  • (Object, nil)

    context the caller-supplied context (defaults to the caller of serialize)



126
127
128
# File 'lib/transmutation/serializer.rb', line 126

def max_depth
  @max_depth
end

#objectObject, ... (readonly)

Returns:

  • (Object)

    the object being serialized

  • (Integer)

    depth the current nesting depth

  • (Integer)

    max_depth the maximum nesting depth to serialize

  • (Object, nil)

    context the caller-supplied context (defaults to the caller of serialize)



126
127
128
# File 'lib/transmutation/serializer.rb', line 126

def object
  @object
end

Class Method Details

.association(association_name, namespace: nil, serializer: nil, **options) {|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

  • if (Symbol, Proc)

    Only include the association when the condition evaluates truthy

  • unless (Symbol, Proc)

    Exclude the association when the condition evaluates truthy

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


85
86
87
# File 'lib/transmutation/serializer.rb', line 85

def association(association_name, namespace: nil, serializer: nil, **options, &block)
  fields[association_name] = Association.new(association_name, namespace:, serializer:, **options, &block)
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



111
112
113
114
115
# File 'lib/transmutation/serializer.rb', line 111

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

.attribute(attribute_name, **options) {|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

  attribute :email, if: :admin?
end

Parameters:

  • attribute_name (Symbol)

    The name of the attribute to serialize

  • if (Symbol, Proc)

    Only include the attribute when the condition evaluates truthy

  • unless (Symbol, Proc)

    Exclude the attribute when the condition evaluates truthy

    • A Symbol is sent to the serializer instance, a Proc is evaluated in its context

Yields:

  • (object)

    The block to call to get the value of the attribute

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


60
61
62
# File 'lib/transmutation/serializer.rb', line 60

def attribute(attribute_name, **options, &block)
  fields[attribute_name] = Attribute.new(attribute_name, **options, &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



97
98
99
100
101
# File 'lib/transmutation/serializer.rb', line 97

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

Instance Method Details

#as_json(options = {}) ⇒ Object



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

def as_json(options = {})
  fields.each_with_object({}) do |field, hash|
    hash[field.key] = field.value(self, options) if field.render?(self)
  end
end

#to_json(options = {}) ⇒ Object



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

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