Permalink
Browse files

Squashed commit of the following:

commit 9b76a2bde84e9366c93a32b703bb34d429b107e7
Author: Josh Hull <joshbuddy@gmail.com>
Date:   Wed Aug 3 09:03:28 2011 -0700

    nice inspection, to_s
  • Loading branch information...
1 parent 63ce0da commit bb74453f85791f5f6399ddd15ceb04b8da4ae308 @joshbuddy committed Aug 3, 2011
View
@@ -215,6 +215,15 @@ def no_response(env)
supported_methods.empty? ? @default_app.call(env) : [405, {'Allow' => supported_methods.sort.join(", ")}, []]
end
+ def to_s
+ "#<HttpRouter:0x#{object_id.to_s(16)} number of routes (#{routes.size}) ignore_trailing_slash? (#{ignore_trailing_slash?}) redirect_trailing_slash? (#{redirect_trailing_slash?}) known_methods (#{known_methods.to_a.join(', ')})>"
+ end
+
+ def inspect
+ head = to_s
+ "#{to_s}\n#{'=' * head.size}\n#{@root.inspect}"
+ end
+
private
def add_with_request_method(path, method, opts = {}, &app)
route = add(path, opts).send(method.to_sym)
View
@@ -64,6 +64,27 @@ def usable?(other)
false
end
+ def inspect
+ ins = "#{' ' * depth}#{inspect_label}"
+ body = inspect_matchers_body
+ unless body =~ /^\s*$/
+ ins << "\n" << body
+ end
+ ins
+ end
+
+ def inspect_label
+ "#{self.class.name.split("::").last} (#{@matchers.size} matchers)"
+ end
+
+ def inspect_matchers_body
+ @matchers.map{ |m| m.inspect}.join("\n")
+ end
+
+ def depth
+ @parent.send(:depth) + 1
+ end
+
private
def inject_root_methods(code = nil, &blk)
code ? root.methods_module.module_eval(code) : root.methods_module.module_eval(&blk)
@@ -14,6 +14,16 @@ def usable?(other)
other.class == self.class
end
+ def inspect_matchers_body
+ @map.map { |key, values|
+ ins = "#{' ' * depth}when #{key.inspect}:\n"
+ ins << values.map{|v| v.inspect}.join("\n") }.join("\n")
+ end
+
+ def inspect_label
+ "#{self.class.name}"
+ end
+
def to_code
lookup_ivar = inject_root_ivar(@map)
method_prefix = "lookup_#{root.next_counter} "
@@ -53,6 +53,10 @@ def usable?(other)
other == self
end
+ def inspect_label
+ "Path: #{original_path.inspect} for route #{route.named || 'unnamed route'} to #{route.dest.inspect}"
+ end
+
private
def raw_url(args, options)
raise InvalidRouteException
@@ -36,6 +36,10 @@ def to_code
code << "\n #{super}\nend"
end
+ def inspect_label
+ "#{self.class.name.split("::").last} #{opts.inspect} (#{@matchers.size} matchers)"
+ end
+
private
def to_code_condition(k, v)
case v
@@ -28,6 +28,14 @@ def inject_root_ivar(obj)
name
end
+ def depth
+ 0
+ end
+
+ def inspect_label
+ "Root (#{@matchers.size} matchers)"
+ end
+
private
def compile
root.extend(root.methods_module)
View
@@ -86,4 +86,14 @@ def test_public_interface
assert methods.include?(:rewrite_partial_path_info)
assert methods.include?(:rewrite_path_info)
end
+
+ def test_to_s_and_inspect
+ router = HttpRouter.new
+ router.add('/').to(:test)
+ router.add('/test').to(:test2)
+ router.post('/test').to(:test3)
+ assert router.to_s.match(/^#<HttpRouter:0x[0-9a-f]+ number of routes \(3\) ignore_trailing_slash\? \(true\) redirect_trailing_slash\? \(false\) known_methods \(POST\)>$/)
+ assert router.inspect.match(/^#<HttpRouter:0x[0-9a-f]+ number of routes \(3\) ignore_trailing_slash\? \(true\) redirect_trailing_slash\? \(false\) known_methods \(POST\)>/)
+ assert router.inspect.match(/Path: "\/test" for route unnamed route to :test3/)
+ end
end

0 comments on commit bb74453

Please sign in to comment.