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_name = nil) ⇒ Mapper

Returns a new instance of Mapper.



8
9
10
# File 'lib/simple_controller/router/mapper.rb', line 8

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

Instance Attribute Details

#controller_nameObject (readonly)

Returns the value of attribute controller_name.



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

def controller_name
  @controller_name
end

#namespacesObject (readonly)

Returns the value of attribute namespaces.



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

def namespaces
  @namespaces
end

#routerObject (readonly)

Returns the value of attribute router.



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

def router
  @router
end

Instance Method Details

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



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

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

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

  mapper.instance_eval(&block) if block_given?
end

#match(arg) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/simple_controller/router/mapper.rb', line 30

def match(arg)
  route_path, partition = parse_match_arg(arg)

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

  controller_name_parts = [self.controller_name || partition.first]
  controller_name_parts.unshift(*namespaces)

  action_name = partition.last

  router.add_route join_parts(route_parts), Route.new(join_parts(controller_name_parts), action_name)
end

#namespace(namespace, &block) ⇒ Object



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

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

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