Module: Balotelli::Core::Router

Included in:
Base, Module::Base
Defined in:
lib/balotelli/core/router.rb

Instance Method Summary collapse

Instance Method Details

#match(str, priv = false) ⇒ Object



39
40
41
42
43
# File 'lib/balotelli/core/router.rb', line 39

def match(str, priv = false)
  if (m = @routes.detect { |k, v| match?(str, k) && v[priv] })
    [m[0], m[1][priv], str]
  end
end

#match?(str, route) ⇒ Boolean



28
29
30
31
32
33
34
35
36
37
# File 'lib/balotelli/core/router.rb', line 28

def match?(str, route)
  case route
  when Regexp
    str =~ route
  when String
    str == route
  when Symbol
    str == route
  end
end

#on(route, option = {}, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/balotelli/core/router.rb', line 8

def on(route, option = {}, &block)
  raise 'no block given' if block.nil?

  option[:private] = false if !option.key?(:private) || route == :join

  if option[:private] == :both
    on(route, option.clone.tap { |o| o[:private] = true }, &block)
    on(route, option.clone.tap { |o| o[:private] = false }, &block)
    return
  end

  @routes[route] ||= {}

  if @routes[route][option[:private]] && !option[:force]
    raise "Route #{route.inspect}, private: #{option[:private]} already exists"
  end

  @routes[route][option[:private]] = [block, self]
end

#setup_routerObject



4
5
6
# File 'lib/balotelli/core/router.rb', line 4

def setup_router
  @routes = {}
end