Skip to content

Commit

Permalink
Tidy up for RDoc a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
rtomayko committed Feb 5, 2009
1 parent 4ab3b3b commit 3f513fa
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 45 deletions.
81 changes: 52 additions & 29 deletions lib/sinatra/base.rb
Expand Up @@ -7,6 +7,8 @@
module Sinatra
VERSION = '0.9.0.4'

# The request object. See Rack::Request for more info:
# http://rack.rubyforge.org/doc/classes/Rack/Request.html
class Request < Rack::Request
def user_agent
@env['HTTP_USER_AGENT']
Expand All @@ -24,6 +26,10 @@ def params
end
end

# The response object. See Rack::Response and Rack::ResponseHelpers for
# more info:
# http://rack.rubyforge.org/doc/classes/Rack/Response.html
# http://rack.rubyforge.org/doc/classes/Rack/Response/Helpers.html
class Response < Rack::Response
def initialize
@status, @body = 200, []
Expand Down Expand Up @@ -52,10 +58,11 @@ def finish
end
end

class NotFound < NameError # :)
class NotFound < NameError #:nodoc:
def code ; 404 ; end
end

# Methods available to routes, before filters, and views.
module Helpers
# Set or retrieve the response status code.
def status(value=nil)
Expand Down Expand Up @@ -201,8 +208,35 @@ def back ; request.referer ; end

end

# Template rendering methods. Each method takes a the name of a template
# to render as a Symbol and returns a String with the rendered output.
module Templates
def render(engine, template, options={})
def erb(template, options={})
require 'erb' unless defined? ::ERB
render :erb, template, options
end

def haml(template, options={})
require 'haml' unless defined? ::Haml
options[:options] ||= self.class.haml if self.class.respond_to? :haml
render :haml, template, options
end

def sass(template, options={}, &block)
require 'sass' unless defined? ::Sass
options[:layout] = false
render :sass, template, options
end

def builder(template=nil, options={}, &block)
require 'builder' unless defined? ::Builder
options, template = template, nil if template.is_a?(Hash)
template = lambda { block } if template.nil?
render :builder, template, options
end

private
def render(engine, template, options={}) #:nodoc:
data = lookup_template(engine, template, options)
output = __send__("render_#{engine}", template, data, options)
layout, data = lookup_layout(engine, options)
Expand Down Expand Up @@ -246,11 +280,6 @@ def template_path(engine, template, options={})
"#{views_dir}/#{template}.#{engine}"
end

def erb(template, options={})
require 'erb' unless defined? ::ERB
render :erb, template, options
end

def render_erb(template, data, options, &block)
data = data.call if data.kind_of? Proc
instance = ::ERB.new(data)
Expand All @@ -260,35 +289,16 @@ def render_erb(template, data, options, &block)
eval src, binding, '(__ERB__)', locals_assigns.length + 1
end

def haml(template, options={})
require 'haml' unless defined? ::Haml
options[:options] ||= self.class.haml if self.class.respond_to? :haml
render :haml, template, options
end

def render_haml(template, data, options, &block)
engine = ::Haml::Engine.new(data, options[:options] || {})
engine.render(self, options[:locals] || {}, &block)
end

def sass(template, options={}, &block)
require 'sass' unless defined? ::Sass
options[:layout] = false
render :sass, template, options
end

def render_sass(template, data, options, &block)
engine = ::Sass::Engine.new(data, options[:sass] || {})
engine.render
end

def builder(template=nil, options={}, &block)
require 'builder' unless defined? ::Builder
options, template = template, nil if template.is_a?(Hash)
template = lambda { block } if template.nil?
render :builder, template, options
end

def render_builder(template, data, options, &block)
xml = ::Builder::XmlMarkup.new(:indent => 2)
if data.respond_to?(:to_str)
Expand All @@ -300,6 +310,7 @@ def render_builder(template, data, options, &block)
end
end

# Base class for all Sinatra applications and middleware.
class Base
include Rack::Utils
include Helpers
Expand All @@ -312,6 +323,7 @@ def initialize(app=nil)
yield self if block_given?
end

# Rack call interface.
def call(env)
dup.call!(env)
end
Expand All @@ -333,15 +345,18 @@ def call!(env)
@response.finish
end

# Access options defined with Base.set.
def options
self.class
end

# Exit the current block and halt the response.
def halt(*response)
response = response.first if response.length == 1
throw :halt, response
end

# Pass control to the next matching route.
def pass
throw :pass
end
Expand Down Expand Up @@ -509,6 +524,7 @@ class << self
attr_accessor :routes, :filters, :conditions, :templates,
:middleware, :errors

public
def set(option, value=self)
if value.kind_of?(Proc)
metadef(option, &value)
Expand Down Expand Up @@ -585,6 +601,7 @@ def condition(&block)
@conditions << block
end

private
def host_name(pattern)
condition { pattern === request.host }
end
Expand Down Expand Up @@ -615,6 +632,7 @@ def accept_mime_types(types)
}
end

