Class: Ruta::Route
- Inherits:
-
Object
- Object
- Ruta::Route
- 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
- #flags ⇒ Symbol readonly
- #handlers ⇒ Symbol readonly
- #named ⇒ Symbol readonly
- #regexp ⇒ Symbol readonly
- #type ⇒ Symbol readonly
- #url ⇒ Symbol readonly
Instance Method Summary collapse
-
#execute_handler(params = {}, path = nil) ⇒ Object
execute’s route’s associated handlers.
-
#get(params = nil) ⇒ Symbol => Number, ...
get route hash and paramaterize url if needed.
-
#initialize(pattern, context_ref, flags) ⇒ Route
constructor
A new instance of Route.
-
#match(path) ⇒ Hash, false
match this route against a given path.
-
#paramaterize(params) ⇒ String
take in params and return a paramaterized route.
- #params_hash(params) ⇒ Object
Constructor Details
#initialize(pattern, context_ref, flags) ⇒ Route
Returns a new instance of 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
#flags ⇒ Symbol (readonly)
25 |
# File 'lib/ruta/route.rb', line 25 attr_reader :regexp, :named, :type, :handlers, :url,:flags |
#handlers ⇒ Symbol (readonly)
25 |
# File 'lib/ruta/route.rb', line 25 attr_reader :regexp, :named, :type, :handlers, :url,:flags |
#named ⇒ Symbol (readonly)
25 |
# File 'lib/ruta/route.rb', line 25 attr_reader :regexp, :named, :type, :handlers, :url,:flags |
#regexp ⇒ Symbol (readonly)
25 26 27 |
# File 'lib/ruta/route.rb', line 25 def regexp @regexp end |
#type ⇒ Symbol (readonly)
25 |
# File 'lib/ruta/route.rb', line 25 attr_reader :regexp, :named, :type, :handlers, :url,:flags |
#url ⇒ Symbol (readonly)
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
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
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
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
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 |