Class | ActionController::Routing::DynamicSegment |
In: |
vendor/rails/actionpack/lib/action_controller/routing.rb
|
Parent: | Segment |
default | [RW] | |
key | [RW] | |
regexp | [RW] |
# File vendor/rails/actionpack/lib/action_controller/routing.rb, line 642 642: def initialize(key = nil, options = {}) 643: super() 644: self.key = key 645: self.default = options[:default] if options.key? :default 646: self.is_optional = true if options[:optional] || options.key?(:default) 647: end
# File vendor/rails/actionpack/lib/action_controller/routing.rb, line 708 708: def build_pattern(pattern) 709: chunk = regexp_chunk 710: chunk = "(#{chunk})" if Regexp.new(chunk).number_of_captures == 0 711: pattern = "#{chunk}#{pattern}" 712: optional? ? Regexp.optionalize(pattern) : pattern 713: end
# File vendor/rails/actionpack/lib/action_controller/routing.rb, line 672 672: def expiry_statement 673: "not_expired, hash = false, options if not_expired && expire_on[:#{key}]" 674: end
# File vendor/rails/actionpack/lib/action_controller/routing.rb, line 658 658: def extract_value 659: "#{local_name} = hash[:#{key}] #{"|| #{default.inspect}" if default}" 660: end
# File vendor/rails/actionpack/lib/action_controller/routing.rb, line 676 676: def extraction_code 677: s = extract_value 678: vc = value_check 679: s << "\nreturn [nil,nil] unless #{vc}" if vc 680: s << "\n#{expiry_statement}" 681: end
# File vendor/rails/actionpack/lib/action_controller/routing.rb, line 683 683: def interpolation_chunk 684: "\#{CGI.escape(#{local_name}.to_s)}" 685: end
The local variable name that the value of this segment will be extracted to.
# File vendor/rails/actionpack/lib/action_controller/routing.rb, line 654 654: def local_name 655: "#{key}_value" 656: end
# File vendor/rails/actionpack/lib/action_controller/routing.rb, line 714 714: def match_extraction(next_capture) 715: hangon = (default ? "|| #{default.inspect}" : "if match[#{next_capture}]") 716: 717: # All non code-related keys (such as :id, :slug) have to be unescaped as other CGI params 718: "params[:#{key}] = match[#{next_capture}] #{hangon}" 719: end
# File vendor/rails/actionpack/lib/action_controller/routing.rb, line 721 721: def optionality_implied? 722: [:action, :id].include? key 723: end
# File vendor/rails/actionpack/lib/action_controller/routing.rb, line 704 704: def regexp_chunk 705: regexp ? "(#{regexp.source})" : "([^#{Routing::SEPARATORS.join}]+)" 706: end
# File vendor/rails/actionpack/lib/action_controller/routing.rb, line 687 687: def string_structure(prior_segments) 688: if optional? # We have a conditional to do... 689: # If we should not appear in the url, just write the code for the prior 690: # segments. This occurs if our value is the default value, or, if we are 691: # optional, if we have nil as our value. 692: "if #{local_name} == #{default.inspect}\n" + 693: continue_string_structure(prior_segments) + 694: "\nelse\n" + # Otherwise, write the code up to here 695: "#{interpolation_statement(prior_segments)}\nend" 696: else 697: interpolation_statement(prior_segments) 698: end 699: end
# File vendor/rails/actionpack/lib/action_controller/routing.rb, line 649 649: def to_s 650: ":#{key}" 651: end
# File vendor/rails/actionpack/lib/action_controller/routing.rb, line 661 661: def value_check 662: if default # Then we know it won't be nil 663: "#{value_regexp.inspect} =~ #{local_name}" if regexp 664: elsif optional? 665: # If we have a regexp check that the value is not given, or that it matches. 666: # If we have no regexp, return nil since we do not require a condition. 667: "#{local_name}.nil? || #{value_regexp.inspect} =~ #{local_name}" if regexp 668: else # Then it must be present, and if we have a regexp, it must match too. 669: "#{local_name} #{"&& #{value_regexp.inspect} =~ #{local_name}" if regexp}" 670: end 671: end