Class: SimpleController::Router

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/simple_controller/router.rb,
lib/simple_controller/router/route.rb,
lib/simple_controller/router/mapper.rb

Defined Under Namespace

Classes: Mapper, Route

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



10
11
12
# File 'lib/simple_controller/router.rb', line 10

def initialize
  @route_mapping = {}
end

Instance Attribute Details

#controller_name_blockObject (readonly)

Returns the value of attribute controller_name_block.



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

def controller_name_block
  @controller_name_block
end

#routeObject (readonly)

Returns the value of attribute route.



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

def route
  @route
end

#route_mappingObject (readonly)

Returns the value of attribute route_mapping.



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

def route_mapping
  @route_mapping
end

#route_pathObject (readonly)

Returns the value of attribute route_path.



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

def route_path
  @route_path
end

Class Method Details

.call(*args) ⇒ Object



50
51
52
# File 'lib/simple_controller/router.rb', line 50

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

.instanceObject



46
47
48
# File 'lib/simple_controller/router.rb', line 46

def instance
  @instance ||= new
end

Instance Method Details

#add_route(route_path, route) ⇒ Object



37
38
39
# File 'lib/simple_controller/router.rb', line 37

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

#call(route_path, params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/simple_controller/router.rb', line 14

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

#draw(&block) ⇒ Object



32
33
34
35
# File 'lib/simple_controller/router.rb', line 32

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

#parse_controller_name(&block) ⇒ Object



41
42
43
# File 'lib/simple_controller/router.rb', line 41

def parse_controller_name(&block)
  @controller_name_block = block
end

#route_pathsObject



28
29
30
# File 'lib/simple_controller/router.rb', line 28

def route_paths
  route_mapping.keys
end