Class: Ruta::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/ruta/router.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Router

Returns a new instance of Router.



15
16
17
18
19
# File 'lib/ruta/router.rb', line 15

def initialize block
  @current_context = []
  Context.define(:no_context)
  instance_exec(&block)
end

Class Attribute Details

.current_contextArray<Symbol> (readonly)

Returns root the initial context of the app.

Returns:

  • (Array<Symbol>)

    root the initial context of the app



# File 'lib/ruta/router.rb', line 55

.historyArray<Symbol> (readonly)

Returns history.

Returns:

  • (Array<Symbol>)

    history



# File 'lib/ruta/router.rb', line 58

.rootArray<Symbol> (readonly)

Returns root the initial context of the app.

Returns:

  • (Array<Symbol>)

    root the initial context of the app



67
# File 'lib/ruta/router.rb', line 67

attr_reader :current_context, :root

.windowArray<Symbol> (readonly)

Returns window.

Returns:

  • (Array<Symbol>)

    window



# File 'lib/ruta/router.rb', line 61

Instance Attribute Details

#current_contextArray<Symbol>

Returns current_context a list of contexts, the last being the current.

Returns:

  • (Array<Symbol>)

    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

Note:

please be aware that placing contexts within other contexts doesn’t actully do anything.

define a router, this can be called multiple times

Examples:

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

Yields:

  • Use this block to define any routes



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


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

Parameters:

  • context (Symbol)

    to map 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

Parameters:

  • ref (Symbol)

    to map route to for easy future reference



32
33
34
35
# 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) ⇒ Object

Note:

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

Parameters:

  • reference (Symbol)

    to context



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