Class: ActionController::Routing::DynamicSegment

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

Overview

:nodoc:

Direct Known Subclasses

ControllerSegment, PathSegment

Constant Summary

Constants inherited from Segment

Segment::RESERVED_PCHAR, Segment::UNSAFE_PCHAR

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.



114
115
116
117
118
119
# File 'lib/action_controller/routing/segments.rb', line 114

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.



112
113
114
# File 'lib/action_controller/routing/segments.rb', line 112

def default
  @default
end

#keyObject

Returns the value of attribute key.



112
113
114
# File 'lib/action_controller/routing/segments.rb', line 112

def key
  @key
end

#regexpObject

Returns the value of attribute regexp.



112
113
114
# File 'lib/action_controller/routing/segments.rb', line 112

def regexp
  @regexp
end

Instance Method Details

#build_pattern(pattern) ⇒ Object



189
190
191
192
193
194
# File 'lib/action_controller/routing/segments.rb', line 189

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



144
145
146
# File 'lib/action_controller/routing/segments.rb', line 144

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

#extract_valueObject



130
131
132
# File 'lib/action_controller/routing/segments.rb', line 130

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

#extraction_codeObject



148
149
150
151
152
153
# File 'lib/action_controller/routing/segments.rb', line 148

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

#interpolation_chunk(value_code = "#{local_name}") ⇒ Object



155
156
157
# File 'lib/action_controller/routing/segments.rb', line 155

def interpolation_chunk(value_code = "#{local_name}")
  "\#{URI.escape(#{value_code}.to_s, ActionController::Routing::Segment::UNSAFE_PCHAR)}"
end

#local_nameObject

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



126
127
128
# File 'lib/action_controller/routing/segments.rb', line 126

def local_name
  "#{key}_value"
end

#match_extraction(next_capture) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/action_controller/routing/segments.rb', line 196

def match_extraction(next_capture)
  # All non code-related keys (such as :id, :slug) are URI-unescaped as
  # path parameters.
  default_value = default ? default.inspect : nil
  %[
    value = if (m = match[#{next_capture}])
      URI.unescape(m)
    else
      #{default_value}
    end
    params[:#{key}] = value if value
  ]
end

#optionality_implied?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/action_controller/routing/segments.rb', line 210

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

#regexp_chunkObject



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/action_controller/routing/segments.rb', line 177

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

#regexp_has_modifiers?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/action_controller/routing/segments.rb', line 214

def regexp_has_modifiers?
  regexp.options & (Regexp::IGNORECASE | Regexp::EXTENDED) != 0
end

#string_structure(prior_segments) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/action_controller/routing/segments.rb', line 159

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



121
122
123
# File 'lib/action_controller/routing/segments.rb', line 121

def to_s
  ":#{key}"
end

#value_checkObject



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/action_controller/routing/segments.rb', line 133

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



173
174
175
# File 'lib/action_controller/routing/segments.rb', line 173

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