Sha256: c75d643c5fe18a5270440c0405070f0abbd6bc28ec86a4f071a24bb5c52db27e

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'simple_controller/router/mapper'

module SimpleController
  class Router
    attr_reader :route_mapping, :route, :route_path, :controller_name_block

    include ActiveSupport::Callbacks
    define_callbacks :call

    def initialize
      @route_mapping = {}
    end

    def call(route_path, params={})
      @route_path = route_path.to_s
      @route = @route_mapping[@route_path]

      raise "#{self.class} route for '#{@route_path}' not found" unless route

      run_callbacks(:call) do
        @route.call params, controller_name_block
      end
    ensure
      @route_path = nil
      @route = nil
    end

    def route_paths
      route_mapping.keys
    end

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

    def add_route(route_path, route)
      @route_mapping[route_path] = route
    end

    def parse_controller_name(&block)
      @controller_name_block = block
    end

    class << self
      def instance
        @instance ||= new
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_controller-0.1.1 lib/simple_controller/router.rb