This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
commit 4405ac5db172a40d533b71a0a57a3233aea13435
tree 55278af25b5cb51e26f3e5131585788af1b85a94
parent 247779ebe96100b49690581801a897636e72b36a
tree 55278af25b5cb51e26f3e5131585788af1b85a94
parent 247779ebe96100b49690581801a897636e72b36a
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 1 | require "optparse" | |
| 460780b9 » | Hampton Catlin | 2008-02-01 | 2 | ||
| 7dffeb41 » | ezmobius | 2008-01-13 | 3 | module Merb | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 4 | ||
| 7dffeb41 » | ezmobius | 2008-01-13 | 5 | class Config | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 6 | ||
| 7dffeb41 » | ezmobius | 2008-01-13 | 7 | class << self | |
| 460780b9 » | Hampton Catlin | 2008-02-01 | 8 | ||
| ecf073ff » | Janne Asmala | 2008-02-12 | 9 | # ==== Returns | |
| 10 | # Hash:: The defaults for the config. | ||||
| 7dffeb41 » | ezmobius | 2008-01-13 | 11 | def defaults | |
| 12 | @defaults ||= { | ||||
| 13 | :host => "0.0.0.0", | ||||
| 14 | :port => "4000", | ||||
| 40150311 » | ezmobius | 2008-02-26 | 15 | :adapter => "runner", | |
| cbece728 » | ezmobius | 2008-01-30 | 16 | :reload_classes => true, | |
| acb90a6e » | wycats | 2008-09-25 | 17 | :fork_for_class_load => !RUBY_PLATFORM.in?("windows", "java"), | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 18 | :environment => "development", | |
| 7dffeb41 » | ezmobius | 2008-01-13 | 19 | :merb_root => Dir.pwd, | |
| 20 | :use_mutex => true, | ||||
| 2f1555c5 » | fabien | 2008-03-10 | 21 | :log_delimiter => " ~ ", | |
| 22 | :log_auto_flush => false, | ||||
| c6337598 » | michaelklishin | 2008-07-20 | 23 | :log_level => :info, | |
| 0f9cd255 » | ezmobius | 2008-04-18 | 24 | :disabled_components => [], | |
| 304e19d1 » | michaelklishin | 2008-05-12 | 25 | :deferred_actions => [], | |
| 26 | :verbose => false | ||||
| 7dffeb41 » | ezmobius | 2008-01-13 | 27 | } | |
| 28 | end | ||||
| 460780b9 » | Hampton Catlin | 2008-02-01 | 29 | ||
| ecf073ff » | Janne Asmala | 2008-02-12 | 30 | # Yields the configuration. | |
| 31 | # | ||||
| 9a59da73 » | Janne Asmala | 2008-02-27 | 32 | # ==== Block parameters | |
| 33 | # c<Hash>:: The configuration parameters. | ||||
| 34 | # | ||||
| ecf073ff » | Janne Asmala | 2008-02-12 | 35 | # ==== Examples | |
| 36 | # Merb::Config.use do |config| | ||||
| 37 | # config[:exception_details] = false | ||||
| 38 | # end | ||||
| cbece728 » | ezmobius | 2008-01-30 | 39 | def use | |
| db8d5d70 » | ezmobius | 2008-02-02 | 40 | @configuration ||= {} | |
| cbece728 » | ezmobius | 2008-01-30 | 41 | yield @configuration | |
| 42 | end | ||||
| 460780b9 » | Hampton Catlin | 2008-02-01 | 43 | ||
| ecf073ff » | Janne Asmala | 2008-02-12 | 44 | # ==== Parameters | |
| 45 | # key<Object>:: The key to check. | ||||
| 46 | # | ||||
| 47 | # ==== Returns | ||||
| 48 | # Boolean:: True if the key exists in the config. | ||||
| 759f91df » | Yehuda Katz | 2008-02-02 | 49 | def key?(key) | |
| 50 | @configuration.key?(key) | ||||
| 51 | end | ||||
| 52 | |||||
| ecf073ff » | Janne Asmala | 2008-02-12 | 53 | # ==== Parameters | |
| 54 | # key<Object>:: The key to retrieve the parameter for. | ||||
| 55 | # | ||||
| 56 | # ==== Returns | ||||
| 57 | # Object:: The value of the configuration parameter. | ||||
| 7dffeb41 » | ezmobius | 2008-01-13 | 58 | def [](key) | |
| 59 | (@configuration||={})[key] | ||||
| 60 | end | ||||
| 460780b9 » | Hampton Catlin | 2008-02-01 | 61 | ||
| ecf073ff » | Janne Asmala | 2008-02-12 | 62 | # ==== Parameters | |
| 63 | # key<Object>:: The key to set the parameter for. | ||||
| 64 | # val<Object>:: The value of the parameter. | ||||
| 7dffeb41 » | ezmobius | 2008-01-13 | 65 | def []=(key,val) | |
| 66 | @configuration[key] = val | ||||
| 67 | end | ||||
| 460780b9 » | Hampton Catlin | 2008-02-01 | 68 | ||
| ecf073ff » | Janne Asmala | 2008-02-12 | 69 | # ==== Parameters | |
| 70 | # key<Object>:: The key of the parameter to delete. | ||||
| 7dffeb41 » | ezmobius | 2008-01-13 | 71 | def delete(key) | |
| ecf073ff » | Janne Asmala | 2008-02-12 | 72 | @configuration.delete(key) | |
| 7dffeb41 » | ezmobius | 2008-01-13 | 73 | end | |
| 460780b9 » | Hampton Catlin | 2008-02-01 | 74 | ||
| ecf073ff » | Janne Asmala | 2008-02-12 | 75 | # ==== Parameters | |
| 76 | # key<Object>:: The key to retrieve the parameter for. | ||||
| 77 | # default<Object>:: | ||||
| 78 | # The default value to return if the parameter is not set. | ||||
| 79 | # | ||||
| 80 | # ==== Returns | ||||
| 81 | # Object:: The value of the configuration parameter or the default. | ||||
| 7dffeb41 » | ezmobius | 2008-01-13 | 82 | def fetch(key, default) | |
| ecf073ff » | Janne Asmala | 2008-02-12 | 83 | @configuration.fetch(key, default) | |
| 7dffeb41 » | ezmobius | 2008-01-13 | 84 | end | |
| 460780b9 » | Hampton Catlin | 2008-02-01 | 85 | ||
| ecf073ff » | Janne Asmala | 2008-02-12 | 86 | # ==== Returns | |
| 87 | # Hash:: The config as a hash. | ||||
| f56ec87e » | ezmobius | 2008-01-21 | 88 | def to_hash | |
| 89 | @configuration | ||||
| 90 | end | ||||
| 460780b9 » | Hampton Catlin | 2008-02-01 | 91 | ||
| ecf073ff » | Janne Asmala | 2008-02-12 | 92 | # ==== Returns | |
| 93 | # String:: The config as YAML. | ||||
| 7dffeb41 » | ezmobius | 2008-01-13 | 94 | def to_yaml | |
| 4dd15027 » | wycats | 2008-08-19 | 95 | require "yaml" | |
| a397e370 » | michaelklishin | 2008-04-21 | 96 | @configuration.to_yaml | |
| 7dffeb41 » | ezmobius | 2008-01-13 | 97 | end | |
| 460780b9 » | Hampton Catlin | 2008-02-01 | 98 | ||
| ecf073ff » | Janne Asmala | 2008-02-12 | 99 | # Sets up the configuration by storing the given settings. | |
| 100 | # | ||||
| 101 | # ==== Parameters | ||||
| 102 | # settings<Hash>:: | ||||
| 103 | # Configuration settings to use. These are merged with the defaults. | ||||
| cc38bc8c » | ezmobius | 2008-01-31 | 104 | def setup(settings = {}) | |
| 105 | @configuration = defaults.merge(settings) | ||||
| f4fd0d39 » | wycats | 2008-09-25 | 106 | ||
| 107 | unless @configuration[:reload_classes] | ||||
| 108 | @configuration[:fork_for_class_load] = false | ||||
| 109 | end | ||||
| 110 | |||||
| 111 | @configuration | ||||
| 7dffeb41 » | ezmobius | 2008-01-13 | 112 | end | |
| 113 | |||||
| ecf073ff » | Janne Asmala | 2008-02-12 | 114 | # Parses the command line arguments and stores them in the config. | |
| 115 | # | ||||
| 116 | # ==== Parameters | ||||
| 117 | # argv<String>:: The command line arguments. Defaults to +ARGV+. | ||||
| 7dffeb41 » | ezmobius | 2008-01-13 | 118 | def parse_args(argv = ARGV) | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 119 | @configuration ||= {} | |
| 120 | # Our primary configuration hash for the length of this method | ||||
| 121 | options = {} | ||||
| 122 | |||||
| 123 | # Environment variables always win | ||||
| 124 | options[:environment] = ENV["MERB_ENV"] if ENV["MERB_ENV"] | ||||
| b38a93c8 » | fabien | 2008-09-07 | 125 | ||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 126 | # Build a parser for the command line arguments | |
| 127 | opts = OptionParser.new do |opts| | ||||
| 128 | opts.version = Merb::VERSION | ||||
| 129 | |||||
| 130 | opts.banner = "Usage: merb [uGdcIpPhmailLerkKX] [argument]" | ||||
| a397e370 » | michaelklishin | 2008-04-21 | 131 | opts.define_head "Merb. Pocket rocket web framework" | |
| 9ec4de21 » | wycats | 2008-09-23 | 132 | opts.separator '*' * 80 | |
| 133 | opts.separator "If no flags are given, Merb starts in the " \ | ||||
| 134 | "foreground on port 4000." | ||||
| 135 | opts.separator '*' * 80 | ||||
| 136 | |||||
| 137 | opts.on("-u", "--user USER", "This flag is for having merb run " \ | ||||
| 138 | "as a user other than the one currently logged in. Note: " \ | ||||
| 139 | "if you set this you must also provide a --group option " \ | ||||
| 140 | "for it to take effect.") do |user| | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 141 | options[:user] = user | |
| 142 | end | ||||
| 143 | |||||
| 9ec4de21 » | wycats | 2008-09-23 | 144 | opts.on("-G", "--group GROUP", "This flag is for having merb run " \ | |
| 145 | "as a group other than the one currently logged in. Note: " \ | ||||
| 146 | "if you set this you must also provide a --user option " \ | ||||
| 147 | "for it to take effect.") do |group| | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 148 | options[:group] = group | |
| 149 | end | ||||
| 150 | |||||
| 9ec4de21 » | wycats | 2008-09-23 | 151 | opts.on("-d", "--daemonize", "This will run a single merb in the " \ | |
| 152 | "background.") do |daemon| | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 153 | options[:daemonize] = true | |
| 154 | end | ||||
| 9ec4de21 » | wycats | 2008-09-23 | 155 | ||
| 156 | opts.on("-N", "--no-daemonize", "This will allow you to run a " \ | ||||
| 157 | "cluster in console mode") do |no_daemon| | ||||
| 158 | options[:daemonize] = false | ||||
| 159 | end | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 160 | ||
| 9ec4de21 » | wycats | 2008-09-23 | 161 | opts.on("-c", "--cluster-nodes NUM_MERBS", Integer, | |
| 162 | "Number of merb daemons to run.") do |nodes| | ||||
| 163 | options[:daemonize] = true unless options.key?(:daemonize) | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 164 | options[:cluster] = nodes | |
| 165 | end | ||||
| 166 | |||||
| 9ec4de21 » | wycats | 2008-09-23 | 167 | opts.on("-I", "--init-file FILE", "File to use for initialization " \ | |
| 168 | "on load, defaults to config/init.rb") do |init_file| | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 169 | options[:init_file] = init_file | |
| 170 | end | ||||
| 171 | |||||
| 9ec4de21 » | wycats | 2008-09-23 | 172 | opts.on("-p", "--port PORTNUM", Integer, "Port to run merb on, " \ | |
| 173 | "defaults to 4000.") do |port| | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 174 | options[:port] = port | |
| 175 | end | ||||
| 4d93d323 » | wayneeseguin | 2008-07-05 | 176 | ||
| 9ec4de21 » | wycats | 2008-09-23 | 177 | opts.on("-o", "--socket-file FILE", "Socket file to run merb on, " \ | |
| 178 | "defaults to [Merb.root]/log/merb.sock. This is for " \ | ||||
| 179 | "web servers, like thin, that use sockets.") do |port| | ||||
| 4d93d323 » | wayneeseguin | 2008-07-05 | 180 | options[:socket_file] = port | |
| 181 | end | ||||
| 182 | |||||
| 9ec4de21 » | wycats | 2008-09-23 | 183 | opts.on("-s", "--socket SOCKNUM", Integer, "Socket number to run " \ | |
| 184 | "merb on, defaults to 0.") do |port| | ||||
| 4d93d323 » | wayneeseguin | 2008-07-05 | 185 | options[:socket] = port | |
| 186 | end | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 187 | ||
| 9ec4de21 » | wycats | 2008-09-23 | 188 | opts.on("-P", "--pid PIDFILE", "PID file, defaults to " \ | |
| 189 | "[Merb.root]/log/merb.main.pid for the master process and" \ | ||||
| 190 | "[Merb.root]/log/merb.[port number].pid for worker " \ | ||||
| 191 | "processes. For clusters, use %s to specify where " \ | ||||
| 192 | "in the file merb should place the port number. For " \ | ||||
| 193 | "instance: -P myapp.%s.pid") do |pid_file| | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 194 | options[:pid_file] = pid_file | |
| 195 | end | ||||
| 196 | |||||
| 9ec4de21 » | wycats | 2008-09-23 | 197 | opts.on("-h", "--host HOSTNAME", "Host to bind to " \ | |
| 198 | "(default is 0.0.0.0).") do |host| | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 199 | options[:host] = host | |
| 200 | end | ||||
| 201 | |||||
| 9ec4de21 » | wycats | 2008-09-23 | 202 | opts.on("-m", "--merb-root /path/to/approot", "The path to the " \ | |
| 203 | "Merb.root for the app you want to run " \ | ||||
| 204 | "(default is current working directory).") do |root| | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 205 | options[:merb_root] = File.expand_path(root) | |
| 206 | end | ||||
| 207 | |||||
| 9ec4de21 » | wycats | 2008-09-23 | 208 | adapters = [:mongrel, :emongrel, :thin, :ebb, :fastcgi, :webrick] | |
| 209 | |||||
| a5b48f4b » | wycats | 2008-09-24 | 210 | opts.on("-a", "--adapter ADAPTER", | |
| 9ec4de21 » | wycats | 2008-09-23 | 211 | "The rack adapter to use to run merb (default is mongrel)" \ | |
| 212 | "[#{adapters.join(', ')}]") do |adapter| | ||||
| a5b48f4b » | wycats | 2008-09-24 | 213 | options[:adapter] ||= adapter | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 214 | end | |
| 215 | |||||
| 9ec4de21 » | wycats | 2008-09-23 | 216 | opts.on("-R", "--rackup FILE", "Load an alternate Rack config " \ | |
| 217 | "file (default is config/rack.rb)") do |rackup| | ||||
| ae200b7d » | gabriel | 2008-05-01 | 218 | options[:rackup] = rackup | |
| 219 | end | ||||
| 220 | |||||
| 9ec4de21 » | wycats | 2008-09-23 | 221 | opts.on("-i", "--irb-console", "This flag will start merb in " \ | |
| 222 | "irb console mode. All your models and other classes will " \ | ||||
| 223 | "be available for you in an irb session.") do |console| | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 224 | options[:adapter] = 'irb' | |
| 225 | end | ||||
| a397e370 » | michaelklishin | 2008-04-21 | 226 | ||
| 9ec4de21 » | wycats | 2008-09-23 | 227 | opts.on("-S", "--sandbox", "This flag will enable a sandboxed irb " \ | |
| 228 | "console. If your ORM supports transactions, all edits will " \ | ||||
| 229 | "be rolled back on exit.") do |sandbox| | ||||
| 0a15099a » | fabien | 2008-03-12 | 230 | options[:sandbox] = true | |
| 231 | end | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 232 | ||
| 9ec4de21 » | wycats | 2008-09-23 | 233 | opts.on("-l", "--log-level LEVEL", "Log levels can be set to any of " \ | |
| 234 | "these options: debug < info < warn < error < " \ | ||||
| 235 | "fatal (default is info)") do |log_level| | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 236 | options[:log_level] = log_level.to_sym | |
| 237 | end | ||||
| 238 | |||||
| 9ec4de21 » | wycats | 2008-09-23 | 239 | opts.on("-L", "--log LOGFILE", "A string representing the logfile to " \ | |
| 240 | "use. Defaults to [Merb.root]/log/merb.[main].log for the " \ | ||||
| 241 | "master process and [Merb.root]/log/merb[port number].log" \ | ||||
| 242 | "for worker processes") do |log_file| | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 243 | options[:log_file] = log_file | |
| 244 | end | ||||
| 245 | |||||
| 9ec4de21 » | wycats | 2008-09-23 | 246 | opts.on("-e", "--environment STRING", "Environment to run Merb " \ | |
| 247 | "under [development, production, testing] " \ | ||||
| 248 | "(default is development)") do |env| | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 249 | options[:environment] = env | |
| 250 | end | ||||
| 251 | |||||
| a397e370 » | michaelklishin | 2008-04-21 | 252 | opts.on("-r", "--script-runner ['RUBY CODE'| FULL_SCRIPT_PATH]", | |
| 9ec4de21 » | wycats | 2008-09-23 | 253 | "Command-line option to run scripts and/or code in the " \ | |
| 254 | "merb app.") do |code_or_file| | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 255 | options[:runner_code] = code_or_file | |
| 256 | options[:adapter] = 'runner' | ||||
| 257 | end | ||||
| 258 | |||||
| 9ec4de21 » | wycats | 2008-09-23 | 259 | opts.on("-K", "--graceful PORT or all", "Gracefully kill one " \ | |
| 260 | "merb proceses by port number. Use merb -K all to " \ | ||||
| 261 | "gracefully kill all merbs.") do |ports| | ||||
| 5a4589f5 » | gabriel | 2008-04-29 | 262 | options[:action] = :kill | |
| 67e1756c » | wycats | 2008-09-27 | 263 | ports = "main" if ports == "all" | |
| 5a4589f5 » | gabriel | 2008-04-29 | 264 | options[:port] = ports | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 265 | end | |
| 266 | |||||
| 9ec4de21 » | wycats | 2008-09-23 | 267 | opts.on("-k", "--kill PORT", "Force kill one merb worker " \ | |
| 268 | "by port number. This will cause the worker to" \ | ||||
| 269 | "be respawned. If you want to kill ") do |port| | ||||
| 5a4589f5 » | gabriel | 2008-04-29 | 270 | options[:action] = :kill_9 | |
| 67e1756c » | wycats | 2008-09-27 | 271 | port = "main" if port == "all" | |
| 5a4589f5 » | gabriel | 2008-04-29 | 272 | options[:port] = port | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 273 | end | |
| 4405ac5d » | wycats | 2008-09-27 | 274 | ||
| 275 | opts.on("--fast-deploy", "Reload the code, but not your" \ | ||||
| 276 | "init.rb or gems") do | ||||
| 277 | options[:action] = :fast_deploy | ||||
| 278 | end | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 279 | ||
| 9ec4de21 » | wycats | 2008-09-23 | 280 | # @todo Do we really need this flag? It seems unlikely to want to | |
| 281 | # change the mutex from the command-line. | ||||
| 282 | opts.on("-X", "--mutex on/off", "This flag is for turning the " \ | ||||
| 283 | "mutex lock on and off.") do |mutex| | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 284 | if mutex == "off" | |
| 285 | options[:use_mutex] = false | ||||
| 286 | else | ||||
| 287 | options[:use_mutex] = true | ||||
| a397e370 » | michaelklishin | 2008-04-21 | 288 | end | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 289 | end | |
| 290 | |||||
| 291 | opts.on("-D", "--debugger", "Run merb using rDebug.") do | ||||
| 292 | begin | ||||
| 293 | require "ruby-debug" | ||||
| 7dffeb41 » | ezmobius | 2008-01-13 | 294 | Debugger.start | |
| 9ec4de21 » | wycats | 2008-09-23 | 295 | if Debugger.respond_to?(:settings) | |
| 296 | Debugger.settings[:autoeval] = true | ||||
| 297 | end | ||||
| 7dffeb41 » | ezmobius | 2008-01-13 | 298 | puts "Debugger enabled" | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 299 | rescue LoadError | |
| 9ec4de21 » | wycats | 2008-09-23 | 300 | puts "You need to install ruby-debug to run the server in " \ | |
| 301 | "debugging mode. With gems, use `gem install ruby-debug'" | ||||
| 7dffeb41 » | ezmobius | 2008-01-13 | 302 | exit | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 303 | end | |
| 304 | end | ||||
| 305 | |||||
| 304e19d1 » | michaelklishin | 2008-05-12 | 306 | opts.on("-V", "--verbose", "Print extra information") do | |
| 307 | options[:verbose] = true | ||||
| 308 | end | ||||
| 1a72840a » | benburkert | 2008-09-07 | 309 | ||
| 310 | opts.on("-C", "--console-trap", "Enter an irb console on ^C") do | ||||
| 311 | options[:console_trap] = true | ||||
| 312 | end | ||||
| 313 | |||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 314 | opts.on("-?", "-H", "--help", "Show this help message") do | |
| a397e370 » | michaelklishin | 2008-04-21 | 315 | puts opts | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 316 | exit | |
| 317 | end | ||||
| 318 | end | ||||
| 319 | |||||
| 320 | # Parse what we have on the command line | ||||
| e5937d5a » | wycats | 2008-09-25 | 321 | begin | |
| 322 | opts.parse!(argv) | ||||
| 323 | rescue OptionParser::InvalidOption => e | ||||
| 324 | Merb.fatal! e.message, e | ||||
| 325 | end | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 326 | Merb::Config.setup(options) | |
| 327 | end | ||||
| 328 | |||||
| 0ff80a07 » | ezmobius | 2008-05-12 | 329 | attr_accessor :configuration | |
| 4cd91a55 » | Janne Asmala | 2008-02-25 | 330 | ||
| 331 | # Set configuration parameters from a code block, where each method | ||||
| 332 | # evaluates to a config parameter. | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 333 | # | |
| 334 | # ==== Parameters | ||||
| 9a59da73 » | Janne Asmala | 2008-02-27 | 335 | # &block:: Configuration parameter block. | |
| 4cd91a55 » | Janne Asmala | 2008-02-25 | 336 | # | |
| 337 | # ==== Examples | ||||
| 338 | # # Set environment and log level. | ||||
| 339 | # Merb::Config.configure do | ||||
| 340 | # environment "development" | ||||
| 341 | # log_level "debug" | ||||
| 342 | # end | ||||
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 343 | def configure(&block) | |
| 344 | ConfigBlock.new(self, &block) if block_given? | ||||
| 345 | end | ||||
| a397e370 » | michaelklishin | 2008-04-21 | 346 | ||
| f1f5a625 » | wayneeseguin | 2008-02-25 | 347 | # Allows retrieval of single key config values via Merb.config.<key> | |
| 348 | # Allows single key assignment via Merb.config.<key> = ... | ||||
| 349 | # | ||||
| 350 | # ==== Parameters | ||||
| 9a59da73 » | Janne Asmala | 2008-02-27 | 351 | # method<~to_s>:: Method name as hash key value. | |
| 352 | # *args:: Value to set the configuration parameter to. | ||||
| 0ff80a07 » | ezmobius | 2008-05-12 | 353 | def method_missing(method, *args) | |
| 905dbe19 » | wayneeseguin | 2008-02-25 | 354 | if method.to_s[-1,1] == '=' | |
| 355 | @configuration[method.to_s.tr('=','').to_sym] = *args | ||||
| 356 | else | ||||
| 357 | @configuration[method] | ||||
| 358 | end | ||||
| 733ae866 » | wayneeseguin | 2008-02-24 | 359 | end | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 360 | ||
| 7dffeb41 » | ezmobius | 2008-01-13 | 361 | end # class << self | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 362 | ||
| 0ff80a07 » | ezmobius | 2008-05-12 | 363 | class ConfigBlock | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 364 | ||
| 0ff80a07 » | ezmobius | 2008-05-12 | 365 | def initialize(klass, &block) | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 366 | @klass = klass | |
| 367 | instance_eval(&block) | ||||
| 368 | end | ||||
| 369 | |||||
| 0ff80a07 » | ezmobius | 2008-05-12 | 370 | def method_missing(method, *args) | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 371 | @klass[method] = *args | |
| 372 | end | ||||
| 373 | |||||
| 374 | end # class Configurator | ||||
| 375 | |||||
| 7dffeb41 » | ezmobius | 2008-01-13 | 376 | end # Config | |
| 40f99fc8 » | wayneeseguin | 2008-02-24 | 377 | ||
| 378 | end # Merb | ||||








