class Panda::Routing::Router

Attributes

endpoints[R]

Public Class Methods

new() click to toggle source
# File lib/panda/routing/router.rb, line 6
def initialize
  @endpoints ||= Hash.new { |h, k| h[k] = [] }
end

Public Instance Methods

draw(&block) click to toggle source
# File lib/panda/routing/router.rb, line 10
def draw(&block)
  instance_eval(&block)
end
root(target) click to toggle source
# File lib/panda/routing/router.rb, line 20
def root(target) get "/", to: target end

Private Instance Methods

match_placeholders(path) click to toggle source
# File lib/panda/routing/router.rb, line 33
def match_placeholders(path)
  placeholders = []
  path_ = path.gsub(/(:\w+)/) do |match|
    placeholders << match[1..-1].freeze
    "(?<#{placeholders.last}>\\w+)"
  end
  [/^#{path_}$/, placeholders]
end
route(verb, url, options = {}) click to toggle source
# File lib/panda/routing/router.rb, line 24
def route(verb, url, options = {})
  url = "/#{url}" unless url[0] == "/"
  @endpoints[verb] << {
    pattern: match_placeholders(url),
    path: Regexp.new("^#{url}$"),
    target: set_controller_action(options[:to])
  }
end
set_controller_action(string) click to toggle source
# File lib/panda/routing/router.rb, line 42
def set_controller_action(string)
  string =~ /^([^#]+)#([^#]+)$/
  [$1.to_camel_case, $2]
end