Class: ActionController::Routing::DynamicSegment

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

Direct Known Subclasses

ControllerSegment, PathSegment

Instance Attribute Summary collapse

Attributes inherited from Segment

#is_optional

Instance Method Summary collapse

Methods inherited from Segment

#all_optionals_available_condition, #continue_string_structure, #interpolation_statement

Constructor Details

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

Returns a new instance of DynamicSegment.



643
644
645
646
647
648
# File 'lib/action_controller/routing.rb', line 643

def initialize(key = nil, options = {})
  super()
  self.key = key
  self.default = options[:default] if options.key? :default
  self.is_optional = true if options[:optional] || options.key?(:default)
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



641
642
643
# File 'lib/action_controller/routing.rb', line 641

def default
  @default
end

#keyObject

Returns the value of attribute key.



641
642
643
# File 'lib/action_controller/routing.rb', line 641

def key
  @key
end

#regexpObject

Returns the value of attribute regexp.



641
642
643
# File 'lib/action_controller/routing.rb', line 641

def regexp
  @regexp
end

Instance Method Details

#build_pattern(pattern) ⇒ Object



709
710
711
712
713
714
# File 'lib/action_controller/routing.rb', line 709

def build_pattern(pattern)
  chunk = regexp_chunk
  chunk = "(#{chunk})" if Regexp.new(chunk).number_of_captures == 0
  pattern = "#{chunk}#{pattern}"
  optional? ? Regexp.optionalize(pattern) : pattern
end

#expiry_statementObject



673
674
675
# File 'lib/action_controller/routing.rb', line 673

def expiry_statement
  "expired, hash = true, options if !expired && expire_on[:#{key}]"
end

#extract_valueObject



659
660
661
# File 'lib/action_controller/routing.rb', line 659

def extract_value
  "#{local_name} = hash[:#{key}] #{"|| #{default.inspect}" if default}"
end

#extraction_codeObject



677
678
679
680
681
682
# File 'lib/action_controller/routing.rb', line 677

def extraction_code
  s = extract_value
  vc = value_check
  s << "\nreturn [nil,nil] unless #{vc}" if vc
  s << "\n#{expiry_statement}"
end

#interpolation_chunkObject



684
685
686
# File 'lib/action_controller/routing.rb', line 684

def interpolation_chunk
  "\#{CGI.escape(#{local_name}.to_s)}"
end

#local_nameObject

The local variable name that the value of this segment will be extracted to.



655
656
657
# File 'lib/action_controller/routing.rb', line 655

def local_name
  "#{key}_value"
end

#match_extraction(next_capture) ⇒ Object



715
716
717
718
719
720
# File 'lib/action_controller/routing.rb', line 715

def match_extraction(next_capture)
  hangon = (default ? "|| #{default.inspect}" : "if match[#{next_capture}]")
  
  # All non code-related keys (such as :id, :slug) have to be unescaped as other CGI params
  "params[:#{key}] = match[#{next_capture}] #{hangon}"
end

#optionality_implied?Boolean

Returns:

  • (Boolean)


722
723
724
# File 'lib/action_controller/routing.rb', line 722

def optionality_implied?
  [:action, :id].include? key
end

#regexp_chunkObject



705
706
707
# File 'lib/action_controller/routing.rb', line 705

def regexp_chunk
  regexp ? "(#{regexp.source})" : "([^#{Routing::SEPARATORS.join}]+)"
end

#string_structure(prior_segments) ⇒ Object



688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/action_controller/routing.rb', line 688

def string_structure(prior_segments)
  if optional? # We have a conditional to do...
    # If we should not appear in the url, just write the code for the prior
    # segments. This occurs if our value is the default value, or, if we are
    # optional, if we have nil as our value.
    "if #{local_name} == #{default.inspect}\n" + 
      continue_string_structure(prior_segments) + 
    "\nelse\n" + # Otherwise, write the code up to here
      "#{interpolation_statement(prior_segments)}\nend"
  else
    interpolation_statement(prior_segments)
  end
end

#to_sObject



650
651
652
# File 'lib/action_controller/routing.rb', line 650

def to_s
  ":#{key}"
end

#value_checkObject



662
663
664
665
666
667
668
669
670
671
672
# File 'lib/action_controller/routing.rb', line 662

def value_check
  if default # Then we know it won't be nil
    "#{value_regexp.inspect} =~ #{local_name}" if regexp
  elsif optional?
    # If we have a regexp check that the value is not given, or that it matches.
    # If we have no regexp, return nil since we do not require a condition.
    "#{local_name}.nil? || #{value_regexp.inspect} =~ #{local_name}" if regexp
  else # Then it must be present, and if we have a regexp, it must match too.
    "#{local_name} #{"&& #{value_regexp.inspect} =~ #{local_name}" if regexp}"
  end
end

#value_regexpObject



702
703
704
# File 'lib/action_controller/routing.rb', line 702

def value_regexp
  Regexp.new "\\A#{regexp.source}\\Z" if regexp
end