Module: Sinatra::Trails

Defined in:
lib/sinatra/trails.rb,
lib/sinatra/trails/version.rb

Defined Under Namespace

Modules: Helpers Classes: Resource, Resources, Route, RouteNotDefined, Scope, ScopeMatcher

Constant Summary collapse

VERSION =
"0.0.7"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



284
285
286
287
288
289
290
291
292
# File 'lib/sinatra/trails.rb', line 284

def registered app
  routes_module = Module.new do
    include Helpers
    @routes = Hash.new { |hash, key| hash[key.to_s] if Symbol === key }
  end

  app.const_set :Routes, routes_module
  app.send :include, routes_module
end

.route_for(klass, name) ⇒ Object

Raises:



274
275
276
277
278
279
280
281
282
# File 'lib/sinatra/trails.rb', line 274

def route_for klass, name
  klass.ancestors.each do |ancestor| 
    next unless ancestor.class == Module && ancestor.include?(Helpers)
    if route = ancestor.instance_variable_get(:@routes)[name]
      return route
    end
  end
  raise RouteNotDefined.new("The route `#{name}` is not defined")
end

Instance Method Details

#map(name, opts = {}, &block) ⇒ Object



229
230
231
232
# File 'lib/sinatra/trails.rb', line 229

def map name, opts = {}, &block
  namespace(nil) { map name, opts, &block }
  route_for name
end

#namespace(name, &block) ⇒ Object



225
226
227
# File 'lib/sinatra/trails.rb', line 225

def namespace name, &block
  named_routes.merge! Scope.new(self, name).routes_hash(&block)
end

#path_for(name, *args) ⇒ Object



246
247
248
# File 'lib/sinatra/trails.rb', line 246

def path_for name, *args
  Trails.route_for(self, name).to_path(*args)
end


250
251
252
253
254
255
256
# File 'lib/sinatra/trails.rb', line 250

def print_routes
  trails       = named_routes.map { |name, route| [name, route.to_route] }
  name_padding = trails.sort_by{ |e| e.first.size }.last.first.size + 3
  trails.each do |name, route|
    puts sprintf("%#{name_padding}s => %s", name, route) 
  end
end

#resource(*args, &block) ⇒ Object



234
235
236
# File 'lib/sinatra/trails.rb', line 234

def resource *args, &block
  namespace(nil) { resource *args, &block }
end

#resources(*args, &block) ⇒ Object



238
239
240
# File 'lib/sinatra/trails.rb', line 238

def resources *args, &block
  namespace(nil) { resources *args, &block }
end

#route_for(name) ⇒ Object



242
243
244
# File 'lib/sinatra/trails.rb', line 242

def route_for name
  Trails.route_for(self, name).to_route
end