Class: SimpleController::Router
- Inherits:
-
Object
- Object
- SimpleController::Router
- 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
Instance Attribute Summary collapse
-
#controller_name_block ⇒ Object
readonly
Returns the value of attribute controller_name_block.
-
#route ⇒ Object
readonly
Returns the value of attribute route.
-
#route_mapping ⇒ Object
readonly
Returns the value of attribute route_mapping.
-
#route_path ⇒ Object
readonly
Returns the value of attribute route_path.
Class Method Summary collapse
Instance Method Summary collapse
- #add_route(route_path, route) ⇒ Object
- #call(route_path, params = {}) ⇒ Object
- #draw(&block) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #parse_controller_name(&block) ⇒ Object
- #route_paths ⇒ Object
Constructor Details
#initialize ⇒ Router
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_block ⇒ Object (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 |
#route ⇒ Object (readonly)
Returns the value of attribute route.
5 6 7 |
# File 'lib/simple_controller/router.rb', line 5 def route @route end |
#route_mapping ⇒ Object (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_path ⇒ Object (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 |
.instance ⇒ Object
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_paths ⇒ Object
28 29 30 |
# File 'lib/simple_controller/router.rb', line 28 def route_paths route_mapping.keys end |