class Rack::App::Serializer

Constants

CONTENT_TYPE

Public Class Methods

new(options={}) click to toggle source
# File lib/rack/app/serializer.rb, line 5
def initialize(options={})

  @default_formatter = options[:default_formatter] || lambda { |o| o.to_s }
  @default_content_type = options[:default_content_type]

  formatters = options[:formatters] || {}
  content_types = options[:content_types] || {}

  @content_types = Hash.new(@default_content_type)
  @content_types.merge!(content_types)

  @formatters = Hash.new(@default_formatter)
  @formatters.merge!(formatters)

end

Public Instance Methods

extnames() click to toggle source
# File lib/rack/app/serializer.rb, line 40
def extnames
  (@formatters.keys + @content_types.keys).uniq
end
response_headers_for(extname) click to toggle source
# File lib/rack/app/serializer.rb, line 25
def response_headers_for(extname)
  headers = {}
  add_content_type_for(headers, extname)
  headers
end
serialize(extname, object) click to toggle source
# File lib/rack/app/serializer.rb, line 21
def serialize(extname, object)
  String(@formatters[extname].call(object))
end
to_options() click to toggle source
# File lib/rack/app/serializer.rb, line 31
def to_options
  {
    :formatters => @formatters,
    :content_types => @content_types,
    :default_formatter => @default_formatter,
    :default_content_type => @default_content_type,
  }
end

Protected Instance Methods

add_content_type_for(headers, extname) click to toggle source
# File lib/rack/app/serializer.rb, line 48
def add_content_type_for(headers, extname)
  content_type = @content_types[extname]
  if content_type
    headers[CONTENT_TYPE]= content_type.dup
  end
end