Class: Rack::App::Serializer::FormatsBuilder

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

Instance Method Summary collapse

Constructor Details

#initializeFormatsBuilder

Returns a new instance of FormatsBuilder.



3
4
5
6
7
8
# File 'lib/rack/app/serializer/formats_builder.rb', line 3

def initialize
  @formatters = {}
  @content_types = {}
  @default_formatter = lambda { |o| o.to_s }
  @default_content_type = nil
end

Instance Method Details

#default(content_type = nil, &block) ⇒ Object



18
19
20
21
22
# File 'lib/rack/app/serializer/formats_builder.rb', line 18

def default(content_type=nil, &block)
  @default_content_type = content_type if content_type
  @default_formatter = block if block
  self
end

#merge!(formats_builder) ⇒ Object



33
34
35
36
37
# File 'lib/rack/app/serializer/formats_builder.rb', line 33

def merge!(formats_builder)
  @formatters.merge!(formats_builder.instance_variable_get(:@formatters))
  @default_formatter = formats_builder.instance_variable_get(:@default_formatter)
  self
end

#on(extname, response_content_type, &formatter) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/rack/app/serializer/formats_builder.rb', line 10

def on(extname, response_content_type, &formatter)
  format = String(extname).downcase
  extension = (format[0,1] == '.' ? format : ".#{format}")
  @formatters[extension]= formatter
  @content_types[extension]= response_content_type
  self
end

#to_serializerObject



24
25
26
27
28
29
30
31
# File 'lib/rack/app/serializer/formats_builder.rb', line 24

def to_serializer
  Rack::App::Serializer.new(
    :formatters => @formatters,
    :content_types => @content_types,
    :default_formatter => @default_formatter,
    :default_content_type => @default_content_type
  )
end