Class: Trellis::Router

Inherits:
Object show all
Defined in:
lib/trellis/trellis.rb

Overview

– Router – A Router returns a Route in response to an HTTP request

Direct Known Subclasses

DefaultRouter

Constant Summary collapse

EVENT_REGEX =
%r{^(?:.+)/events/(?:([^/\.]+)(?:\.([^/\.]+)?)?)(?:/(?:([^\.]+)?))?}

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options={})
  @application = options[:application]
  @path = options[:path]
  @page = options[:page]
  if @path
    compile_path 
    compute_score
  else
    @score = 3 # since "/*" scores at 2
  end
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



364
365
366
# File 'lib/trellis/trellis.rb', line 364

def application
  @application
end

#keysObject (readonly)

Returns the value of attribute keys.



364
365
366
# File 'lib/trellis/trellis.rb', line 364

def keys
  @keys
end

#pageObject (readonly)

Returns the value of attribute page.



364
365
366
# File 'lib/trellis/trellis.rb', line 364

def page
  @page
end

#pathObject (readonly)

Returns the value of attribute path.



364
365
366
# File 'lib/trellis/trellis.rb', line 364

def path
  @path
end

#patternObject (readonly)

Returns the value of attribute pattern.



364
365
366
# File 'lib/trellis/trellis.rb', line 364

def pattern
  @pattern
end

#scoreObject (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

Returns:

  • (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_sObject



412
413
414
# File 'lib/trellis/trellis.rb', line 412

def to_s
  @path
end