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.



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

def initialize
  @routes = []
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



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

def routes
  @routes
end

Instance Method Details

#<<(file) ⇒ Object



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

def <<(file)
  files << file
end

#blocksObject



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

def blocks
  @blocks ||= []
end

#configure(&block) ⇒ Object



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

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

#filesObject



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

def files
  @files ||= []
end

#files=(files) ⇒ Object



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

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

#load(file) ⇒ Object



46
47
48
49
# File 'lib/msgr/routes.rb', line 46

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

#reloadObject



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

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

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



51
52
53
54
55
56
57
58
59
60
# File 'lib/msgr/routes.rb', line 51

def route(key, opts = {})
  routes.each do |route|
    if route.accept? key, opts
      route.add key
      return
    end
  end

  routes << Msgr::Route.new(key, opts)
end