Sha256: 107d64693db47fe207d282d83ef01fda103498c64a926638b2426f49c9f426e3

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module Panda
  module Routing
    class Router
      attr_reader :endpoints

      def initialize
        @endpoints ||= Hash.new { |h, k| h[k] = [] }
      end

      def draw(&block)
        instance_eval(&block)
      end

      %w(get post delete put patch).each do |method_name|
        define_method(method_name) do |path, options|
          route method_name.upcase, path, options
        end
      end

      def root(target) get "/", to: target end

      private

      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

      def match_placeholders(path)
        placeholders = []
        path_ = path.gsub(/(:\w+)/) do |match|
          placeholders << match[1..-1].freeze
          "(?<#{placeholders.last}>\\w+)"
        end
        [/^#{path_}$/, placeholders]
      end

      def set_controller_action(string)
        string =~ /^([^#]+)#([^#]+)$/
        [$1.to_camel_case, $2]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
panda_frwk-0.1.0 lib/panda/routing/router.rb