Class: ActionController::MimeResponds::Collector

Inherits:
Object
  • Object
show all
Includes:
AbstractController::Collector
Defined in:
lib/action_controller/metal/mime_responds.rb

Overview

A container for responses available from the current controller for requests for different mime-types sent to a particular action.

The public controller methods respond_with and respond_to may be called with a block that is used to define responses to different mime-types, e.g. for respond_to :

respond_to do |format|
  format.html
  format.xml { render xml: @people }
end

In this usage, the argument passed to the block (format above) is an instance of the ActionController::MimeResponds::Collector class. This object serves as a container in which available responses can be stored by calling any of the dynamically generated, mime-type-specific methods such as html, xml etc on the Collector. Each response is represented by a corresponding block if present.

A subsequent call to #negotiate_format(request) will enable the Collector to determine which specific mime-type it should respond with for the current request, with this response then being accessible by calling #response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AbstractController::Collector

generate_method_for_mime

Constructor Details

#initialize(mimes) ⇒ Collector

Returns a new instance of Collector.



402
403
404
405
# File 'lib/action_controller/metal/mime_responds.rb', line 402

def initialize(mimes)
  @order, @responses = [], {}
  mimes.each { |mime| send(mime) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AbstractController::Collector

Instance Attribute Details

#formatObject

Returns the value of attribute format.



400
401
402
# File 'lib/action_controller/metal/mime_responds.rb', line 400

def format
  @format
end

#orderObject

Returns the value of attribute order.



400
401
402
# File 'lib/action_controller/metal/mime_responds.rb', line 400

def order
  @order
end

Instance Method Details

#any(*args, &block) ⇒ Object Also known as: all



407
408
409
410
411
412
413
# File 'lib/action_controller/metal/mime_responds.rb', line 407

def any(*args, &block)
  if args.any?
    args.each { |type| send(type, &block) }
  else
    custom(Mime::ALL, &block)
  end
end

#custom(mime_type, &block) ⇒ Object



416
417
418
419
420
# File 'lib/action_controller/metal/mime_responds.rb', line 416

def custom(mime_type, &block)
  mime_type = Mime::Type.lookup(mime_type.to_s) unless mime_type.is_a?(Mime::Type)
  @order << mime_type
  @responses[mime_type] ||= block
end

#negotiate_format(request) ⇒ Object



426
427
428
# File 'lib/action_controller/metal/mime_responds.rb', line 426

def negotiate_format(request)
  @format = request.negotiate_mime(order)
end

#responseObject



422
423
424
# File 'lib/action_controller/metal/mime_responds.rb', line 422

def response
  @responses.fetch(format, @responses[Mime::ALL])
end