Rails源代码分析(35):ActionController::Routing(6) Segment
来源:互联网 发布:如何绕开公司网络限制 编辑:程序博客网 时间:2024/04/28 13:40
1 Segment的分类
- Segment. Abstract Base Class
DynamicSegment
. This represents parts of the route that begin with a colon, like:action
,:permalink
or:id
.ControllerSegment
. This is actually a subclass ofDynamicSegment
. It represents to special string:controller
, because it does some special recognition on those strings. (We’ll cover that more in the next article).PathSegment
. This is for segments that start with an asterisk, and which represent the remainder of the path. Routes like"/file/*path"
use aPathSegment
.StaticSegment
. This is any static text in your route that must be matched (or generated) verbatim. If you have a path like"/one/two"
, the strings"one"
and"two"
are both static segments.DividerSegment
. This is any segment that is used to delimit the other segments. Generally, this will be the forward slash character, but also includes commas, periods, semicolons, and question marks.
2 实现
先看看Segment实现
- class Segment #:nodoc:
- RESERVED_PCHAR = ':@&=+$,;'
- UNSAFE_PCHAR = Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}#{RESERVED_PCHAR}]", false, 'N').freeze
- attr_accessor :is_optional
- alias_method :optional?, :is_optional
- def initialize
- self.is_optional = false
- end
- def extraction_code
- nil
- end
- # Continue generating string for the prior segments.
- def continue_string_structure(prior_segments)
- if prior_segments.empty?
- interpolation_statement(prior_segments)
- else
- new_priors = prior_segments[0..-2]
- prior_segments.last.string_structure(new_priors)
- end
- end
- def interpolation_chunk
- URI.escape(value, UNSAFE_PCHAR)
- end
- # Return a string interpolation statement for this segment and those before it.
- def interpolation_statement(prior_segments)
- chunks = prior_segments.collect { |s| s.interpolation_chunk }
- chunks << interpolation_chunk
- "/"#{chunks * ''}/"#{all_optionals_available_condition(prior_segments)}"
- end
- def string_structure(prior_segments)
- optional? ? continue_string_structure(prior_segments) : interpolation_statement(prior_segments)
- end
- # Return an if condition that is true if all the prior segments can be generated.
- # If there are no optional segments before this one, then nil is returned.
- def all_optionals_available_condition(prior_segments)
- optional_locals = prior_segments.collect { |s| s.local_name if s.optional? && s.respond_to?(:local_name) }.compact
- optional_locals.empty? ? nil : " if #{optional_locals * ' && '}"
- end
- # Recognition
- def match_extraction(next_capture)
- nil
- end
- # Warning
- # Returns true if this segment is optional? because of a default. If so, then
- # no warning will be emitted regarding this segment.
- def optionality_implied?
- false
- end
- end
- Rails源代码分析(35):ActionController::Routing(6) Segment
- Rails源代码分析(30):ActionController::Routing(1)
- Rails源代码分析(31):ActionController::Routing(2) PolymorphicRoutes
- Rails源代码分析(32):ActionController::Routing(3) RouteSet
- Rails源代码分析(33):ActionController::Routing(4) Builder
- Rails源代码分析(34):ActionController::Routing(5) Route
- Rails源代码分析(36):ActionController::Routing(7) Optimisation
- Rails源代码分析(37):ActionController::Routing(8) Recognition Optimisation
- Rails源代码分析(38):ActionController::Routing(9) 总结
- Rails源代码分析(6):ActionController::Flash
- Rails源代码分析(22):ActionController::Caching(6) Sweeping
- Rails源代码分析(5):ActionController概览
- Rails源代码分析(12):ActionController::Benchmarking
- Rails源代码分析(13):ActionController::Rescue
- Rails源代码分析(14):ActionController::MimeResponds
- Rails源代码分析(15):ActionController::Helpers
- Rails源代码分析(16):ActionController::Cookies
- Rails源代码分析(23):ActionController::Verification
- 推广方法 ==一个思路!
- JAVA16进制字符串转字符或数字
- SQL SERVER DateDiff方法的用法
- hibernate显示SQL时出现问号解决方案
- 小语种培训大有可为
- Rails源代码分析(35):ActionController::Routing(6) Segment
- 用PowerDesigner生成Hibernate映射文件的步骤
- C#模拟提交表单 post-带附件的表单[multipart/form-data]
- Autodesk3400万美元收购ALGOR
- ext 2.0 核心API详解
- 使用PowerDesigner 的Hibernate O/R Mapping功能简介
- 安全编程法则
- sql处理半角引号
- 数据库设计的5种常见关系