module Rackful::Resource::ClassMethods

Public Instance Methods

add_parser(parser, method = :PUT) click to toggle source

Meta-programmer method.

A parser is an object or a class that implements to the following two methods:

  1. (Array<String>) media_types()

  2. (void) parse(Request, Resource)

The first call returns a list of media types this parser can parse, for example: ‘[ ’text/html’, ‘application/xhtml+xml’ ]‘. The second call parses a request in the context of a certain resource. @example Have your resource accept XHTML in `PUT` requests

class MyResource
  include Rackful::Resource
  add_parser Rackful::Parser::XHTML, :PUT
end

@param parser [#parse, media_types] an implementation (ie. subclass) of {Parser} @param method [#to_sym] For example: ‘:PUT` or `:POST` @return [self]

# File lib/rackful/resource.rb, line 118
def add_parser parser, method = :PUT
  method = method.to_sym
  parsers[method] ||= []
  parser.media_types.each do
    |mt|
    parsers[method] << [mt, parser]
  end
  parsers[method].uniq!
  self
end
add_serializer(serializer, quality = 1.0) click to toggle source

Meta-programmer method. @example Have your resource rendered in XML and JSON

class MyResource
  include Rackful::Resource
  add_serializer MyResource2XML
  add_serializer MyResource2JSON, 0.5
end

@param serializer [Serializer] @param quality [Float] @return [self]

# File lib/rackful/resource.rb, line 84
def add_serializer serializer, quality = 1.0
  quality = quality.to_f
  quality = 1.0 if quality > 1.0
  quality = 0.0 if quality < 0.0
  sq = [serializer, quality]
  serializer.content_types.each do
    |content_type|
    unless  ( old = serializers[content_type] ) and # @formatter:off
            old[1] > quality # @formatter:on
      serializers[content_type] = sq
    end
  end
  self
end
all_parsers() click to toggle source

All parsers for this class, including parsers added to parent classes. The result of this method is cached, which will interfere with code reloading. @return [Hash{Symbol => Array<Class>}] @api private

# File lib/rackful/resource.rb, line 134
def all_parsers
  # The single '@' on the following line is on purpose!
  @rackful_resource_all_parsers ||=
    if self.superclass.respond_to?(:all_parsers)
      self.superclass.all_parsers.merge( parsers ) do
        |key, oldval, newval|
        ( oldval + newval ).uniq
      end
    else
      parsers
    end
end
all_serializers() click to toggle source

All serializers for this class, including those added to parent classes. The result of this method is cached, which will interfere with code reloading. @return [Hash{ String( content_type ) => Array( Serializer, Float(quality) ) }] @api private

# File lib/rackful/resource.rb, line 152
def all_serializers
  # The single '@' on the following line is on purpose!
  @rackful_resource_all_serializers ||=
    if self.superclass.respond_to?(:all_serializers)
      self.superclass.all_serializers.merge( serializers ) do
        |key, oldval, newval|
        newval[1] >= oldval[1] ? newval : oldval
      end
    else
      serializers
    end
end

Private Instance Methods

parsers() click to toggle source

All parsers added to this class. Ie. not including parsers added to parent classes. @return [Hash{Symbol => Array<Class>}] A hash of lists of {Parser} classes,

indexed by HTTP method.

@api private

# File lib/rackful/resource.rb, line 172
def parsers
  # The single '@' on the following line is on purpose!
  @rackful_resource_parsers ||= {}
end
serializers() click to toggle source

All serializers added to this class. Ie. not including serializers added to parent classes. @return [Hash{ String( content_type ) => Array( Serializer, Float(quality) ) }] @api private

# File lib/rackful/resource.rb, line 182
def serializers
  # The single '@' on the following line is on purpose!
  @rackful_resource_serializers ||= {}
end