Private utility methods used throughout Rack::Mount.
# File lib/rack/mount/utils.rb, line 25 def debug(msg) warn "Rack::Mount #{msg}" if $DEBUG end
# File lib/rack/mount/utils.rb, line 71 def escape_uri(uri) Parser.escape(uri.to_s, UNSAFE_PCHAR) end
# File lib/rack/mount/utils.rb, line 118 def normalize_extended_expression(regexp) return regexp if (regexp.options & Regexp::EXTENDED) == 0 source = regexp.source source.gsub!(/#.+$/, '') source.gsub!(/\s+/, '') source.gsub!(/\\\//, '/') Regexp.compile(source) end
Normalizes URI path.
Strips off trailing slash and ensures there is a leading slash.
normalize_path("/foo") # => "/foo" normalize_path("/foo/") # => "/foo" normalize_path("foo") # => "/foo" normalize_path("") # => "/"
# File lib/rack/mount/utils.rb, line 38 def normalize_path(path) path = "/#{path}" path.squeeze!('/') path.sub!(%{/+\Z}, '') path = '/' if path == '' path end
# File lib/rack/mount/utils.rb, line 128 def parse_regexp(regexp) cache = @@_parse_regexp_cache ||= {} if expression = cache[regexp] return expression end unless regexp.is_a?(RegexpWithNamedGroups) regexp = RegexpWithNamedGroups.new(regexp) end expression = Regin.parse(regexp) unless Regin.regexp_supports_named_captures? tag_captures = Proc.new do |group| case group when Regin::Group # TODO: dup instead of mutating group.instance_variable_set('@name', regexp.names[group.index]) if group.index tag_captures.call(group.expression) when Regin::Expression group.each { |child| tag_captures.call(child) } end end tag_captures.call(expression) end cache[regexp] = expression.freeze expression rescue Racc::ParseError, Regin::Parser::ScanError [] end
Removes trailing nils from array.
pop_trailing_blanks!([1, 2, 3]) # => [1, 2, 3] pop_trailing_blanks!([1, 2, 3, nil, ""]) # => [1, 2, 3] pop_trailing_blanks!([nil]) # => [] pop_trailing_blanks!([""]) # => []
# File lib/rack/mount/utils.rb, line 53 def pop_trailing_blanks!(ary) while ary.length > 0 && (ary.last.nil? || ary.last == '') ary.pop end ary end
Determines whether the regexp must match the entire string.
regexp_anchored?(/^foo$/) # => true regexp_anchored?(/foo/) # => false regexp_anchored?(/^foo/) # => false regexp_anchored?(/foo$/) # => false
# File lib/rack/mount/utils.rb, line 113 def regexp_anchored?(regexp) regexp.source =~ /\A(\\A|\^).*(\\Z|\$)\Z/ ? true : false end
Generated with the Darkfish Rdoc Generator 2.