Skip to content

Commit

Permalink
Deprecation warnings for 0.3.x compat features (#52)
Browse files Browse the repository at this point in the history
While here, remove deprecation of the not_found class method
for registering a 404 error page.
  • Loading branch information
rtomayko committed Jan 11, 2009
1 parent 88afbb4 commit f41472f
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 44 deletions.
3 changes: 3 additions & 0 deletions compat/helper.rb
@@ -1,6 +1,9 @@
require 'rubygems'
require 'mocha'

# disable warnings in compat specs.
$VERBOSE = nil

$:.unshift File.dirname(File.dirname(__FILE__)) + "/lib"

ENV['RACK_ENV'] ||= 'test'
Expand Down
4 changes: 4 additions & 0 deletions lib/sinatra/base.rb
Expand Up @@ -469,6 +469,10 @@ def error(codes=Exception, &block)
end
end

def not_found(&block)
error 404, &block
end

def template(name, &block)
templates[name] = block
end
Expand Down
167 changes: 123 additions & 44 deletions lib/sinatra/compat.rb
@@ -1,107 +1,173 @@
# Sinatra 0.3.x compatibility module.
#
# The following code makes Sinatra 0.9.x compatible with Sinatra 0.3.x to
# ease the transition to the final 1.0 release. Everything defined in this
# file will be removed for the 1.0 release.

require 'ostruct'
require 'sinatra/base'
require 'sinatra/main'

# Deprecated. Do we still need this?
# Like Kernel#warn but outputs the location that triggered the warning.
def sinatra_warn(*message)
line = caller[1].sub(/:in .*/, '')
warn "#{line}: warning: #{message.join(' ')}"
end

# Rack now supports evented and swiftiplied mongrels through separate
# handler.
if ENV['SWIFT']
sinatra_warn 'the SWIFT environment variable is deprecated;',
'use Rack::Handler::SwiftipliedMongrel instead.'
require 'swiftcore/swiftiplied_mongrel'
puts "Using Swiftiplied Mongrel"
elsif ENV['EVENT']
sinatra_warn 'the EVENT environment variable is deprecated;',
'use Rack::Handler::EventedMongrel instead.'
require 'swiftcore/evented_mongrel'
puts "Using Evented Mongrel"
end

# Deprecated. Make Rack 0.9.0 backward compatibile with 0.4.0
# mime types
# Make Rack 0.9.0 backward compatibile with 0.4.0 mime types. This isn't
# 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
unless defined? MIME_TYPES
MIME_TYPES = Hash.new {|hash,key|
Rack::Mime::MIME_TYPES[".#{key}"] }
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

# Deprecated. Rack::Utils will not extend itself in the future. Sinatra::Base
# includes Rack::Utils, however.
module Rack::Utils ; extend self ; end

module Sinatra
module Compat
end

# Deprecated. Use: error
# The ServerError exception is deprecated. Any exception is considered an
# internal server error.
class ServerError < RuntimeError
def initialize(*args, &block)
sinatra_warn 'Sinatra::ServerError is deprecated;',
'use another exception, error, or Kernel#fail instead.'
end
def code ; 500 ; end
end

class Default < Base
# Deprecated.
FORWARD_METHODS = Sinatra::Delegator::METHODS
def self.const_missing(const_name)
if const_name == :FORWARD_METHODS
sinatra_warn 'Sinatra::Application::FORWARD_METHODS is deprecated;',
'use Sinatra::Delegator::METHODS instead.'
const_set :FORWARD_METHODS, Sinatra::Delegator::METHODS
Sinatra::Delegator::METHODS
else
super
end
end

# Deprecated. Use: response['Header-Name']
def headers(header=nil)
sinatra_warn "The 'headers' method is deprecated; use 'response' instead."
response.headers.merge!(header) if header
response.headers
end
alias :header :headers

# Deprecated. Use: halt
alias :stop :halt
def stop(*args, &block)
sinatra_warn "The 'stop' method is deprecated; use 'halt' instead."
halt(*args, &block)
end

# Deprecated. Use: etag
alias :entity_tag :etag
def entity_tag(*args, &block)
sinatra_warn "The 'entity_tag' method is deprecated; use 'etag' instead."
etag(*args, &block)
end

# The :disposition option is deprecated; use: #attachment. This method
# setting the Content-Transfer-Encoding header is deprecated.
#--
# TODO deprecation warning for :disposition argument.
def send_file(path, opts={})
opts[:disposition] = 'attachment' if !opts.key?(:disposition)
attachment opts[:filename] || path if opts[:filename] || opts[:disposition]
response['Content-Transfer-Encoding'] = 'binary' if opts[:disposition]
super(path, opts)
end

def options ; self.class.options ; end
def options
Options.new(self.class)
end

class Options < Struct.new(:target) #:nodoc:
def method_missing(name, *args, &block)
if target.respond_to?(name)
target.__send__(name, *args, &block)
elsif args.empty? && name.to_s !~ /=$/
sinatra_warn 'accessing undefined options will raise a NameError in Sinatra 1.0'
nil
else
super
end
end
end

class << self
# Deprecated. Options are stored directly on the class object.
def options ; Options.new(self) ; end

class Options < Struct.new(:target) #:nodoc:
def method_missing(name, *args, &block)
if target.respond_to?(name)
target.__send__(name, *args, &block)
elsif args.empty? && name.to_s !~ /=$/
nil
else
super
end
end
def options
sinatra_warn "The 'options' class method is deprecated; use 'self' instead."
Options.new(self)
end

# Deprecated. Use: configure
alias :configures :configure
def configures(*args, &block)
sinatra_warn "The 'configures' method is deprecated; use 'configure' instead."
configure(*args, &block)
end

# Deprecated. Use: set
def default_options
sinatra_warn "Sinatra::Application.default_options is deprecated; use 'set' instead."
fake = lambda { |options| set(options) }
def fake.merge!(options) ; call(options) ; end
fake
end

# Deprecated. Use: set
alias :set_option :set
alias :set_options :set
def set_option(*args, &block)
sinatra_warn "The 'set_option' method is deprecated; use 'set' instead."
set(*args, &block)
end

def set_options(*args, &block)
sinatra_warn "The 'set_options' method is deprecated; use 'set' instead."
set(*args, &block)
end

# Deprecated. Use: set :environment, ENV
def env=(value)
sinatra_warn "The :env option is deprecated; use :environment instead."
set :environment, value
end
alias :env :environment

# Deprecated. Use: options.environment
def env
sinatra_warn "The :env option is deprecated; use :environment instead."
environment
end
end

# Deprecated. Missing messages are no longer delegated to @response.
def method_missing(name, *args, &b)
if @response.respond_to?(name)
sinatra_warn "The '#{name}' method is deprecated; use 'response.#{name}' instead."
@response.send(name, *args, &b)
else
super
Expand All @@ -112,30 +178,43 @@ def method_missing(name, *args, &b)
class << self
# Deprecated. Use: Sinatra::Application
def application
sinatra_warn "Sinatra.application is deprecated; use Sinatra::Application instead."
Sinatra::Application
end

# Deprecated. Use: error 404
def not_found(&block)
error 404, &block
end

# Deprecated. Use: Sinatra::Application.reset!
def application=(value)
raise ArgumentError unless value.nil?
sinatra_warn "Setting Sinatra.application to nil is deprecated; create a new instance instead."
Sinatra.class_eval do
remove_const :Application
const_set :Application, Class.new(Sinatra::Default)
end
end

# Deprecated. Use: Sinatra::Application
alias :build_application :application
def build_application
sinatra_warn "Sinatra.build_application is deprecated; use Sinatra::Application instead."
Sinatra::Application
end

# Deprecated.
def options ; Sinatra::Application.options ; end
def port ; options.port ; end
def host ; options.host ; end
def env ; options.environment ; end
def options
sinatra_warn "Sinatra.options is deprecated; use Sinatra::Application.option_name instead."
Sinatra::Application.options
end

def port
sinatra_warn "Sinatra.port is deprecated; use Sinatra::Application.port instead."
options.port
end

def host
sinatra_warn "Sinatra.host is deprecated; use Sinatra::Application.host instead."
options.host
end

def env
sinatra_warn "Sinatra.env is deprecated; use Sinatra::Application.environment instead."
options.environment
end
end
end

0 comments on commit f41472f

Please sign in to comment.