123456789_123456789_123456789_123456789_123456789_

Module: Sinatra::Namespace::NamespacedMethods

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Defined in: sinatra-contrib/lib/sinatra/namespace.rb

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) (private)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 355

def method_missing(method, *args, &block)
  base.send(method, *args, &block)
end

Class Method Details

.prefixed(*names)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 237

def self.prefixed(*names)
  names.each { |n| define_method(n) { |*a, &b| prefixed(n, *a, &b) } }
end

Instance Attribute Details

#base (readonly)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 229

attr_reader :base, :templates

#templates (readonly)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 229

attr_reader :base, :templates

Instance Method Details

#app (private)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 324

def app
  base.respond_to?(:base) ? base.base : base
end

#compile(pattern, conditions, default_pattern = nil) (private)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 328

def compile(pattern, conditions, default_pattern = nil)
  if pattern.respond_to? :to_hash
    conditions = conditions.merge pattern.to_hash
    pattern = nil
  end
  base_pattern = @pattern
  base_conditions = @conditions
  pattern ||= default_pattern
  [prefixed_path(base_pattern, pattern),
   (base_conditions || {}).merge(conditions)]
end

#disable(*opts)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 303

def disable(*opts)
  opts.each { |key| set(key, false) }
end

#enable(*opts)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 299

def enable(*opts)
  opts.each { |key| set(key, true) }
end

#error(*codes, &block)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 273

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

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 265

def errors
  base.errors.merge(namespace_errors)
end

#helpers(*extensions, &block)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 243

def helpers(*extensions, &block)
  class_eval(&block) if block_given?
  include(*extensions) if extensions.any?
end

#invoke_hook(name, *args)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 257

def invoke_hook(name, *args)
  @extensions.each { |e| e.send(name, *args) if e.respond_to?(name) }
end

#layout(name = :layout, &block)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 314

def layout(name = :layout, &block)
  template name, &block
end

#namespace_errors

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 269

def namespace_errors
  @errors
end

#not_found(&block)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 261

def not_found(&block)
  error(Sinatra::NotFound, &block)
end

#pattern

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 318

def pattern
  @pattern
end

#prefixed(method, pattern = nil, conditions = {}, &block) (private)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 347

def prefixed(method, pattern = nil, conditions = {}, &block)
  default = %r{(?:/.*)?} if (method == :before) || (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) (private)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 340

def prefixed_path(a, b)
  return a || b || /.*/ unless a && b
  return Mustermann.new(b) if a == /.*/

  Mustermann.new(a) + Mustermann.new(b)
end

#register(*extensions, &block)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 248

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)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 285

def respond_to(*args)
  return @conditions[:provides] || base.respond_to if args.empty?

  @conditions[:provides] = args
end

#respond_to?(method, include_private = false) ⇒ Boolean (private)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 359

def respond_to?(method, include_private = false)
  super || base.respond_to?(method, include_private)
end

#set(key, value = self, &block)

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 291

def set(key, value = self, &block)
  return key.each { |k, v| set(k, v) } if key.respond_to?(:each) && block.nil? && (value == self)
  raise ArgumentError, "may not set #{key}" unless ([:views] + ALLOWED_ENGINES).include?(key)

  block ||= proc { value }
  singleton_class.send(:define_method, key, &block)
end

#template(name, &block)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/namespace.rb', line 307

def template(name, &block)
  first_location = caller_locations.first
  filename = first_location.path
  line = first_location.lineno
  templates[name] = [block, filename, line]
end