module Sinatra::Namespace::NamespacedMethods

Attributes

base[R]
templates[R]

Public Class Methods

prefixed(*names) click to toggle source
# File lib/sinatra/namespace.rb, line 227
def self.prefixed(*names)
  names.each { |n| define_method(n) { |*a, &b| prefixed(n, *a, &b) }}
end

Public Instance Methods

disable(*opts) click to toggle source
# File lib/sinatra/namespace.rb, line 291
def disable(*opts)
  opts.each { |key| set(key, false) }
end
enable(*opts) click to toggle source
# File lib/sinatra/namespace.rb, line 287
def enable(*opts)
  opts.each { |key| set(key, true) }
end
error(*codes, &block) click to toggle source
# File lib/sinatra/namespace.rb, line 263
def error(*codes, &block)
  args  = Sinatra::Base.send(:compile!, "ERROR", /.*/, block)
  codes = codes.map { |c| Array(c) }.flatten
  codes << Exception if codes.empty?
  codes << Sinatra::NotFound if codes.include?(404)

  codes.each do |c|
    errors = @errors[c] ||= []
    errors << args
  end
end
errors() click to toggle source
# File lib/sinatra/namespace.rb, line 255
def errors
  base.errors.merge(namespace_errors)
end
helpers(*extensions, &block) click to toggle source
# File lib/sinatra/namespace.rb, line 233
def helpers(*extensions, &block)
  class_eval(&block) if block_given?
  include(*extensions) if extensions.any?
end
invoke_hook(name, *args) click to toggle source
# File lib/sinatra/namespace.rb, line 247
def invoke_hook(name, *args)
  @extensions.each { |e| e.send(name, *args) if e.respond_to?(name) }
end
layout(name=:layout, &block) click to toggle source
# File lib/sinatra/namespace.rb, line 300
def layout(name=:layout, &block)
  template name, &block
end
namespace_errors() click to toggle source
# File lib/sinatra/namespace.rb, line 259
def namespace_errors
  @errors
end
not_found(&block) click to toggle source
# File lib/sinatra/namespace.rb, line 251
def not_found(&block)
  error(Sinatra::NotFound, &block)
end
pattern() click to toggle source
# File lib/sinatra/namespace.rb, line 304
def pattern
  @pattern
end
register(*extensions, &block) click to toggle source
# File lib/sinatra/namespace.rb, line 238
def register(*extensions, &block)
  extensions << Module.new(&block) if block_given?
  @extensions += extensions
  extensions.each do |extension|
    extend extension
    extension.registered(self) if extension.respond_to?(:registered)
  end
end
respond_to(*args) click to toggle source
# File lib/sinatra/namespace.rb, line 275
def respond_to(*args)
  return @conditions[:provides] || base.respond_to if args.empty?
  @conditions[:provides] = args
end
set(key, value = self, &block) click to toggle source
# File lib/sinatra/namespace.rb, line 280
def set(key, value = self, &block)
  raise ArgumentError, "may not set #{key}" if key != :views
  return key.each { |k,v| set(k, v) } if block.nil? and value == self
  block ||= proc { value }
  singleton_class.send(:define_method, key, &block)
end
template(name, &block) click to toggle source
# File lib/sinatra/namespace.rb, line 295
def template(name, &block)
  filename, line = caller_locations.first
  templates[name] = [block, filename, line.to_i]
end

Private Instance Methods

app() click to toggle source
# File lib/sinatra/namespace.rb, line 310
def app
  base.respond_to?(:base) ? base.base : base
end
compile(pattern, conditions, default_pattern = nil) click to toggle source
# File lib/sinatra/namespace.rb, line 314
def compile(pattern, conditions, default_pattern = nil)
  if pattern.respond_to? :to_hash
    conditions = conditions.merge pattern.to_hash
    pattern = nil
  end
  base_pattern, base_conditions = @pattern, @conditions
  pattern ||= default_pattern
  [ prefixed_path(base_pattern, pattern),
    (base_conditions || {}).merge(conditions) ]
end
method_missing(method, *args, &block) click to toggle source
# File lib/sinatra/namespace.rb, line 340
def method_missing(method, *args, &block)
  base.send(method, *args, &block)
end
prefixed(method, pattern = nil, conditions = {}, &block) click to toggle source
# File lib/sinatra/namespace.rb, line 332
def prefixed(method, pattern = nil, conditions = {}, &block)
  default = %r{(?:/.*)?} if method == :before or method == :after
  pattern, conditions = compile pattern, conditions, default
  result = base.send(method, pattern, conditions, &block)
  invoke_hook :route_added, method.to_s.upcase, pattern, block
  result
end
prefixed_path(a, b) click to toggle source
# File lib/sinatra/namespace.rb, line 325
def prefixed_path(a, b)
  return a || b || /.*/ unless a and b
  return Mustermann.new(b) if a == /.*/

  Mustermann.new(a) + Mustermann.new(b)
end
respond_to?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/sinatra/namespace.rb, line 344
def respond_to?(method, include_private = false)
  super || base.respond_to?(method, include_private)
end