Private utility methods used throughout Rack::Mount.
# File lib/rack/mount/utils.rb, line 25
25: def debug(msg)
26: warn "Rack::Mount #{msg}" if $DEBUG
27: end
# File lib/rack/mount/utils.rb, line 71
71: def escape_uri(uri)
72: Parser.escape(uri.to_s, UNSAFE_PCHAR)
73: end
# File lib/rack/mount/utils.rb, line 118
118: def normalize_extended_expression(regexp)
119: return regexp unless regexp.options & Regexp::EXTENDED != 0
120: source = regexp.source
121: source.gsub!(/#.+$/, '')
122: source.gsub!(/\s+/, '')
123: source.gsub!(/\\\//, '/')
124: Regexp.compile(source)
125: 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
38: def normalize_path(path)
39: path = "/#{path}"
40: path.squeeze!('/')
41: path.sub!(%{/+\Z}, '')
42: path = '/' if path == ''
43: path
44: end
# File lib/rack/mount/utils.rb, line 128
128: def parse_regexp(regexp)
129: cache = @@_parse_regexp_cache ||= {}
130:
131: if expression = cache[regexp]
132: return expression
133: end
134:
135: unless regexp.is_a?(RegexpWithNamedGroups)
136: regexp = RegexpWithNamedGroups.new(regexp)
137: end
138:
139: expression = Regin.parse(regexp)
140:
141: unless Regin.regexp_supports_named_captures?
142: tag_captures = Proc.new do |group|
143: case group
144: when Regin::Group
145: # TODO: dup instead of mutating
146: group.instance_variable_set('@name', regexp.names[group.index]) if group.index
147: tag_captures.call(group.expression)
148: when Regin::Expression
149: group.each { |child| tag_captures.call(child) }
150: end
151: end
152: tag_captures.call(expression)
153: end
154:
155: cache[regexp] = expression.freeze
156: expression
157: rescue Racc::ParseError, Regin::Parser::ScanError
158: []
159: 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
53: def pop_trailing_blanks!(ary)
54: while ary.length > 0 && (ary.last.nil? || ary.last == '')
55: ary.pop
56: end
57: ary
58: 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
113: def regexp_anchored?(regexp)
114: regexp.source =~ /\A(\\A|\^).*(\\Z|\$)\Z/ ? true : false
115: end
# File lib/rack/mount/utils.rb, line 17
17: def silence_debug
18: old_debug, $DEBUG = $DEBUG, nil
19: yield
20: ensure
21: $DEBUG = old_debug
22: end
# File lib/rack/mount/utils.rb, line 25
25: def debug(msg)
26: warn "Rack::Mount #{msg}" if $DEBUG
27: end
# File lib/rack/mount/utils.rb, line 71
71: def escape_uri(uri)
72: Parser.escape(uri.to_s, UNSAFE_PCHAR)
73: end
# File lib/rack/mount/utils.rb, line 118
118: def normalize_extended_expression(regexp)
119: return regexp unless regexp.options & Regexp::EXTENDED != 0
120: source = regexp.source
121: source.gsub!(/#.+$/, '')
122: source.gsub!(/\s+/, '')
123: source.gsub!(/\\\//, '/')
124: Regexp.compile(source)
125: 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
38: def normalize_path(path)
39: path = "/#{path}"
40: path.squeeze!('/')
41: path.sub!(%{/+\Z}, '')
42: path = '/' if path == ''
43: path
44: end
# File lib/rack/mount/utils.rb, line 128
128: def parse_regexp(regexp)
129: cache = @@_parse_regexp_cache ||= {}
130:
131: if expression = cache[regexp]
132: return expression
133: end
134:
135: unless regexp.is_a?(RegexpWithNamedGroups)
136: regexp = RegexpWithNamedGroups.new(regexp)
137: end
138:
139: expression = Regin.parse(regexp)
140:
141: unless Regin.regexp_supports_named_captures?
142: tag_captures = Proc.new do |group|
143: case group
144: when Regin::Group
145: # TODO: dup instead of mutating
146: group.instance_variable_set('@name', regexp.names[group.index]) if group.index
147: tag_captures.call(group.expression)
148: when Regin::Expression
149: group.each { |child| tag_captures.call(child) }
150: end
151: end
152: tag_captures.call(expression)
153: end
154:
155: cache[regexp] = expression.freeze
156: expression
157: rescue Racc::ParseError, Regin::Parser::ScanError
158: []
159: 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
53: def pop_trailing_blanks!(ary)
54: while ary.length > 0 && (ary.last.nil? || ary.last == '')
55: ary.pop
56: end
57: ary
58: 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
113: def regexp_anchored?(regexp)
114: regexp.source =~ /\A(\\A|\^).*(\\Z|\$)\Z/ ? true : false
115: end
# File lib/rack/mount/utils.rb, line 17
17: def silence_debug
18: old_debug, $DEBUG = $DEBUG, nil
19: yield
20: ensure
21: $DEBUG = old_debug
22: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.