Class: Fredo::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/fredo/registry.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.clearObject



19
20
21
# File 'lib/fredo/registry.rb', line 19

def self.clear
  @routes = {}
end

.compile(path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fredo/registry.rb', line 23

def self.compile(path)
  keys = []
  if path.respond_to? :to_str
    special_chars = %w{. + ( )}
    pattern =
      path.to_str.gsub(/((:\w+)|[\*#{special_chars.join}])/) do |match|
        case match
        when "*"
          keys << 'splat'
          "(.*?)"
        when *special_chars
          Regexp.escape(match)
        else
          keys << $2[1..-1]
          "([^/?&#]+)"
        end
      end
    [/^#{pattern}$/, keys]
  elsif path.respond_to?(:keys) && path.respond_to?(:match)
    [path, path.keys]
  elsif path.respond_to? :match
    [path, keys]
  else
    raise TypeError, path
  end
end

.route(verb, uri, options = {}, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/fredo/registry.rb', line 4

def self.route(verb, uri, options={}, &block)
  uri = URI.parse(uri)
  uri.path = '/' if uri.path.empty? 
  pattern, keys = compile(uri.path)

  @routes ||={}
  @routes[verb] ||= {}
  (@routes[verb][uri.host] ||= []).
    push([pattern, keys, options, block]).last
end

.routesObject



15
16
17
# File 'lib/fredo/registry.rb', line 15

def self.routes
  @routes
end