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.
347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/trellis/trellis.rb', line 347 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.
345 346 347 |
# File 'lib/trellis/trellis.rb', line 345 def application @application end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
345 346 347 |
# File 'lib/trellis/trellis.rb', line 345 def keys @keys end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
345 346 347 |
# File 'lib/trellis/trellis.rb', line 345 def page @page end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
345 346 347 |
# File 'lib/trellis/trellis.rb', line 345 def path @path end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
345 346 347 |
# File 'lib/trellis/trellis.rb', line 345 def pattern @pattern end |
#score ⇒ Object (readonly)
Returns the value of attribute score.
345 346 347 |
# File 'lib/trellis/trellis.rb', line 345 def score @score end |
Instance Method Details
#inject_parameters_into_page_instance(page, request) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/trellis/trellis.rb', line 369 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
365 366 367 |
# File 'lib/trellis/trellis.rb', line 365 def matches?(request) request.path_info.gsub(/\/events\/.*/, '').match(@pattern) != nil end |
#route(request = nil) ⇒ Object
359 360 361 362 363 |
# File 'lib/trellis/trellis.rb', line 359 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
393 394 395 |
# File 'lib/trellis/trellis.rb', line 393 def to_s @path end |