lib/usher/splitter.rb in joshbuddy-usher-0.3.6 vs lib/usher/splitter.rb in joshbuddy-usher-0.4.0

- old
+ new

@@ -6,11 +6,11 @@ def self.for_delimiters(delimiters) delimiters_regex = delimiters.collect{|d| Regexp.quote(d)} * '|' SplitterInstance.new( delimiters, - Regexp.new('((:|\*)?[0-9A-Za-z\$\-_\+!\*\',]+|' + delimiters_regex + '|\(|\)|\|)'), + Regexp.new('((:|\*)?[0-9A-Za-z\$\-_\+!\*\',]+|' + delimiters_regex + '|\(|\)|\||\{)'), Regexp.new(delimiters_regex + '|[0-9A-Za-z\$\-_\+!\*\',]+') ) end attr_reader :paths @@ -38,20 +38,44 @@ end end if path && !path.empty? parts end - def split(path, requirements = {}, transformers = {}) + def split(path, requirements = nil, transformers = nil) parts = Group.new(:all, nil) ss = StringScanner.new(path) current_group = parts while !ss.eos? part = ss.scan(@split_regex) case part[0] when ?*, ?: - type = (part[1] == ?: ? part.slice!(0,2) : part.slice!(0).chr).to_sym - current_group << Usher::Route::Variable.new(type, part, :validator => requirements[part.to_sym], :transformer => transformers[part.to_sym]) + type = part.slice!(0).chr.to_sym + current_group << Usher::Route::Variable.new(type, part, requirements && requirements[part.to_sym], transformers && transformers[part.to_sym]) + when ?{ + pattern = '' + count = 1 + variable = ss.scan(/:([^,]+),/) + until count.zero? + regex_part = ss.scan(/\{|\}|[^\{\}]+/) + case regex_part[0] + when ?{ + count += 1 + when ?} + count -= 1 + end + pattern << regex_part + end + pattern.slice!(pattern.size - 1) + regex = Regexp.new(pattern) + if variable + variable_type = variable.slice!(0).chr.to_sym + variable_name = variable[0, variable.size - 1].to_sym + current_group << Usher::Route::Variable.new(variable_type, variable_name, requirements && requirements[variable_name], transformers && transformers[variable_name], regex) + else + current_group << regex + end + when ?( new_group = Group.new(:any, current_group) current_group << new_group current_group = new_group when ?) @@ -73,23 +97,19 @@ current_group << part end end unless !path || path.empty? paths = calc_paths(parts) paths.each do |path| - last_delimiter = nil - last_variable = nil - path.each do |part| - case part - when Symbol - last_delimiter = part - when Usher::Route::Variable - if last_variable - last_variable.look_ahead = last_delimiter || @delimiters.first.to_sym + path.each_with_index do |part, index| + if part.is_a?(Usher::Route::Variable) + case part.type + when :* + part.look_ahead = path[index + 1, path.size].find{|p| !p.is_a?(Symbol) && !p.is_a?(Usher::Route::Variable)} || nil + when :':' + part.look_ahead = path[index + 1, path.size].find{|p| p.is_a?(Symbol)} || @delimiters.first.to_sym end - last_variable = part end end - last_variable.look_ahead = last_delimiter || @delimiters.first.to_sym if last_variable end paths end private \ No newline at end of file