lib/simple_controller/router/mapper.rb in simple_controller-0.1.1 vs lib/simple_controller/router/mapper.rb in simple_controller-1.0.0

- old
+ new

@@ -1,47 +1,43 @@ -require 'simple_controller/router/route' - module SimpleController class Router class Mapper - attr_reader :router, :namespaces, :controller_name + attr_reader :router, :namespaces, :controller_path - def initialize(router, namespaces=[], controller_name=nil) - @router, @namespaces, @controller_name = router, namespaces, controller_name + def initialize(router, namespaces=[], controller_path=nil) + @router, @namespaces, @controller_path = router, namespaces, controller_path end def namespace(namespace, &block) @namespaces << namespace - mapper = self.class.new(router, namespaces, controller_name) + mapper = self.class.new(router, namespaces, controller_path) mapper.instance_eval(&block) ensure @namespaces.pop end - def controller(controller_name, options={}, &block) - raise "can't have multiple controller scopes" if self.controller_name + def controller(controller_path, options={}, &block) + raise "can't have multiple controller scopes" if self.controller_path - mapper = self.class.new(router, namespaces, controller_name) + mapper = self.class.new(router, namespaces, controller_path) Array(options[:actions]).each { |action| mapper.match(action) } mapper.instance_eval(&block) if block_given? end def match(arg) - route_path, partition = parse_match_arg(arg) + route_path, controller_name, action_name = parse_match_arg(arg) route_parts = [route_path] - route_parts.unshift(self.controller_name) if self.controller_name + route_parts.unshift(self.controller_path) if self.controller_path route_parts.unshift(*namespaces) - controller_name_parts = [self.controller_name || partition.first] - controller_name_parts.unshift(*namespaces) + controller_path_parts = [self.controller_path || controller_name] + controller_path_parts.unshift(*namespaces) - action_name = partition.last - - router.add_route join_parts(route_parts), Route.new(join_parts(controller_name_parts), action_name) + router.add_route join_parts(route_parts), join_parts(controller_path_parts), action_name end protected def join_parts(parts) parts.map do |part| @@ -52,17 +48,21 @@ end.join("/") end def parse_match_arg(arg) if arg.class == Hash + # match "threes/dividing" => "threes#divide" + # match subtracting: "subtract" raise "takes only one option" unless arg.size == 1 route_path = arg.keys.first.to_s - partition = arg.values.first.to_s.rpartition("#") + controller_name, _, action_name = arg.values.first.to_s.rpartition("#") else + # match :threes + # match "threes/multiply" route_path = arg.to_s - partition = route_path.rpartition("/") + controller_name, _, action_name = route_path.rpartition("/") end - [route_path, partition] + [route_path, controller_name, action_name] end end end end \ No newline at end of file