module Sinatra::Bind::ClassMethods

Constants

FILTER_NAMES

Public Instance Methods

on(path, options = {}) click to toggle source

Defines a route without block smoothly @param [String] path @option [String, Symbol] :to The method name that must be defined as instance method. @option [String, Symbol] :type The verb means REQUEST_METHOD, also can be received as filter names.

# File lib/sinatra/bind.rb, line 42
def on(path, options = {})
  verb = (options.delete(:type) || 'GET').to_s.upcase
  return bound_filter(verb, path, options) if FILTER_NAMES.include?(verb)
  method_name = options.delete(:to)
  process_bound_route(verb, path, method_name) do
    method = instance_method(method_name)
    bound_route(verb, path, options.merge(method_name: "#{verb} #{method_name}"), method)
  end
end

Private Instance Methods

bound_filter(type, path, options) click to toggle source
# File lib/sinatra/bind.rb, line 60
def bound_filter(type, path, options)
  path, options = //, path if path.respond_to?(:each_pair)
  method = instance_method(options.delete(:to))
  filters[type.downcase.to_sym] << compile_bound_route!(type, path || //, method, options)
end
bound_route(verb, path, options, method) click to toggle source
# File lib/sinatra/bind.rb, line 52
def bound_route(verb, path, options, method)
  host_name(options.delete(:host)) if options.key?(:host)
  signature = compile_bound_route!(verb, path, method, options)
  (@routes[verb] ||= []) << signature
  invoke_hook(:route_added, verb, path, method)
  signature
end
compile_bound_route!(verb, path, unbound_method, options) click to toggle source
# File lib/sinatra/bind.rb, line 66
def compile_bound_route!(verb, path, unbound_method, options)
  method_name = options.delete(:method_name)
  options.each_pair { |option, args| send(option, *args) }
  pattern, keys = compile path
  conditions, @conditions = @conditions, []
  wrapper = unbound_method.arity != 0 ?
    proc { |a,p| unbound_method.bind(a).call(*p) } :
    proc { |a,p| unbound_method.bind(a).call }
  wrapper.instance_variable_set(:@route_name, method_name)
  [ pattern, keys, conditions, wrapper ]
end
process_bound_route(verb, path, method_name) { || ... } click to toggle source
# File lib/sinatra/bind.rb, line 78
def process_bound_route(verb, path, method_name)
  return yield unless verb == 'GET'
  conditions = @conditions.dup; yield; @conditions = conditions
  on path, to: method_name, type: 'HEAD'
end