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.



49
50
51
52
# File 'lib/perfectqueue/application/router.rb', line 49

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

Instance Attribute Details

#patternsObject (readonly)

Returns the value of attribute patterns.



80
81
82
# File 'lib/perfectqueue/application/router.rb', line 80

def patterns
  @patterns
end

Instance Method Details

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



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/perfectqueue/application/router.rb', line 54

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

  @patterns << [pattern, sym]
end

#route(type) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/perfectqueue/application/router.rb', line 67

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