Class: Trellis::Router
Overview
– Router – A Router returns a Route in response to an HTTP request
Direct Known Subclasses
Constant Summary collapse
- EVENT_REGEX =
%r{^(?:.+)/events/(?:([^/\.]+)(?:\.([^/\.]+)?)?)(?:/(?:([^\.]+)?))?}
Instance Attribute Summary collapse
-
#application ⇒ Object
readonly
Returns the value of attribute application.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
-
#score ⇒ Object
readonly
Returns the value of attribute score.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Router
constructor
A new instance of Router.
- #inject_parameters_into_page_instance(page, request) ⇒ Object
- #matches?(request) ⇒ Boolean
- #route(request = nil) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Router
Returns a new instance of Router.
366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/trellis/trellis.rb', line 366 def initialize(={}) @application = [:application] @path = [:path] @page = [:page] if @path compile_path compute_score else @score = 3 # since "/*" scores at 2 end end |
Instance Attribute Details
#application ⇒ Object (readonly)
Returns the value of attribute application.
364 365 366 |
# File 'lib/trellis/trellis.rb', line 364 def application @application end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
364 365 366 |
# File 'lib/trellis/trellis.rb', line 364 def keys @keys end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
364 365 366 |
# File 'lib/trellis/trellis.rb', line 364 def page @page end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
364 365 366 |
# File 'lib/trellis/trellis.rb', line 364 def path @path end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
364 365 366 |
# File 'lib/trellis/trellis.rb', line 364 def pattern @pattern end |
#score ⇒ Object (readonly)
Returns the value of attribute score.
364 365 366 |
# File 'lib/trellis/trellis.rb', line 364 def score @score end |
Instance Method Details
#inject_parameters_into_page_instance(page, request) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/trellis/trellis.rb', line 388 def inject_parameters_into_page_instance(page, request) # extract parameters and named parameters from request if @pattern && @page && match = @pattern.match(request.path_info.gsub(/\/events\/.*/, '')) values = match.captures.to_a params = if @keys.any? @keys.zip(values).inject({}) do |hash,(k,v)| if k == 'splat' (hash[k] ||= []) << v else hash[k] = v end hash end elsif values.any? {'captures' => values} else {} end params << request.params params.each_pair { |name, value| page.instance_variable_set("@#{name}".to_sym, value) } end end |
#matches?(request) ⇒ Boolean
384 385 386 |
# File 'lib/trellis/trellis.rb', line 384 def matches?(request) request.path_info.gsub(/\/events\/.*/, '').match(@pattern) != nil end |
#route(request = nil) ⇒ Object
378 379 380 381 382 |
# File 'lib/trellis/trellis.rb', line 378 def route(request = nil) # get the event information if any value, source, event = request.path_info.match(EVENT_REGEX).to_a.reverse if request Route.new(@page, event, source, value) end |
#to_s ⇒ Object
412 413 414 |
# File 'lib/trellis/trellis.rb', line 412 def to_s @path end |