Class: Doozer::Routing::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/doozer/route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route) ⇒ Route

Initializes a route with the following parameters route - [:name, ‘path’, args]



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/doozer/route.rb', line 209

def initialize(route)
  #p "Doozer::Route#new: #{route}"
  args = route[2]
  @controller = args[:controller]
  @action = args[:action]
  @layout = (args[:layout]) ? args[:layout] : 'default'
  @status = (args[:status]) ? args[:status] : 200
  @app=args[:app]
  @middleware_before=args[:middleware_before]
  @middleware_after=args[:middleware_after]
  @format = (args[:format]) ? args[:format] : :html
  #@content_type = (args[:content_type]) ? args[:content_type] : 'text/html'
  case @format
    when :js
      content_type = 'text/javascript'
    when :xml
      content_type = 'text/xml'
    when :json
      content_type = 'application/json'
    when :rss
      content_type = 'application/rss+xml'
    when :atom
      content_type = 'application/atom+xml'
    else
      content_type = 'text/html'
  end
  @content_type = content_type
  @tokens = []
  path = route[1]
  path = '/' if path == ''
  @path = (@format == :html) ? path : "#{path}.#{format}"
  @name = (@format == :html) ? route[0] : "#{route[0]}_#{format.to_s}".to_sym
  @layout = "default_#{@format.to_s}".to_sym if @format != :html and @layout == 'default'
  
  @view = "#{@action}_#{@format.to_s}"
  @view_path = "#{@controller}/#{@action}.#{@format.to_s}.erb"
  regify()
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



202
203
204
# File 'lib/doozer/route.rb', line 202

def action
  @action
end

#appObject

Returns the value of attribute app.



202
203
204
# File 'lib/doozer/route.rb', line 202

def app
  @app
end

#content_typeObject

Returns the value of attribute content_type.



202
203
204
# File 'lib/doozer/route.rb', line 202

def content_type
  @content_type
end

#controllerObject

Returns the value of attribute controller.



202
203
204
# File 'lib/doozer/route.rb', line 202

def controller
  @controller
end

#formatObject

Returns the value of attribute format.



202
203
204
# File 'lib/doozer/route.rb', line 202

def format
  @format
end

#groupingObject

Returns the value of attribute grouping.



202
203
204
# File 'lib/doozer/route.rb', line 202

def grouping
  @grouping
end

#layoutObject

Returns the value of attribute layout.



202
203
204
# File 'lib/doozer/route.rb', line 202

def layout
  @layout
end

#middleware_afterObject

Returns the value of attribute middleware_after.



202
203
204
# File 'lib/doozer/route.rb', line 202

def middleware_after
  @middleware_after
end

#middleware_beforeObject

Returns the value of attribute middleware_before.



202
203
204
# File 'lib/doozer/route.rb', line 202

def middleware_before
  @middleware_before
end

#nameObject

Returns the value of attribute name.



202
203
204
# File 'lib/doozer/route.rb', line 202

def name
  @name
end

#pathObject

Returns the value of attribute path.



202
203
204
# File 'lib/doozer/route.rb', line 202

def path
  @path
end

#statusObject

Returns the value of attribute status.



202
203
204
# File 'lib/doozer/route.rb', line 202

def status
  @status
end

#tokensObject

Returns the value of attribute tokens.



202
203
204
# File 'lib/doozer/route.rb', line 202

def tokens
  @tokens
end

#viewObject

Returns the value of attribute view.



202
203
204
# File 'lib/doozer/route.rb', line 202

def view
  @view
end

#view_pathObject

Returns the value of attribute view_path.



202
203
204
# File 'lib/doozer/route.rb', line 202

def view_path
  @view_path
end

Instance Method Details

#extra_params(path) ⇒ Object

Parses route tokens and creates a hash of extra params



290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/doozer/route.rb', line 290

def extra_params(path)
  hashish = {}
  params = @grouping.match(path)
  # make sure to remove the format from the last token
  @tokens.last.gsub!(Regexp.compile("\.#{@format.to_s}$"), '') if @format != :html if not @tokens.empty?
  i = 1
  for token in @tokens
    hashish[token.to_sym] = params[i]
    i += 1 
  end        
  return hashish
end

#match(path) ⇒ Object

Matches a request path against a route.path if a direct match or route.grouping



274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/doozer/route.rb', line 274

def match(path)
  # p "#{path} vs #{@path}" 
  # p path =~ @grouping
  # short-circut for root
  return false if path == '/' and @path != '/' #handles root condition
  # short-circut for exact match with no tokens
  return true if path == @path
  # test for tokens
  pass=(path =~ @grouping) == 0 ? true : false
  # p @tokens.inspect if pass
  if @tokens.empty?; pass=false if @path != path; end #handles root condition '/'
  pass=false if path.split('/').length != @path.split('/').length #handles the root condition /:token
  return pass
end

#regifyObject

Creates the Regex grouping for matching and parsing route tokens



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/doozer/route.rb', line 249

def regify
  if (@path.index('/'))
    grouping = []
    url = @path.split('/')
    for part in url
        if /^:/.match(part) 
          token = part.gsub(/:/,'')
          # part = '(?P<'+token+'>.)'
          # part = '(\.*)'
          # part = '(\w*)'
          part = '([a-zA-Z0-9,-.%_~;]*)' # this picks up all allowable route tokens (a-zA-Z0-9,-.%)
          @tokens.push(token)
        end
        grouping.push(part)
    end
    out = "^#{grouping.join('/')}"
    out += ".#{@format.to_s}" if @format != :html # we need to include the 
    @grouping = Regexp.compile(out)
  else
    #handle default index route
    @grouping = Regexp.compile("/")
  end
end

#url_helper_methodObject

Parses route tokens and returns a helper method which evntually is module_eval’d into Doozer::ViewHelpers

> creates helper methods for route_name_url (relative) and route_name_aurl (absolute)



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/doozer/route.rb', line 306

def url_helper_method
  method_name=[@name]
  # method_name.push(@format) if @format != :html
  method_name.push('url')
  signature = []
  signature.push('(')
  if not @tokens.empty?
    t = []
    for token in @tokens
      if token.index('.')
        t.push(token.split('.')[0])
      else
        t.push(token)
      end
    end
    signature.push(t.join(', '))
  end
  signature.push(')')
  
  url_method = []
  url_method.push("url({:name=>:#{@name}")
  if not @tokens.empty?
    t = []
    for token in @tokens
      if token.index('.')
        token = token.split('.')[0]
      end
      t.push(" :#{token.to_sym}=>#{token}")
    end
    url_method.push(",#{t.join(',')}")
  end
  url_method.push("})")
  method = """def #{method_name.join('_')}#{signature.join('')}; #{url_method} end"""
  method += "\n#{method.gsub(/url\(/, 'aurl(')}"
  return method
end