Class: Serial::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/serial/serializer.rb

Overview

Using this class you build serializers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|builder, value| ... } ⇒ Serializer

Create a new Serializer, using the block as instructions.

Examples:

# app/serializers/person_serializer.rb
PersonSerializer = Serial::Serializer.new do |h, person|
  h.attribute(:name, person.name)
end

Yields:

  • (builder, value)

Yield Parameters:



15
16
17
18
19
20
21
22
# File 'lib/serial/serializer.rb', line 15

def initialize(&block)
  unless block_given?
    raise ArgumentError, "instructions (block) is required"
  end

  @block = block
  @to_proc = method(:to_proc_implementation).to_proc
end

Instance Attribute Details

#to_procProc (readonly)

Serializer composition!

Examples:

# app/serializers/person_serializer.rb
PersonSerializer = Serial::Serializer.new do |h, person|
  h.attribute(:name, person.name)
end

# app/serializers/project_serializer.rb
ProjectSerializer = Serial::Serializer.new do |h, project|
  h.attribute(:owner, project.owner, &PersonSerializer)
  h.map(:people, project.people, &PersonSerializer)
end

Returns:

  • (Proc)


103
104
105
# File 'lib/serial/serializer.rb', line 103

def to_proc
  @to_proc
end

Instance Method Details

#call(context = nil, value) ⇒ Hash

Serialize an object with this serializer, optionally within a context.

Examples:

with context, the serializer block is evaluated inside the context

# app/serializers/person_serializer.rb
PersonSerializer = Serial::Serializer.new do |h, person|
  h.attribute(:id, person.id)
  h.attribute(:url, people_url(person))
end

# app/controllers/person_controller.rb
def show
  person = Person.find()
  render json: PersonSerializer.call(self, person)
end

without context, the serializer block is evaluated using normal closure rules

# app/models/person.rb
class Person
  Serializer = Serial::Serializer.new do |h, person|
    h.attribute(:id, person.id)
    h.attribute(:available_roles, available_roles)
  end

  def self.available_roles
    
  end
end

# app/controllers/person_controller.rb
def show
  person = Person.find()
  render json: Person::Serializer.call(person)
end

Parameters:

  • context (#instance_exec, nil) (defaults to: nil)

    context to execute serializer in, or nil to use regular block closure rules.

  • value

Returns:

  • (Hash)


61
62
63
# File 'lib/serial/serializer.rb', line 61

def call(context = nil, value)
  HashBuilder.build(context, value, &@block)
end

#map(context = nil, list) ⇒ Array<Hash>

Serialize a list of objects with this serializer, optionally within a context.

Examples:

# app/serializers/person_serializer.rb
PersonSerializer = Serial::Serializer.new do |h, person|
  h.attribute(:id, person.id)
  h.attribute(:url, people_url(person))
end

# app/controllers/person_controller.rb
def index
  people = Person.all
  render json: PersonSerializer.map(self, people)
end

Parameters:

  • list (#map)
  • context (#instance_exec, nil) (defaults to: nil)

    context to execute serializer in, or nil to use regular block closure rules.

Returns:

  • (Array<Hash>)

See Also:



84
85
86
# File 'lib/serial/serializer.rb', line 84

def map(context = nil, list)
  list.map { |item| call(context, item) }
end