Module: Rack::Mount::Generation::Route

Included in:
Route
Defined in:
lib/rack/mount/generation/route.rb

Overview

:nodoc:

Defined Under Namespace

Classes: DynamicSegment

Instance Method Summary collapse

Instance Method Details

#initialize(*args) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/rack/mount/generation/route.rb', line 17

def initialize(*args)
  super

  @segments = segments(@path).freeze
  @required_params = @segments.find_all { |s|
    s.is_a?(DynamicSegment)
  }.map { |s| s.name }.freeze
end

#url_for(params = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack/mount/generation/route.rb', line 26

def url_for(params = {})
  params = (params || {}).dup

  return nil if @segments.empty?
  return nil unless @required_params.all? { |p| params.include?(p) }

  path = generate_from_segments(@segments, params, @defaults)

  @defaults.each do |key, value|
    params.delete(key)
  end

  if params.any?
    path << "?#{Rack::Utils.build_query(params)}"
  end

  path
end