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

Inherits:
Object
  • Object
show all
Defined in:
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.



292
293
294
295
# File 'lib/action_controller/metal/mime_responds.rb', line 292

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



308
309
310
# File 'lib/action_controller/metal/mime_responds.rb', line 308

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

Instance Method Details

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



297
298
299
300
301
302
303
304
305
# File 'lib/action_controller/metal/mime_responds.rb', line 297

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



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/action_controller/metal/mime_responds.rb', line 312

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