Class: SimpleController::Router::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_controller/router/mapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(router, namespaces = [], controller_path = nil) ⇒ Mapper

Returns a new instance of Mapper.



6
7
8
# File 'lib/simple_controller/router/mapper.rb', line 6

def initialize(router, namespaces=[], controller_path=nil)
  @router, @namespaces, @controller_path = router, namespaces, controller_path
end

Instance Attribute Details

#controller_pathObject (readonly)

Returns the value of attribute controller_path.



4
5
6
# File 'lib/simple_controller/router/mapper.rb', line 4

def controller_path
  @controller_path
end

#namespacesObject (readonly)

Returns the value of attribute namespaces.



4
5
6
# File 'lib/simple_controller/router/mapper.rb', line 4

def namespaces
  @namespaces
end

#routerObject (readonly)

Returns the value of attribute router.



4
5
6
# File 'lib/simple_controller/router/mapper.rb', line 4

def router
  @router
end

Instance Method Details

#controller(controller_path, options = {}, &block) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/simple_controller/router/mapper.rb', line 19

def controller(controller_path, options={}, &block)
  raise "can't have multiple controller scopes" if self.controller_path

  mapper = self.class.new(router, namespaces, controller_path)
  Array(options[:actions]).each { |action| mapper.match(action) }

  mapper.instance_eval(&block) if block_given?
end

#match(arg) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/simple_controller/router/mapper.rb', line 28

def match(arg)
  route_path, controller_name, action_name = parse_match_arg(arg)

  route_parts = [route_path]
  route_parts.unshift(self.controller_path) if self.controller_path
  route_parts.unshift(*namespaces)

  controller_path_parts = [self.controller_path || controller_name]
  controller_path_parts.unshift(*namespaces)

  router.add_route join_parts(route_parts), join_parts(controller_path_parts), action_name
end

#namespace(namespace, &block) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/simple_controller/router/mapper.rb', line 10

def namespace(namespace, &block)
  @namespaces << namespace

  mapper = self.class.new(router, namespaces, controller_path)
  mapper.instance_eval(&block)
ensure
  @namespaces.pop
end