class Ruta::Router

Attributes

current_context[R]

@!attribute [r] root @return [Array<Symbol>] root the initial context of the app

root[R]

@!attribute [r] root @return [Array<Symbol>] root the initial context of the app

current_context[RW]

@!attribute [r,w] current_context @return [Array<Symbol>] current_context a list of contexts, the last being the current

Public Class Methods

new(block) click to toggle source

@see (see Router#define)

# File lib/ruta/router.rb, line 15
def initialize block
  @current_context = []
  Context.define(:no_context)
  instance_exec(&block)
end

Private Class Methods

define(&block) click to toggle source

define a router, this can be called multiple times @example defining routes

Ruta::Router.define do
  for_context :main do
    for_context :info_view do
      map :i_switch, '/buttons/:switch_to', to: [:scroller,:buttons]
      map :sign_up, '/sign_up', context: :sign_up
    end
  end

  root_to :main
end

@note please be aware that placing contexts within other contexts doesn’t actully do anything. @yield Use this block to define any routes

# File lib/ruta/router.rb, line 83
def define &block
  new block
end
find(path) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
route_for(context, ref,params=nil) click to toggle source
# 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) click to toggle source
# File lib/ruta/router.rb, line 87
def set_context_to context
  @current_context = context
end
set_root_to(context) click to toggle source
# File lib/ruta/router.rb, line 125
def set_root_to context
  @root = context
  Router.set_context_to root
end

Public Instance Methods

for_context(context) { || ... } click to toggle source

set which Context to map the following routes to

@param [Symbol] context to map routes to

# File lib/ruta/router.rb, line 24
def for_context context
  @current_context << context
  yield
  @current_context.pop
end
map(ref,route, options={}) click to toggle source
map a route

@param [Symbol] ref to map route to for easy future reference

# File lib/ruta/router.rb, line 32
def map ref,route, options={}
  context = Context.collection[get_context]
  context.routes[ref]= Route.new(route, context,options)
end
root_to(reference) click to toggle source

set the root context, this is the initial context that will be renered by the router

@note there is only ever one root, calling this multiple times will over right the original root @param [Symbol] reference to context

# 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

Private Instance Methods

get_context() click to toggle source
# File lib/ruta/router.rb, line 49
def get_context
  @current_context.last || :no_context
end