Class: Msgr::Routes

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/msgr/routes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log, #log_name

Constructor Details

#initializeRoutes

Returns a new instance of Routes


9
10
11
# File 'lib/msgr/routes.rb', line 9

def initialize
  @routes = []
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes


6
7
8
# File 'lib/msgr/routes.rb', line 6

def routes
  @routes
end

Instance Method Details

#<<(file) ⇒ Object


30
31
32
# File 'lib/msgr/routes.rb', line 30

def <<(file)
  files << file
end

#blocksObject


22
23
24
# File 'lib/msgr/routes.rb', line 22

def blocks
  @blocks ||= []
end

#configure(&block) ⇒ Object


13
14
15
16
# File 'lib/msgr/routes.rb', line 13

def configure(&block)
  blocks << block
  instance_eval(&block)
end

#filesObject


18
19
20
# File 'lib/msgr/routes.rb', line 18

def files
  @files ||= []
end

#files=(files) ⇒ Object


26
27
28
# File 'lib/msgr/routes.rb', line 26

def files=(files)
  @files = Array files
end

#load(file) ⇒ Object


47
48
49
50
51
52
53
# File 'lib/msgr/routes.rb', line 47

def load(file)
  unless File.exist?(file)
    raise ArgumentError.new "File `#{file}` does not exists."
  end

  instance_eval File.read file
end

#reloadObject


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/msgr/routes.rb', line 34

def reload
  routes.clear
  blocks.each {|block| instance_eval(&block) }
  files.uniq!
  files.each do |file|
    if File.exist? file
      load file
    else
      log(:warn) { "Routes file `#{file}` does not exists (anymore)." }
    end
  end
end

#route(key, opts = {}) ⇒ Object


55
56
57
58
59
60
61
# File 'lib/msgr/routes.rb', line 55

def route(key, opts = {})
  if (route = routes.find {|r| r.accept?(key, opts) })
    route.add key
  else
    routes << Msgr::Route.new(key, opts)
  end
end