Class: ActionController::Routing::OptionalFormatSegment

Inherits:
DynamicSegment show all
Defined in:
lib/action_controller/routing/segments.rb

Overview

The OptionalFormatSegment allows for any resource route to have an optional :format, which decreases the amount of routes created by 50%.

Constant Summary

Constants inherited from Segment

Segment::RESERVED_PCHAR, Segment::SAFE_PCHAR, Segment::UNSAFE_PCHAR

Instance Attribute Summary

Attributes inherited from DynamicSegment

#default, #key, #regexp

Attributes inherited from Segment

#is_optional

Instance Method Summary collapse

Methods inherited from DynamicSegment

#build_pattern, #default_regexp_chunk, #expiry_statement, #extraction_code, #local_name, #number_of_captures, #optionality_implied?, #regexp_has_modifiers?, #regexp_string, #string_structure, #value_check, #value_regexp

Methods inherited from Segment

#all_optionals_available_condition, #continue_string_structure, #extraction_code, #interpolation_statement, #number_of_captures, #optionality_implied?, #string_structure

Constructor Details

#initialize(key = nil, options = {}) ⇒ OptionalFormatSegment

Returns a new instance of OptionalFormatSegment.



312
313
314
# File 'lib/action_controller/routing/segments.rb', line 312

def initialize(key = nil, options = {})
  super(:format, {:optional => true}.merge(options))            
end

Instance Method Details

#extract_valueObject



328
329
330
# File 'lib/action_controller/routing/segments.rb', line 328

def extract_value
  "#{local_name} = options[:#{key}] && options[:#{key}].to_s.downcase"
end

#interpolation_chunkObject



316
317
318
# File 'lib/action_controller/routing/segments.rb', line 316

def interpolation_chunk
  "." + super
end

#match_extraction(next_capture) ⇒ Object

the value should not include the period (.)



333
334
335
336
337
338
339
# File 'lib/action_controller/routing/segments.rb', line 333

def match_extraction(next_capture)
  %[
    if (m = match[#{next_capture}])
      params[:#{key}] = URI.unescape(m.from(1))
    end
  ]
end

#regexp_chunkObject



320
321
322
# File 'lib/action_controller/routing/segments.rb', line 320

def regexp_chunk
  '/|(\.[^/?\.]+)?'
end

#to_sObject



324
325
326
# File 'lib/action_controller/routing/segments.rb', line 324

def to_s
  '(.:format)?'
end