module Balotelli::Core::Router

Constants

RouteCollisionError

Public Instance Methods

match(str, priv = false) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_router() click to toggle source
# File lib/balotelli/core/router/router.rb, line 6
def setup_router
  @routes = {}
end