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.



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

def initialize
  @routes = []
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



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

def routes
  @routes
end

Instance Method Details

#<<(file) ⇒ Object



32
33
34
# File 'lib/msgr/routes.rb', line 32

def <<(file)
  files << file
end

#blocksObject



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

def blocks
  @blocks ||= []
end

#configure(&block) ⇒ Object



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

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

#filesObject



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

def files
  @files ||= []
end

#files=(files) ⇒ Object



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

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

#load(file) ⇒ Object



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

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

  instance_eval File.read file
end

#reloadObject



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

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



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

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