Class: Ruta::Route

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

Overview

this class was taken from vienna and modified github.com/opal/vienna/blob/master/opal/vienna/router.rb#L42

Constant Summary collapse

DOM =
::Kernel.method(:DOM)
NAMED =
/:(\w+)/
SPLAT =
/\\\*(\w+)/
OPTIONAL =
/\\\((.*?)\\\)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, context_ref, flags) ⇒ Route

Returns a new instance of Route.

Parameters:

  • pattern (String)

    of url to match against

  • context_ref (Symbol)

    to context route is mounted to

  • flags ({Symbol => String,Number,Boolean})

    attached to route



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruta/route.rb', line 30

def initialize(pattern, context_ref,flags)

 if flags[:to]
   @type = :handlers
   @handlers = [flags.delete(:to)].flatten
 elsif flags[:context]
   @type = :context
   @handlers = flags.delete :context
 else
   @type = :ref_only
 end
 @context_ref = context_ref
 @flags = flags
 @url = pattern
 @named = []

 pattern = Regexp.escape pattern
 pattern = pattern.gsub OPTIONAL, "(?:$1)?"

 pattern.gsub(NAMED) { |m| @named << m[1..-1] }
 pattern.gsub(SPLAT) { |m| @named << m[2..-1] }

 pattern = pattern.gsub NAMED, "([^\\/]*)"
 pattern = pattern.gsub SPLAT, "(.*?)"

 @regexp = Regexp.new "^#{pattern}$"

end

Instance Attribute Details

#flagsSymbol (readonly)

Returns:

  • (Symbol)

    the regexp used to match against a route

  • (Symbol)

    list of all named paramters

  • (Symbol)

    the type of the route’s handler

  • (Symbol)

    a list of the handlers this route is suposed to execute

  • (Symbol)

    the raw url to match against

  • (Symbol)

    any flags this route possesses



25
# File 'lib/ruta/route.rb', line 25

attr_reader :regexp, :named, :type, :handlers, :url,:flags

#handlersSymbol (readonly)

Returns:

  • (Symbol)

    the regexp used to match against a route

  • (Symbol)

    list of all named paramters

  • (Symbol)

    the type of the route’s handler

  • (Symbol)

    a list of the handlers this route is suposed to execute

  • (Symbol)

    the raw url to match against

  • (Symbol)

    any flags this route possesses



25
# File 'lib/ruta/route.rb', line 25

attr_reader :regexp, :named, :type, :handlers, :url,:flags

#namedSymbol (readonly)

Returns:

  • (Symbol)

    the regexp used to match against a route

  • (Symbol)

    list of all named paramters

  • (Symbol)

    the type of the route’s handler

  • (Symbol)

    a list of the handlers this route is suposed to execute

  • (Symbol)

    the raw url to match against

  • (Symbol)

    any flags this route possesses



25
# File 'lib/ruta/route.rb', line 25

attr_reader :regexp, :named, :type, :handlers, :url,:flags

#regexpSymbol (readonly)

Returns:

  • (Symbol)

    the regexp used to match against a route

  • (Symbol)

    list of all named paramters

  • (Symbol)

    the type of the route’s handler

  • (Symbol)

    a list of the handlers this route is suposed to execute

  • (Symbol)

    the raw url to match against

  • (Symbol)

    any flags this route possesses



25
26
27
# File 'lib/ruta/route.rb', line 25

def regexp
  @regexp
end

#typeSymbol (readonly)

Returns:

  • (Symbol)

    the regexp used to match against a route

  • (Symbol)

    list of all named paramters

  • (Symbol)

    the type of the route’s handler

  • (Symbol)

    a list of the handlers this route is suposed to execute

  • (Symbol)

    the raw url to match against

  • (Symbol)

    any flags this route possesses



25
# File 'lib/ruta/route.rb', line 25

attr_reader :regexp, :named, :type, :handlers, :url,:flags

#urlSymbol (readonly)

Returns:

  • (Symbol)

    the regexp used to match against a route

  • (Symbol)

    list of all named paramters

  • (Symbol)

    the type of the route’s handler

  • (Symbol)

    a list of the handlers this route is suposed to execute

  • (Symbol)

    the raw url to match against

  • (Symbol)

    any flags this route possesses



25
# File 'lib/ruta/route.rb', line 25

attr_reader :regexp, :named, :type, :handlers, :url,:flags

Instance Method Details

#execute_handler(params = {}, path = nil) ⇒ Object

execute’s route’s associated handlers

Parameters:

  • params (Symbol => String) (defaults to: {})

    from the route with there respective keys

  • path (String) (defaults to: nil)

    containing params placed into there respective named positions



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ruta/route.rb', line 115

def execute_handler params={},path=nil
 case @type
 when :handlers
   @handlers.each do |handler_ident|
     handler = @context_ref.handlers.fetch(handler_ident) {raise "handler #{handler_ident} doesn't exist in context #{@context_ref.ref}"}
     component = handler.(params,path||@url,&:call)
     Context.current_context = @context_ref.ref
     if component.class == Proc
       component.call
     else
       Context.renderer.call(component,handler_ident)
     end
     Context.current_context = :no_context
   end
 when :context
   Context.wipe
   Context.render handlers
   History.push(@context_ref.ref,"",[],@context_ref.ref)
 end
end

#get(params = nil) ⇒ Symbol => Number, ...

get route hash and paramaterize url if needed

Parameters:

  • params (Array<String,Number,Boolean>) (defaults to: nil)

    to replace named params in the returned url

Returns:

  • (Symbol => Number, String, Route)

    hash specificly formatted:

    url: of the route with named params replaced,
    title: the name of page if the url has one,
    params: a list of all the params used in the route,
    route: the #Route object
    



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ruta/route.rb', line 78

def get params=nil
 path = if params
   paramaterize params.dup
 else
   @url
 end
 {
     path: path,
     title: self.flags.fetch(:title){nil},
     params: params_hash(params),
     route: self
 }
end

#match(path) ⇒ Hash, false

match this route against a given path

Parameters:

  • path (String, Regex)

    to match against

Returns:

  • (Hash, false)

    (see #get) or false if there is no match



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ruta/route.rb', line 96

def match(path)
   if match = @regexp.match(path)
     params = {}
     @named.each_with_index { |name, i| params[name] = match[i + 1] } if @type == :handlers
     {
         path: path,
         title: self.flags.fetch(:title){nil},
         params: params,
         route: self
     }
   else
     false
   end
end

#paramaterize(params) ⇒ String

take in params and return a paramaterized route

Parameters:

  • params (Array<String,Number,Boolean>)

    a list of params to replace named params in a route

Returns:

  • (String)

    url containing named params replaced



63
64
65
66
# File 'lib/ruta/route.rb', line 63

def paramaterize params
 segments = @url.split('/')
 segments.map { |item| item[0] == ':' ? params.shift : item }.join('/')
end

#params_hash(params) ⇒ Object



136
137
138
# File 'lib/ruta/route.rb', line 136

def params_hash(params)
  Hash[@named.zip(params)]
end