public
Fork of bmizerany/sinatra
Description: Classy web-development dressed in a DSL
Homepage: http://sinatrarb.com
Clone URL: git://github.com/JackDanger/sinatra.git
Refactor options / configures into Sinatra::Application

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.
rtomayko (author)
Mon Apr 14 16:59:31 -0700 2008
commit  38588af109a9ad0cfc422ae6e2cce3593445f07f
tree    56dd015b0576afb4be46f1a37ebc75c1e99619cf
parent  da3439af026a76d008d7cd49206cb1081da7b316
...
849
850
851
852
853
854
855
856
857
858
859
860
 
 
 
861
862
863
...
865
866
867
 
868
869
 
 
 
 
 
 
 
 
870
 
871
872
 
873
874
875
...
879
880
881
882
883
884
885
 
 
886
887
888
889
890
 
 
 
891
892
 
893
894
895
...
907
908
909
 
910
911
912
913
914
915
916
 
917
918
919
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
920
921
922
...
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
...
1052
1053
1054
1055
 
1056
1057
1058
...
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
...
849
850
851
 
 
 
852
853
854
855
 
 
856
857
858
859
860
861
...
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
 
880
881
882
883
...
887
888
889
 
 
 
 
890
891
892
893
 
 
 
894
895
896
897
 
898
899
900
901
...
913
914
915
916
917
918
919
920
921
922
 
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
...
1063
1064
1065
 
 
 
 
1066
1067
1068
...
1075
1076
1077
 
1078
1079
1080
1081
...
1259
1260
1261
 
 
 
 
 
 
 
1262
1263
 
1264
1265
1266
0
@@ -849,15 +849,13 @@ module Sinatra
0
     # handlers as values.
0
     attr_reader :filters
0
 
0
- # Truthful when the application is in the process of being reloaded.
0
- attr_reader :reloading
0
-
0
     # Array of objects to clear during reload. The objects in this array
0
     # must respond to :clear.
0
     attr_reader :clearables
0
 
0
- # Application options.
0
- attr_accessor :options
0
+ # Object including open attribute methods for modifying Application
0
+ # configuration.
0
+ attr_reader :options
0
 
0
     # List of methods available from top-level scope. When invoked from
0
     # top-level the method is forwarded to the default application
0
@@ -865,11 +863,21 @@ module Sinatra
0
     FORWARD_METHODS = %w[
0
       get put post delete head
0
       template layout before error not_found
0
+ configures configure
0
     ]
0
 
0
+ # Hash of default application configuration options. When a new
0
+ # Application is created, the #options object takes its initial values
0
+ # from here.
0
+ #
0
+ # Changes to the default_options Hash effect only Application objects
0
+ # created after the changes are made. For this reason, modifications to
0
+ # the default_options Hash typically occur at the very beginning of a
0
+ # file, before any DSL related functions are invoked.
0
     def self.default_options
0
+ return @default_options unless @default_options.nil?
0
       root = File.expand_path(File.dirname($0))
0
- @@default_options ||= {
0
+ @default_options = {
0
         :run => true,
0
         :port => 4567,
0
         :env => :development,
0
@@ -879,17 +887,15 @@ module Sinatra
0
         :sessions => false,
0
         :logging => true
0
       }
0
- end
0
-
0
- def default_options
0
- self.class.default_options
0
+ load_default_options_from_command_line!
0
+ @default_options
0
     end
0
 
0
-
0
- ##
0
- # Load all options given on the command line
0
+ # Search ARGV for command line arguments and update the
0
+ # Sinatra::default_options Hash accordingly. This method is
0
+ # invoked the first time the default_options Hash is accessed.
0
     # NOTE: Ignores --name so unit/spec tests can run individually
0
- def load_options!
0
+ def self.load_default_options_from_command_line! #:nodoc:
0
       require 'optparse'
0
       OptionParser.new do |op|
0
         op.on('-p port') { |port| default_options[:port] = port }
0
@@ -907,16 +913,37 @@ module Sinatra
0
     end
0
 
0
     def initialize
0
+ @reloading = false
0
       @clearables = [
0
         @events = Hash.new { |hash, key| hash[key] = [] },
0
         @errors = Hash.new,
0
         @filters = Hash.new { |hash, key| hash[key] = [] },
0
         @templates = Hash.new
0
       ]
0
- load_options!
0
+ @options = OpenStruct.new(self.class.default_options)
0
       load_default_events!
0
     end
0
 
0
+ # Determine whether the application is in the process of being
0
+ # reloaded.
0
+ def reloading?
0
+ @reloading == true
0
+ end
0
+
0
+ # Yield to the block for configuration if the current environment
0
+ # matches any included in the +envs+ list. Always yield to the block
0
+ # when no environment is specified.
0
+ #
0
+ # NOTE: configuration blocks are not executed during reloads.
0
+ def configures(*envs, &b)
0
+ return if reloading?
0
+ return unless envs.empty? || envs.include?(options.env)
0
+ yield self
0
+ end
0
+
0
+ alias :configure :configures
0
+
0
+
0
     # Define an event handler for the given request method and path
0
     # spec. The block is executed when a request matches the method
0
     # and spec.
0
@@ -1036,10 +1063,6 @@ module Sinatra
0
         errors[NotFound].invoke(request)
0
     end
0
 
0
- def options
0
- @options ||= OpenStruct.new(default_options)
0
- end
0
-
0
     def development?
0
       options.env == :development
0
     end
0
@@ -1052,7 +1075,7 @@ module Sinatra
0
       @reloading = false
0
       Environment.setup!
0
     end
0
-
0
+
0
     def mutex
0
       @@mutex ||= Mutex.new
0
     end
0
@@ -1236,16 +1259,8 @@ def use_in_file_templates!
0
   end
0
 end
0
 
0
-def configures(*envs, &b)
0
- yield if !Sinatra.application.reloading &&
0
- (envs.include?(Sinatra.application.options.env) ||
0
- envs.empty?)
0
-end
0
-alias :configure :configures
0
-
0
 def set_options(opts)
0
   Sinatra::Application.default_options.merge!(opts)
0
- Sinatra.application.options = nil
0
 end
0
 
0
 def set_option(key, value)
...
10
11
12
13
 
...
10
11
12
 
13
0
@@ -10,4 +10,4 @@ Sinatra::Application.default_options.merge!(
0
   :logging => false
0
 )
0
 
0
-Sinatra.application.options = nil
0
+Sinatra.application = nil
...
97
98
99
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
102
103
...
97
98
99
 
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
0
@@ -97,7 +97,38 @@ context "An app returns" do
0
   end
0
   
0
 end
0
-
0
+
0
+context "Application#configure blocks" do
0
+
0
+ setup do
0
+ Sinatra.application = nil
0
+ end
0
+
0
+ specify "run when no environment specified" do
0
+ ref = false
0
+ configure { ref = true }
0
+ ref.should.equal true
0
+ end
0
+
0
+ specify "run when matching environment specified" do
0
+ ref = false
0
+ configure(:test) { ref = true }
0
+ ref.should.equal true
0
+ end
0
+
0
+ specify "do not run when no matching environment specified" do
0
+ configure(:foo) { flunk "block should not have been executed" }
0
+ configure(:development, :production, :foo) { flunk "block should not have been executed" }
0
+ end
0
+
0
+ specify "accept multiple environments" do
0
+ ref = false
0
+ configure(:foo, :test, :bar) { ref = true }
0
+ ref.should.equal true
0
+ end
0
+
0
+end
0
+
0
 context "Events in an app" do
0
   
0
   setup do

Comments

    No one has commented yet.