Sha256: 6f6dee16a6634f0a2f049a7da08686153779feac8b09c2cbb02e7988778b2f07

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'simple_controller/router/route'
require 'simple_controller/router/mapper'

module SimpleController
  class Router
    module Core
      extend ActiveSupport::Concern

      included do
        attr_reader :route_mapping, :route_path, :params, :controller_path_block
      end
      def initialize
        @route_mapping = {}
      end

      def call(route_path, params={})
        @route_path = route_path.to_s
        @params = params

        route = @route_mapping[@route_path]
        raise "#{self.class} route for '#{@route_path}' not found" unless route
        _call(route)
      ensure
        @route_path = @params = nil
      end

      def route_paths
        route_mapping.keys
      end

      def draw(&block)
        mapper = Mapper.new(self)
        mapper.instance_eval(&block)
        self
      end

      def add_route(route_path, controller_path, action)
        @route_mapping[route_path] = Route.new(controller_path, action)
      end

      def parse_controller_path(&block)
        @controller_path_block = block
      end

      protected
      def _call(route)
        route.call params, controller_path_block
      end

      module ClassMethods
        def instance
          @instance ||= new
        end

        def call(*args)
          instance.call(*args)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_controller-1.0.0 lib/simple_controller/router/core.rb