class Sinatra::Bells

Constants

VERSION

Public Class Methods

set(option, *args, &block) click to toggle source
Calls superclass method
# File lib/sinatra/bells.rb, line 36
def set(option, *args, &block)
  block ? !args.empty? ?
    raise(ArgumentError, 'both block and arguments given') :
    set_defer(option, &block) : super(option, *args, &nil)
end
set_async(option, &block) click to toggle source
# File lib/sinatra/bells.rb, line 46
def set_async(option, &block)
  thread = Thread.new(&block)
  set_defer(option) { thread.value }
end
set_defer(option, &block) click to toggle source
# File lib/sinatra/bells.rb, line 42
def set_defer(option, &block)
  set(option, lambda { set(option, value = instance_eval(&block)); value })
end
set_hash(opt, ary, suf = nil, &block) click to toggle source
# File lib/sinatra/bells.rb, line 51
def set_hash(opt, ary, suf = nil, &block)
  (a, b = suf; ary.map! { |i| %W[#{i}#{a} #{i}#{b}] }) if suf
  ary.map!(&block) if block
  set(opt, Hash[ary])
end
set_root(file) click to toggle source
# File lib/sinatra/bells.rb, line 57
def set_root(file)
  set(:root, file.chomp(File.extname(file)))
end

Private Class Methods

route(verb, path, options = {}, &block) click to toggle source
Calls superclass method
# File lib/sinatra/bells.rb, line 63
def route(verb, path, options = {}, &block)
  return super unless render = options.delete(:render)

  if render.is_a?(Hash)
    template = render.delete(:html)
  else
    template, render = render, settings.default_render
  end

  blocks = {}

  type_paths(render, path) { |type_path, type, method|
    super(verb, type_path, options, &blocks[type] = lambda {
      content_type(type)
      instance_eval(&block)
      instance_eval(&method)
    })
  }

  super(verb, path, options.merge(provides: :html)) {
    instance_eval(&block)
    send(settings.default_renderer, template)
  }

  blocks.each { |type, type_block|
    super(verb, path, options.merge(provides: type), &type_block)
  }
end
type_paths(render, path) { |re ? regexp: type_path, type, method| ... } click to toggle source
# File lib/sinatra/bells.rb, line 92
def type_paths(render, path)
  path, re = path.source, path if path.is_a?(Regexp)
  anchor = $& if re && path.sub!(/\z|\$/i, '')

  dot = '.' unless path.end_with?('/')
  dot = Regexp.escape(dot) if dot && re

  render.each { |type, method|
    type_path = "#{path}#{dot}#{type}#{anchor}"
    yield re ? Regexp.new(type_path, re.options) : type_path, type, method
  }
end