Class: Rack::App::Serializer

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Serializer

Returns a new instance of Serializer.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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

Instance Method Details

#extnamesObject



40
41
42
# File 'lib/rack/app/serializer.rb', line 40

def extnames
  (@formatters.keys + @content_types.keys).uniq
end

#response_headers_for(extname) ⇒ Object



25
26
27
28
29
# 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) ⇒ Object



21
22
23
# File 'lib/rack/app/serializer.rb', line 21

def serialize(extname, object)
  String(@formatters[extname].call(object))
end

#to_optionsObject



31
32
33
34
35
36
37
38
# 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