Class: Fastr::Router

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/fastr/router.rb

Defined Under Namespace

Modules: Handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

included

Constructor Details

#initialize(app) ⇒ Router

Returns a new instance of Router.



7
8
9
10
11
12
# File 'lib/fastr/router.rb', line 7

def initialize(app)
  @app = app
  self.routes = []
  self.route_file = "#{@app.app_path}/app/config/routes.rb"
  setup_watcher
end

Instance Attribute Details

#route_fileObject

Returns the value of attribute route_file.



5
6
7
# File 'lib/fastr/router.rb', line 5

def route_file
  @route_file
end

#routesObject

Returns the value of attribute routes.



5
6
7
# File 'lib/fastr/router.rb', line 5

def routes
  @routes
end

Instance Method Details

#draw(&block) ⇒ Object



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

def draw(&block)
  block.call(self)
end

#file_modifiedObject



58
59
60
# File 'lib/fastr/router.rb', line 58

def file_modified
  puts "changed!"
end

#for(path, *args) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/fastr/router.rb', line 44

def for(path, *args)
  arg = args[0]
  log.debug "Adding route, path: #{path}, args: #{args.inspect}"

  match = get_regex_for_route(path, arg)
  hash = get_to_hash(arg)
  
  self.routes.push({:regex => match[:regex], :args => arg, :vars => match[:vars], :hash => hash})
end

#loadObject



36
37
38
39
40
41
42
# File 'lib/fastr/router.rb', line 36

def load
  log.debug "Loading routes from: #{self.route_file}"
  self.routes = []
  
  file = File.open(self.route_file)
  @app.instance_eval(file.read)
end

#match(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fastr/router.rb', line 14

def match(env)
  self.routes.each do |info|
    match = env['PATH_INFO'].match(info[:regex])

    # See if a route matches
    if not match.nil?

      # Map any parameters in our matched string
      vars = {}
      
      info[:vars].each_index do |i|
        var = info[:vars][i]
        vars[var] = match[i+1]
      end

      return {:ok => vars.merge!(info[:hash]) }
    end
  end
  
  {:error => :not_found}
end