Class: ActionController::MimeResponds::Collector::VariantCollector

Inherits:
Object
  • Object
show all
Defined in:
actionpack/lib/action_controller/metal/mime_responds.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(variant = nil) ⇒ VariantCollector

Returns a new instance of VariantCollector.


513
514
515
516
# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 513

def initialize(variant = nil)
  @variant = variant
  @variants = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object


529
530
531
# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 529

def method_missing(name, *args, &block)
  @variants[name] = block if block_given?
end

Instance Method Details

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


518
519
520
521
522
523
524
525
526
# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 518

def any(*args, &block)
  if block_given?
    if args.any? && args.none?{ |a| a == @variant }
      args.each{ |v| @variants[v] = block }
    else
      @variants[:any] = block
    end
  end
end

#variantObject


533
534
535
536
537
538
539
540
541
542
543
# File 'actionpack/lib/action_controller/metal/mime_responds.rb', line 533

def variant
  if @variant.nil?
    @variants[:none] || @variants[:any]
  elsif (@variants.keys & @variant).any?
    @variant.each do |v|
      return @variants[v] if @variants.key?(v)
    end
  else
    @variants[:any]
  end
end