Class: Rack::Routing::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/route.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Route

Returns a new instance of Route.



4
5
6
# File 'lib/rack/route.rb', line 4

def initialize path
  @my_parts = path.split( '/' ).reject{| p | String.blank?( p )}.map( &:strip )
end

Instance Method Details

#match?(parts) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rack/route.rb', line 8

def match? parts
  return false unless parts.count == @my_parts.count

  # HEAD request matches #GET route
  if parts.first.to_s.upcase.to_sym == :HEAD
    parts[ 0 ] = :GET
  end
  
  indexes = ( 0..@my_parts.count - 1 ).to_a

  param_indexes.each{| i | indexes.delete i }

  indexes.all? do |i|
    parts[ i ].to_s == @my_parts[ i ]
  end
end

#param_indexesObject



43
44
45
# File 'lib/rack/route.rb', line 43

def param_indexes
  @my_parts.map.with_index{ | p,i | i if p[ 0 ] == ':' }.compact
end

#params_for(parts) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/rack/route.rb', line 25

def params_for parts
  return {} unless param_indexes

  arr = param_indexes.map{| i | [ @my_parts[ i ].sub( /\A:/, '' ).to_sym, parts[ i ]]}
  
  arr.to_h
end

#routing_methodObject



33
34
35
36
37
38
39
40
41
# File 'lib/rack/route.rb', line 33

def routing_method
  return "#{ @my_parts[ 0 ].downcase }_root".to_sym if @my_parts.count == 1

  @my_parts.reject{ |p| p[ 0 ] == ':' }
           .map( &:downcase )
           .map{| p | p.gsub( '-', '_' )}
           .join( '_' )
           .to_sym
end

#to_sObject



47
48
49
# File 'lib/rack/route.rb', line 47

def to_s
  "#{ self.class } @my_parts:#{ @my_parts } count:#{ @my_parts.count }"
end