Module: Balotelli::Core::Router

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

Defined Under Namespace

Classes: Action, Match

Constant Summary collapse

RouteCollisionError =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#match(str, priv = false) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/balotelli/core/router/router.rb', line 42

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

#match?(str, route) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
# File 'lib/balotelli/core/router/router.rb', line 31

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

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



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

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

  privacy = options.fetch(:private, false)

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

  @routes[route] ||= {}

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

  @routes[route][privacy] = Action.new(block, self)
end

#setup_routerObject



6
7
8
# File 'lib/balotelli/core/router/router.rb', line 6

def setup_router
  @routes = {}
end