Skip to content

Commit

Permalink
Refactor options / configures into Sinatra::Application
Browse files Browse the repository at this point in the history
This is a grab-bag of minor configuration related refactorings:

  * Moved the top-level #configures into Sinatra::Application and delegate from
    top-level to the default application.

  * Modified default options loading to parse command line arguments
    the first time Application::default_options is accessed only.

  * Removed the ability to reload default_options by setting an application's
    options to nil. We can add this back in fairly easily but it seems to lead
    to the bad practice of modifying default_options and then reloading instead
    of modifying the application options directly.
  • Loading branch information
rtomayko committed Apr 28, 2008
1 parent da3439a commit 38588af
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 30 deletions.
71 changes: 43 additions & 28 deletions lib/sinatra.rb
Original file line number Diff line number Diff line change
Expand Up @@ -849,27 +849,35 @@ class Application
# handlers as values.
attr_reader :filters

# Truthful when the application is in the process of being reloaded.
attr_reader :reloading

# Array of objects to clear during reload. The objects in this array
# must respond to :clear.
attr_reader :clearables

# Application options.
attr_accessor :options
# Object including open attribute methods for modifying Application
# configuration.
attr_reader :options

# List of methods available from top-level scope. When invoked from
# top-level the method is forwarded to the default application
# (Sinatra::application).
FORWARD_METHODS = %w[
get put post delete head
template layout before error not_found
configures configure
]

# Hash of default application configuration options. When a new
# Application is created, the #options object takes its initial values
# from here.
#
# Changes to the default_options Hash effect only Application objects
# created after the changes are made. For this reason, modifications to
# the default_options Hash typically occur at the very beginning of a
# file, before any DSL related functions are invoked.
def self.default_options
return @default_options unless @default_options.nil?
root = File.expand_path(File.dirname($0))
@@default_options ||= {
@default_options = {
:run => true,
:port => 4567,
:env => :development,
Expand All @@ -879,17 +887,15 @@ def self.default_options
:sessions => false,
:logging => true
}
end

def default_options
self.class.default_options
load_default_options_from_command_line!
@default_options
end


##
# Load all options given on the command line
# Search ARGV for command line arguments and update the
# Sinatra::default_options Hash accordingly. This method is
# invoked the first time the default_options Hash is accessed.
# NOTE: Ignores --name so unit/spec tests can run individually
def load_options!
def self.load_default_options_from_command_line! #:nodoc:
require 'optparse'
OptionParser.new do |op|
op.on('-p port') { |port| default_options[:port] = port }
Expand All @@ -907,16 +913,37 @@ def load_default_events!
end

def initialize
@reloading = false
@clearables = [
@events = Hash.new { |hash, key| hash[key] = [] },
@errors = Hash.new,
@filters = Hash.new { |hash, key| hash[key] = [] },
@templates = Hash.new
]
load_options!
@options = OpenStruct.new(self.class.default_options)
load_default_events!
end

# Determine whether the application is in the process of being
# reloaded.
def reloading?
@reloading == true
end

# Yield to the block for configuration if the current environment
# matches any included in the +envs+ list. Always yield to the block
# when no environment is specified.
#
# NOTE: configuration blocks are not executed during reloads.
def configures(*envs, &b)
return if reloading?
return unless envs.empty? || envs.include?(options.env)
yield self
end

alias :configure :configures


# Define an event handler for the given request method and path
# spec. The block is executed when a request matches the method
# and spec.
Expand Down Expand Up @@ -1036,10 +1063,6 @@ def lookup(request)
errors[NotFound].invoke(request)
end

def options
@options ||= OpenStruct.new(default_options)
end

def development?
options.env == :development
end
Expand All @@ -1052,7 +1075,7 @@ def reload!
@reloading = false
Environment.setup!
end

def mutex
@@mutex ||= Mutex.new
end
Expand Down Expand Up @@ -1236,16 +1259,8 @@ def use_in_file_templates!
end
end

def configures(*envs, &b)
yield if !Sinatra.application.reloading &&
(envs.include?(Sinatra.application.options.env) ||
envs.empty?)
end
alias :configure :configures

def set_options(opts)
Sinatra::Application.default_options.merge!(opts)
Sinatra.application.options = nil
end

def set_option(key, value)
Expand Down
2 changes: 1 addition & 1 deletion lib/sinatra/test/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
:logging => false
)

Sinatra.application.options = nil
Sinatra.application = nil
33 changes: 32 additions & 1 deletion test/application_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,38 @@ def each
end

end


context "Application#configure blocks" do

setup do
Sinatra.application = nil
end

specify "run when no environment specified" do
ref = false
configure { ref = true }
ref.should.equal true
end

specify "run when matching environment specified" do
ref = false
configure(:test) { ref = true }
ref.should.equal true
end

specify "do not run when no matching environment specified" do
configure(:foo) { flunk "block should not have been executed" }
configure(:development, :production, :foo) { flunk "block should not have been executed" }
end

specify "accept multiple environments" do
ref = false
configure(:foo, :test, :bar) { ref = true }
ref.should.equal true
end

end

context "Events in an app" do

setup do
Expand Down

0 comments on commit 38588af

Please sign in to comment.