Class: Rack::App::Serializer
- Inherits:
-
Object
- Object
- Rack::App::Serializer
- Defined in:
- lib/rack/app/serializer.rb
Instance Method Summary collapse
- #extnames ⇒ Object
-
#initialize(options = {}) ⇒ Serializer
constructor
A new instance of Serializer.
- #response_headers_for(extname) ⇒ Object
- #serialize(extname, object) ⇒ Object
- #to_options ⇒ Object
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(={}) @default_formatter = [:default_formatter] || lambda { |o| o.to_s } @default_content_type = [:default_content_type] formatters = [:formatters] || {} content_types = [: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
#extnames ⇒ Object
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_options ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/rack/app/serializer.rb', line 31 def { :formatters => @formatters, :content_types => @content_types, :default_formatter => @default_formatter, :default_content_type => @default_content_type, } end |