Skip to content

Commit

Permalink
move counter to root
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy committed Jul 23, 2011
1 parent ee60fb5 commit 8dafa01
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
6 changes: 0 additions & 6 deletions lib/http_router.rb
Expand Up @@ -40,7 +40,6 @@ def initialize(*args, &blk)
@ignore_trailing_slash = options && options.key?(:ignore_trailing_slash) ? options[:ignore_trailing_slash] : true
@redirect_trailing_slash = options && options.key?(:redirect_trailing_slash) ? options[:redirect_trailing_slash] : false
@known_methods = Set.new(options && options[:known_methods] || [])
@counter = 0
reset!
instance_eval(&blk) if blk
end
Expand All @@ -63,7 +62,6 @@ def initialize(*args, &blk)
def add(path, opts = {}, &app)
route = add_route((Regexp === path ? RegexRoute : Route).new(self, path, opts))
route.to(app) if app
root.uncompile
route
end

Expand Down Expand Up @@ -201,10 +199,6 @@ def clone(klass = self.class)
cloned_router
end

def next_counter
@counter += 1
end

private
def no_response(env, perform_call = true)
supported_methods = @known_methods.select do |m|
Expand Down
2 changes: 1 addition & 1 deletion lib/http_router/node/arbitrary.rb
Expand Up @@ -13,7 +13,7 @@ def usable?(other)
end

def to_code
b, method_name = @blk, :"blk_#{router.next_counter}"
b, method_name = @blk, :"blk_#{root.next_counter}"
inject_root_methods { define_method(method_name) { b } }
"#{"if request.path_finished?" unless @allow_partial}
request.continue = proc { |state|
Expand Down
4 changes: 2 additions & 2 deletions lib/http_router/node/lookup.rb
Expand Up @@ -15,9 +15,9 @@ def usable?(other)
end

def to_code
lookup_ivar = :"@lookup_#{router.next_counter}"
lookup_ivar = :"@lookup_#{root.next_counter}"
inject_root_ivar(lookup_ivar, @map)
method_prefix = "lookup_#{router.next_counter} "
method_prefix = "lookup_#{root.next_counter} "
inject_root_methods @map.keys.map {|k|
method = :"#{method_prefix}#{k}"
"define_method(#{method.inspect}) do |request|
Expand Down
3 changes: 2 additions & 1 deletion lib/http_router/node/path.rb
Expand Up @@ -8,6 +8,7 @@ def initialize(router, parent, route, path, param_names = [])
raise AmbiguousVariableException, "You have duplicate variable name present: #{param_names.join(', ')}" if param_names.uniq.size != param_names.size
process_path_for_generation(@original_path) if @original_path.respond_to?(:split)
super router, parent
root.uncompile
end

def hashify_params(params)
Expand All @@ -22,7 +23,7 @@ def url(args, options)
end

def to_code
path_ivar = :"@path_#{router.next_counter}"
path_ivar = :"@path_#{root.next_counter}"
inject_root_ivar(path_ivar, self)
"#{"if request.path_finished?" unless route.match_partially?}
catch(:pass) do
Expand Down
6 changes: 5 additions & 1 deletion lib/http_router/node/root.rb
Expand Up @@ -5,7 +5,7 @@ class Root < Node
alias_method :compiled?, :compiled
def initialize(router)
super(router, nil)
@methods_module = Module.new
@counter, @methods_module = 0, Module.new
end

def [](request)
Expand All @@ -18,6 +18,10 @@ def uncompile
instance_eval "undef :[]; alias :[] :compiling_lookup", __FILE__, __LINE__ if compiled?
end

def next_counter
@counter += 1
end

private
def compile
root.extend(root.methods_module)
Expand Down

0 comments on commit 8dafa01

Please sign in to comment.