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.



347
348
349
350
351
352
353
354
355
356
357
# File 'lib/trellis/trellis.rb', line 347

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.



345
346
347
# File 'lib/trellis/trellis.rb', line 345

def application
  @application
end

#keysObject (readonly)

Returns the value of attribute keys.



345
346
347
# File 'lib/trellis/trellis.rb', line 345

def keys
  @keys
end

#pageObject (readonly)

Returns the value of attribute page.



345
346
347
# File 'lib/trellis/trellis.rb', line 345

def page
  @page
end

#pathObject (readonly)

Returns the value of attribute path.



345
346
347
# File 'lib/trellis/trellis.rb', line 345

def path
  @path
end

#patternObject (readonly)

Returns the value of attribute pattern.



345
346
347
# File 'lib/trellis/trellis.rb', line 345

def pattern
  @pattern
end

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

Returns:

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



393
394
395
# File 'lib/trellis/trellis.rb', line 393

def to_s
  @path
end