Class: Merb::Router::Route

Inherits:
Object show all
Defined in:
lib/merb-core/dispatch/router/route.rb

Overview

This entire class is private and should never be accessed outside of Merb::Router and Behavior

Defined Under Namespace

Classes: Generator

Constant Summary collapse

SEGMENT_REGEXP =

:nodoc:

/(:([a-z](_?[a-z0-9])*))/
OPTIONAL_SEGMENT_REGEX =
/^.*?([\(\)])/i
SEGMENT_REGEXP_WITH_BRACKETS =
/(:[a-z_]+)(\[(\d+)\])?/
JUST_BRACKETS =
/\[(\d+)\]/
SEGMENT_CHARACTERS =
"[^\/.,;?]".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conditions, params, deferred_procs, options = {}) ⇒ Route

Returns a new instance of Route.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/merb-core/dispatch/router/route.rb', line 17

def initialize(conditions, params, deferred_procs, options = {})
  @conditions, @params = conditions, params

  if options[:redirects]
    @redirects         = true
    @redirect_status   = @params[:status]
    @redirect_url      = @params[:url]
    @defaults          = {}
  else
    @defaults          = options[:defaults] || {}
  end
  
  # @conditional_block = conditional_block

  @identifiers       = options[:identifiers]
  @deferred_procs    = deferred_procs
  @segments          = []
  @symbol_conditions = {}
  @placeholders      = {}
  compile
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



13
14
15
# File 'lib/merb-core/dispatch/router/route.rb', line 13

def conditions
  @conditions
end

#fixationObject

Returns the value of attribute fixation.



15
16
17
# File 'lib/merb-core/dispatch/router/route.rb', line 15

def fixation
  @fixation
end

#indexObject (readonly)

Returns the value of attribute index.



14
15
16
# File 'lib/merb-core/dispatch/router/route.rb', line 14

def index
  @index
end

#nameObject

Returns the value of attribute name.



14
15
16
# File 'lib/merb-core/dispatch/router/route.rb', line 14

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



13
14
15
# File 'lib/merb-core/dispatch/router/route.rb', line 13

def params
  @params
end

#segmentsObject (readonly)

Returns the value of attribute segments.



13
14
15
# File 'lib/merb-core/dispatch/router/route.rb', line 13

def segments
  @segments
end

#variablesObject (readonly)

Returns the value of attribute variables.



14
15
16
# File 'lib/merb-core/dispatch/router/route.rb', line 14

def variables
  @variables
end

Instance Method Details

#allow_fixation?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/merb-core/dispatch/router/route.rb', line 43

def allow_fixation?
  @fixation
end

#compiled_statement(first) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/merb-core/dispatch/router/route.rb', line 97

def compiled_statement(first)
  els_if = first ? '  if ' : '  elsif '

  code = ""
  code << els_if << condition_statements.join(" && ") << "\n"
  
  # First, we need to always return the value of the
  # deferred block if it explicitly matched the route
  if @redirects && @deferred_procs.any?
    code << "    return [#{@index.inspect}, block_result] if request.matched?" << "\n" 
    code << "    request.redirects!" << "\n"
    code << "    [#{@index.inspect}, { :url => #{@redirect_url.inspect}, :status => #{@redirect_status.inspect} }]" << "\n"
  elsif @redirects
    code << "    request.redirects!" << "\n"
    code << "    [#{@index.inspect}, { :url => #{@redirect_url.inspect}, :status => #{@redirect_status.inspect} }]" << "\n"
  elsif @deferred_procs.any?
    code << "    [#{@index.inspect}, block_result]" << "\n"
  else
    code << "    [#{@index.inspect}, #{params_as_string}]" << "\n"
  end
end

#generate(args = [], defaults = {}) ⇒ Object

Compiled method ===

Raises:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/merb-core/dispatch/router/route.rb', line 75

def generate(args = [], defaults = {})
  raise GenerationError, "Cannot generate regexp Routes" if regexp?
  
  params = extract_options_from_args!(args) || { }
  
  # Support for anonymous params
  unless args.empty?
    # First, let's determine which variables are missing
    variables = @variables - params.keys
    
    raise GenerationError, "The route has #{@variables.length} variables: #{@variables.inspect}" if args.length > variables.length
    
    args.each_with_index do |param, i|
      params[variables[i]] ||= param
    end
  end
  
  uri = @generator[params, defaults] or raise GenerationError, "Named route #{name} could not be generated with #{params.inspect}"
  uri = Merb::Config[:path_prefix] + uri if Merb::Config[:path_prefix]
  uri
end

#regexp?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/merb-core/dispatch/router/route.rb', line 39

def regexp?
  @regexp
end

#registerObject



55
56
57
58
59
# File 'lib/merb-core/dispatch/router/route.rb', line 55

def register
  @index = Merb::Router.routes.size
  Merb::Router.routes << self
  self
end

#resource=(key) ⇒ Object

Sets the route as a resource route with the given key as the lookup key.



63
64
65
66
# File 'lib/merb-core/dispatch/router/route.rb', line 63

def resource=(key)
  Router.resource_routes[key] = self
  key
end

#to_sObject Also known as: inspect



47
48
49
50
51
# File 'lib/merb-core/dispatch/router/route.rb', line 47

def to_s
  regexp? ?
    "/#{conditions[:path].source}/" :
    segment_level_to_s(segments)
end