Class: PerfectQueue::Application::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/perfectqueue/application/router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



23
24
25
26
# File 'lib/perfectqueue/application/router.rb', line 23

def initialize
  @patterns = []
  @cache = {}
end

Instance Attribute Details

#patternsObject (readonly)

Returns the value of attribute patterns.



54
55
56
# File 'lib/perfectqueue/application/router.rb', line 54

def patterns
  @patterns
end

Instance Method Details

#add(pattern, sym, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/perfectqueue/application/router.rb', line 28

def add(pattern, sym, options={})
  case pattern
  when Regexp
    # ok
  when String, Symbol
    pattern = /#{Regexp.escape(pattern)}/
  else
    raise ArguementError, "pattern should be String or Regexp but got #{pattern.class}: #{pattern.inspect}"
  end

  @patterns << [pattern, sym]
end

#route(type) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/perfectqueue/application/router.rb', line 41

def route(type)
  if @cache.has_key?(type)
    return @cache[type]
  end

  @patterns.each {|(pattern,sym)|
    if pattern.match(type)
      base = resolve_application_base(sym)
      return @cache[type] = base
    end
  }
  return @cache[type] = nil
end