Class: Amber::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/amber/route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRoute

Returns a new instance of Route.



4
5
6
# File 'lib/amber/route.rb', line 4

def initialize
  @root = Amber::RoutePath.new
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



2
3
4
# File 'lib/amber/route.rb', line 2

def root
  @root
end

Instance Method Details

#find(url) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/amber/route.rb', line 45

def find(url)
  layers = self.parse_url(url)
  current_layer = @root

  if layers.length > 0
    layers.delete ""
    layers.each do |layer|
      if current_layer.child.has_key? layer
        current_layer = current_layer.child[layer]
      else
        return nil
      end
    end
  end

  current_layer
end

#map(url, method = nil, &callback) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/amber/route.rb', line 8

def map(url, method = nil, &callback)
  layers = self.parse_url(url)

  if layers.length > 0
    layers.delete ""
    current_layer = @root

    layers.each do |layer|
      if layer == layers.last
        if current_layer.child.has_key? layer
          path = current_layer.child[layer]
          path.callback = callback
          path.method = method if method
          next
        end
        
        new_path = Amber::RoutePath.new
        new_path.callback = callback
        new_path.method = method if method

        current_layer.add_child(layer, new_path)
      else
        if current_layer.child.has_key? layer
          current_layer = current_layer.child[layer]
          next
        end

        new_path = Amber::RoutePath.new
        current_layer.add_child(layer, new_path)
        current_layer = new_path
      end
    end
  else
    @root.callback = callback
  end
end

#parse_url(url) ⇒ Object



63
64
65
# File 'lib/amber/route.rb', line 63

def parse_url(url)
  url.split('/')
end