Class: 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.



2
3
4
# File 'lib/rack/route.rb', line 2

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

Instance Method Details

#match?(parts) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
15
# File 'lib/rack/route.rb', line 6

def match? parts
  return false unless parts.count == @my_parts.count
  
  indexes = ( 0..@my_parts.count - 1 ).to_a
  indexes.delete param_index

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

#param_indexObject



29
30
31
# File 'lib/rack/route.rb', line 29

def param_index
  @my_parts.index{ |p| p[ 0 ] == ':' }
end

#params_for(parts) ⇒ Object



17
18
19
20
21
# File 'lib/rack/route.rb', line 17

def params_for parts
  return {} unless param_index

  { @my_parts[ param_index ].sub( /\A:/, '' ).to_sym => parts[ param_index ]}
end

#routing_methodObject



23
24
25
26
27
# File 'lib/rack/route.rb', line 23

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

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

#to_sObject



33
34
35
# File 'lib/rack/route.rb', line 33

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