Sha256: b9dbb48fe6ae3f2f0b98d7b46d9581c3f6e3ba5d8f9462029874220ec6f15fc8

Contents?: true

Size: 1.03 KB

Versions: 54

Compression:

Stored size: 1.03 KB

Contents

class Rack::App::Serializer::FormatsBuilder

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

  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

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

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

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

end

Version data entries

54 entries across 54 versions & 2 rubygems

Version Path
rack-app-6.1.0 lib/rack/app/serializer/formats_builder.rb
rack-app-6.0.0 lib/rack/app/serializer/formats_builder.rb
rack-app-5.12.0 lib/rack/app/serializer/formats_builder.rb
rack-app-5.11.1 lib/rack/app/serializer/formats_builder.rb
rack-app-5.11.0 lib/rack/app/serializer/formats_builder.rb
rack-app-5.10.2 lib/rack/app/serializer/formats_builder.rb
rack-app-5.10.1 lib/rack/app/serializer/formats_builder.rb
rack-app-5.10.0 lib/rack/app/serializer/formats_builder.rb
rack-app-5.9.0 lib/rack/app/serializer/formats_builder.rb
rack-app-5.8.0 lib/rack/app/serializer/formats_builder.rb
rack-app-5.7.0 lib/rack/app/serializer/formats_builder.rb
rack-app-5.7.0.rc lib/rack/app/serializer/formats_builder.rb
rack-app-5.6.0 lib/rack/app/serializer/formats_builder.rb
rack-app-5.5.1 lib/rack/app/serializer/formats_builder.rb