Class: Ruil::PathInfoParser
- Inherits:
-
Object
- Object
- Ruil::PathInfoParser
- Defined in:
- lib/ruil/path_info_parser.rb
Overview
Each instance of PathInfoParser matches path info strings with a pattern and return variables matching it if the pattern is matched. Else, it returns false.
Usage
parser = PathInfoParser.new('/foo/:type/:id')
parser === '/foo/bar/1' # => {:type => 'bar', :id => '1'}
parser === '/foo/bar/3.js' # => {:type => 'bar', :id => '3'}
parser === '/foo' # => false
parser === '/bar' # => false
Instance Method Summary collapse
-
#===(path_info) ⇒ Hash, false
Match a path info.
-
#initialize(pattern) ⇒ PathInfoParser
constructor
Initialize a new parser.
Constructor Details
#initialize(pattern) ⇒ PathInfoParser
Initialize a new parser
20 21 22 23 24 25 |
# File 'lib/ruil/path_info_parser.rb', line 20 def initialize(pattern) @pattern = pattern.split('/').map do |s| ( s[0,1] == ':' ) ? eval(s) : s end @pattern = ['', ''] if @pattern.empty? end |
Instance Method Details
#===(path_info) ⇒ Hash, false
Match a path info.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ruil/path_info_parser.rb', line 32 def ===(path_info) s = path_info.split('/') s = ['', ''] if s.empty? s.last.gsub!(/\..*$/, '') return false unless s.size == @pattern.size matchs = {} s.each_index do |i| if Symbol === @pattern[i] matchs[@pattern[i]] = s[i] else return false unless @pattern[i] == s[i] end end matchs end |