Method: Serial::Serializer#call

Defined in:
lib/serial/serializer.rb

#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(

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
    

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