Class: Freddie::Handlers::Path

Inherits:
Base
  • Object
show all
Defined in:
lib/freddie/handlers.rb

Instance Attribute Summary

Attributes inherited from Base

#args, #block, #env

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#after_perform, #before_perform, #call, call, #context, #finish!, #finished?, #initialize, #layout, #method_missing, #params, #perform_freddie_block, #request, #response, #session

Constructor Details

This class inherits a constructor from Freddie::Handlers::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Freddie::Handlers::Base

Class Method Details

.path_to_regexp(path) ⇒ Object



113
114
115
116
# File 'lib/freddie/handlers.rb', line 113

def path_to_regexp(path)
  path = ":#{path}" if path.is_a?(Symbol)
  Regexp.compile('^'+path.gsub(/\)/, ')?').gsub(/\//, '\/').gsub(/\./, '\.').gsub(/:(\w+)/, '(?<\\1>\w+)')+'$')
end

Instance Method Details

#performObject



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/freddie/handlers.rb', line 98

def perform
  path = args.shift

  if match_data = self.class.path_to_regexp(path).match(context.path.first)
    # write parameter matches into params
    match_data.names.each { |k| params[k] = match_data[k] }

    # execute block
    el = context.path.shift
    perform_freddie_block
    context.path.unshift(el)
  end
end