Sha256: e436706e0c9f2af9e45f97b64d1401fb77be1cf80b214c11329ec71b05a73771

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Mgt
  module Routing
    class Router
      attr_accessor :endpoints
      def draw(&block)
        instance_eval &block
      end

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

      def endpoints
        @endpoints ||= Hash.new { |hash, key| hash[key] = [] }
      end

      private

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

      def controller_and_action_for(path_to)
        controller_path, action = path_to.split("#")
        controller = "#{controller_path.capitalize}Controller"
        [controller, action.to_sym]
      end

      http_verbs = [:get, :post, :put, :patch, :delete]

      http_verbs.each do |method_name|
        define_method(method_name) do |path, to:|
          path = "/#{path}" unless path[0] = "/"
          klass_and_method = controller_and_action_for(to)
          @route_data = { path: path,
                          pattern: pattern(path),
                          klass_and_method: klass_and_method
                        }
          endpoints[method_name] << @route_data
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mgt-0.1.0 lib/routing/router.rb