Class: ActionController::Routing::Segment

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

Overview

:nodoc:

Direct Known Subclasses

DynamicSegment, StaticSegment

Constant Summary collapse

RESERVED_PCHAR =
':@&=+$,;'
SAFE_PCHAR =
"#{URI::REGEXP::PATTERN::UNRESERVED}#{RESERVED_PCHAR}"
UNSAFE_PCHAR =
Regexp.new("[^#{SAFE_PCHAR}]", false, 'N').freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSegment

Returns a new instance of Segment.



12
13
14
# File 'lib/action_controller/routing/segments.rb', line 12

def initialize
  @is_optional = false
end

Instance Attribute Details

#is_optionalObject Also known as: optional?

TODO: Convert :is_optional accessor to read only



9
10
11
# File 'lib/action_controller/routing/segments.rb', line 9

def is_optional
  @is_optional
end

Instance Method Details

#all_optionals_available_condition(prior_segments) ⇒ Object

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.



51
52
53
54
# File 'lib/action_controller/routing/segments.rb', line 51

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

#continue_string_structure(prior_segments) ⇒ Object

Continue generating string for the prior segments.



25
26
27
28
29
30
31
32
# File 'lib/action_controller/routing/segments.rb', line 25

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

#extraction_codeObject



20
21
22
# File 'lib/action_controller/routing/segments.rb', line 20

def extraction_code
  nil
end

#interpolation_chunkObject



34
35
36
# File 'lib/action_controller/routing/segments.rb', line 34

def interpolation_chunk
  URI.escape(value, UNSAFE_PCHAR)
end

#interpolation_statement(prior_segments) ⇒ Object

Return a string interpolation statement for this segment and those before it.



39
40
41
42
43
# File 'lib/action_controller/routing/segments.rb', line 39

def interpolation_statement(prior_segments)
  chunks = prior_segments.collect { |s| s.interpolation_chunk }
  chunks << interpolation_chunk
  "\"#{chunks * ''}\"#{all_optionals_available_condition(prior_segments)}"
end

#match_extraction(next_capture) ⇒ Object

Recognition



58
59
60
# File 'lib/action_controller/routing/segments.rb', line 58

def match_extraction(next_capture)
  nil
end

#number_of_capturesObject



16
17
18
# File 'lib/action_controller/routing/segments.rb', line 16

def number_of_captures
  Regexp.new(regexp_chunk).number_of_captures
end

#optionality_implied?Boolean

Returns true if this segment is optional? because of a default. If so, then no warning will be emitted regarding this segment.

Returns:

  • (Boolean)


66
67
68
# File 'lib/action_controller/routing/segments.rb', line 66

def optionality_implied?
  false
end

#string_structure(prior_segments) ⇒ Object



45
46
47
# File 'lib/action_controller/routing/segments.rb', line 45

def string_structure(prior_segments)
  optional? ? continue_string_structure(prior_segments) : interpolation_statement(prior_segments)
end