class Rackful::Serializer

Base class for all serializers.

The serializers {Serializer::XHTML} and {Serializer::JSON} defined in this library depend on the presence of method {Rackful::Resource#to_rackful resource.to_rackful}. @abstract Subclasses must implement method ‘#each` and call {::produces} @example Create a new Serializer subclass:

class MySerializer < Rackful::Serializer
  produces 'text/plain'
  def each
    yield 'Hello world!'
  end
end

@!attribute [r] request

@return [Request]

@!attribute [r] resource

@return [Resource]

@!attribute [r] content_type

@return [String] The content type to be served by this Serializer. This will
  always be one of the content types listed in constant `CONTENT_TYPES`.

Attributes

content_type[R]
request[R]
resource[R]

Public Class Methods

content_types() click to toggle source
# File lib/rackful/serializer.rb, line 35
def content_types
  @content_types ||= begin
    retval = rackful_serializer_content_types
    if superclass.respond_to?(:content_types)
      retval += superclass.content_types
    end
    retval.uniq
  end
end
new(request, resource, content_type) click to toggle source

@param request [Request] @param resource [Resource] @param content_type [String]

# File lib/rackful/serializer.rb, line 71
def initialize request, resource, content_type
  @request, @resource, @content_type =
    request, resource, content_type
end
produces(*args) click to toggle source

@overload parses( media_type, … )

@param media_type [String]
# File lib/rackful/serializer.rb, line 47
def produces *args
  rackful_serializer_content_types.unshift(
    *( args.map { |ct| ct.to_s }.reverse )
  )
  rackful_serializer_content_types.uniq!
  self
end

Private Class Methods

rackful_serializer_content_types() click to toggle source
# File lib/rackful/serializer.rb, line 56
def rackful_serializer_content_types
  @rackful_serializer_content_types ||= []
end

Public Instance Methods

each() click to toggle source

@abstract Every serializer must implement this method. @yieldparam block [String] (part of) the entity body

# File lib/rackful/serializer.rb, line 90
def each
  raise NotImplementedError
end