public
def get(path, opts={}, &block)
conditions = @conditions.dup
route('GET', path, opts, &block)
Expand Down Expand Up @@ -896,6 +914,7 @@ def metadef(message, &block)
end
end

# Base class for classic style (top-level) applications.
class Default < Base
set :raise_errors, false
set :dump_errors, true
Expand All @@ -905,17 +924,19 @@ class Default < Base
set :static, true
set :run, false

def self.register(*extensions, &block)
def self.register(*extensions, &block) #:nodoc:
added_methods = extensions.map {|m| m.public_instance_methods }.flatten
Delegator.delegate *added_methods
super(*extensions, &block)
end
end

# The top-level Application. All DSL methods executed on main are delegated
# to this class.
class Application < Default
end

module Delegator
module Delegator #:nodoc:
def self.delegate(*methods)
methods.each do |method_name|
eval <<-RUBY, binding, '(__DELEGATE__)', 1
Expand All @@ -939,16 +960,18 @@ def self.new(base=Base, options={}, &block)
base
end

# Extend the top-level DSL with the modules provided.
def self.register(*extensions, &block)
Default.register(*extensions, &block)
end

# Include the helper modules provided in Sinatra's request context.
def self.helpers(*extensions, &block)
Default.helpers(*extensions, &block)
end
end

class String
class String #:nodoc:
# Define String#each under 1.9 for Rack compatibility. This should be
# removed once Rack is fully 1.9 compatible.
alias_method :each, :each_line unless ''.respond_to? :each
Expand Down
34 changes: 18 additions & 16 deletions lib/sinatra/compat.rb
Expand Up @@ -9,7 +9,7 @@
require 'sinatra/main'

# Like Kernel#warn but outputs the location that triggered the warning.
def sinatra_warn(*message)
def sinatra_warn(*message) #:nodoc:
line = caller.
detect { |line| line !~ /(?:lib\/sinatra\/|__DELEGATE__)/ }.
sub(/:in .*/, '')
Expand All @@ -34,25 +34,27 @@ def sinatra_warn(*message)
# technically a Sinatra issue but many Sinatra apps access the old
# MIME_TYPES constants due to Sinatra example code.
require 'rack/file'
class Rack::File
def self.const_missing(const_name)
if const_name == :MIME_TYPES
hash = Hash.new { |hash,key| Rack::Mime::MIME_TYPES[".#{key}"] }
const_set :MIME_TYPES, hash
sinatra_warn 'Rack::File::MIME_TYPES is deprecated; use Rack::Mime instead.'
hash
else
super
module Rack #:nodoc:
class File #:nodoc:
def self.const_missing(const_name)
if const_name == :MIME_TYPES
hash = Hash.new { |hash,key| Rack::Mime::MIME_TYPES[".#{key}"] }
const_set :MIME_TYPES, hash
sinatra_warn 'Rack::File::MIME_TYPES is deprecated; use Rack::Mime instead.'
hash
else
super
end
end
end
end

module Sinatra
module Compat
module Compat #:nodoc:
end

# Make Sinatra::EventContext an alias for Sinatra::Default to unbreak plugins.
def self.const_missing(const_name)
def self.const_missing(const_name) #:nodoc:
if const_name == :EventContext
const_set :EventContext, Sinatra::Default
sinatra_warn 'Sinatra::EventContext is deprecated; use Sinatra::Default instead.'
Expand All @@ -73,7 +75,7 @@ def code ; 500 ; end
end

class Default < Base
def self.const_missing(const_name)
def self.const_missing(const_name) #:nodoc:
if const_name == :FORWARD_METHODS
sinatra_warn 'Sinatra::Application::FORWARD_METHODS is deprecated;',
'use Sinatra::Delegator::METHODS instead.'
Expand Down Expand Up @@ -107,7 +109,7 @@ def entity_tag(*args, &block)
# Throwing halt with a Symbol and the to_result convention are
# deprecated. Override the invoke method to detect those types of return
# values.
def invoke(&block)
def invoke(&block) #:nodoc:
res = super
case
when res.kind_of?(Symbol)
Expand All @@ -121,7 +123,7 @@ def invoke(&block)
res
end

def options
def options #:nodoc:
Options.new(self.class)
end

Expand Down Expand Up @@ -184,7 +186,7 @@ def env
end

# Deprecated. Missing messages are no longer delegated to @response.
def method_missing(name, *args, &b)
def method_missing(name, *args, &b) #:nodoc:
if @response.respond_to?(name)
sinatra_warn "The '#{name}' method is deprecated; use 'response.#{name}' instead."
@response.send(name, *args, &b)
Expand Down
2 changes: 2 additions & 0 deletions lib/sinatra/test.rb
Expand Up @@ -65,6 +65,8 @@ def respond_to?(symbol, include_private=false)
super || (@response && @response.respond_to?(symbol, include_private))
end

private

RACK_OPT_NAMES = {
:accept => "HTTP_ACCEPT",
:agent => "HTTP_USER_AGENT",
Expand Down

0 comments on commit 3f513fa

Please sign in to comment.