Class: Ruta::Router
- Inherits:
-
Object
- Object
- Ruta::Router
- Defined in:
- lib/ruta/router.rb
Class Attribute Summary collapse
-
.current_context ⇒ Array<Symbol>
readonly
Root the initial context of the app.
-
.history ⇒ Array<Symbol>
readonly
History.
-
.root ⇒ Array<Symbol>
readonly
Root the initial context of the app.
-
.window ⇒ Array<Symbol>
readonly
Window.
Instance Attribute Summary collapse
-
#current_context ⇒ Array<Symbol>
Current_context a list of contexts, the last being the current.
Class Method Summary collapse
-
.define { ... } ⇒ Object
define a router, this can be called multiple times.
- .find(path) ⇒ Object
- .find_and_execute(path) ⇒ Object
- .navigate_to(route) ⇒ Object
- .route_for(context, ref, params = nil) ⇒ Object
- .set_context_to(context) ⇒ Object
- .set_root_to(context) ⇒ Object
Instance Method Summary collapse
-
#for_context(context) ⇒ Object
set which Context to map the following routes to.
-
#initialize(block) ⇒ Router
constructor
A new instance of Router.
-
#map(ref, route, options = {}) ⇒ Object
map a route.
-
#root_to(reference) ⇒ Object
set the root context, this is the initial context that will be renered by the router.
Constructor Details
Class Attribute Details
.current_context ⇒ Array<Symbol> (readonly)
Returns root the initial context of the app.
|
|
# File 'lib/ruta/router.rb', line 55
|
.history ⇒ Array<Symbol> (readonly)
Returns history.
|
|
# File 'lib/ruta/router.rb', line 58
|
.root ⇒ Array<Symbol> (readonly)
Returns root the initial context of the app.
67 |
# File 'lib/ruta/router.rb', line 67 attr_reader :current_context, :root |
.window ⇒ Array<Symbol> (readonly)
Returns window.
|
|
# File 'lib/ruta/router.rb', line 61
|
Instance Attribute Details
#current_context ⇒ Array<Symbol>
Returns current_context a list of contexts, the last being the current.
12 13 14 |
# File 'lib/ruta/router.rb', line 12 def current_context @current_context end |
Class Method Details
.define { ... } ⇒ Object
please be aware that placing contexts within other contexts doesn’t actully do anything.
define a router, this can be called multiple times
83 84 85 |
# File 'lib/ruta/router.rb', line 83 def define &block new block end |
.find(path) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/ruta/router.rb', line 106 def find path Context.collection.each do |con_ref,context| context.routes.each do |r_ref,route| res = route.match(path) return res if res end end false end |
.find_and_execute(path) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ruta/router.rb', line 91 def find_and_execute(path) path = if Ruta.config.context_prefix ( path == '/' || path == "") ? path : (path[/(?:\/.*?)(\/.*)/];$1) else path end res = find(path) if res navigate_to res else raise "no matching route for #{path}" end end |
.navigate_to(route) ⇒ Object
117 118 119 |
# File 'lib/ruta/router.rb', line 117 def navigate_to(route) route[:route].execute_handler route[:params],route[:path] end |
.route_for(context, ref, params = nil) ⇒ Object
121 122 123 |
# File 'lib/ruta/router.rb', line 121 def route_for context, ref,params=nil Context.collection[context].routes[ref].get(params) end |
.set_context_to(context) ⇒ Object
87 88 89 |
# File 'lib/ruta/router.rb', line 87 def set_context_to context @current_context = context end |
.set_root_to(context) ⇒ Object
125 126 127 128 |
# File 'lib/ruta/router.rb', line 125 def set_root_to context @root = context Router.set_context_to root end |
Instance Method Details
#for_context(context) ⇒ Object
set which Context to map the following routes to
24 25 26 27 28 |
# File 'lib/ruta/router.rb', line 24 def for_context context @current_context << context yield @current_context.pop end |
#map(ref, route, options = {}) ⇒ Object
map a route
32 33 34 35 |
# File 'lib/ruta/router.rb', line 32 def map ref,route, ={} context = Context.collection[get_context] context.routes[ref]= Route.new(route, context,) end |
#root_to(reference) ⇒ Object
there is only ever one root, calling this multiple times will over right the original root
set the root context, this is the initial context that will be renered by the router
41 42 43 44 45 |
# File 'lib/ruta/router.rb', line 41 def root_to reference Router.set_root_to reference context = Context.collection[reference] context.routes[:root]= Route.new('/', context,{ context: reference}) end |