Class Merb::Config
In: merb-core/lib/merb-core/config.rb
merb-core/lib/merb-core/dispatch/session.rb
Parent: Object

Methods

[]   []=   configure   defaults   delete   fetch   key?   method_missing   parse_args   session_stores   setup   to_hash   to_yaml   use  

Classes and Modules

Class Merb::Config::ConfigBlock

Attributes

configuration  [RW]  :api: private

Public Class methods

Retrieve the value of a config entry.

Parameters

key<Object>:The key to retrieve the parameter for.

Returns

Object:The value of the configuration parameter.

:api: public

[Source]

    # File merb-core/lib/merb-core/config.rb, line 79
79:       def [](key)
80:         (@configuration ||= setup)[key]
81:       end

Set the value of a config entry.

Parameters

key<Object>:The key to set the parameter for.
val<Object>:The value of the parameter.

:api: public

[Source]

    # File merb-core/lib/merb-core/config.rb, line 90
90:       def []=(key, val)
91:         (@configuration ||= setup)[key] = val
92:       end

Set configuration parameters from a code block, where each method evaluates to a config parameter.

Parameters

&block:Configuration parameter block.

Examples

  # Set environment and log level.
  Merb::Config.configure do
    environment "development"
    log_level   "debug"
    log_file    Merb.root / "log" / "special.log"
  end

Returns

nil

:api: public

[Source]

     # File merb-core/lib/merb-core/config.rb, line 428
428:       def configure(&block)
429:         ConfigBlock.new(self, &block) if block_given?
430:         nil
431:       end

Returns the hash of default config values for Merb.

Returns

Hash:The defaults for the config.

:api: private

[Source]

    # File merb-core/lib/merb-core/config.rb, line 15
15:       def defaults
16:         @defaults ||= {
17:           :host                   => "0.0.0.0",
18:           :port                   => "4000",
19:           :adapter                => "runner",
20:           :reload_classes         => true,
21:           :fork_for_class_load    => Merb.forking_environment?,
22:           :environment            => "development",
23:           :merb_root              => Dir.pwd,
24:           :use_mutex              => true,
25:           :log_delimiter          => " ~ ",
26:           :log_auto_flush         => false,
27:           :log_level              => :info,
28:           :log_stream             => STDOUT,
29:           :disabled_components    => Merb.on_windows? ? [:signals] : [],
30:           :deferred_actions       => [],
31:           :verbose                => false,
32:           :name                   => "merb"
33:         }
34:       end

Remove the value of a config entry.

Parameters

key<Object>:The key of the parameter to delete.

Returns

Object:The value of the removed entry.

:api: public

[Source]

     # File merb-core/lib/merb-core/config.rb, line 103
103:       def delete(key)
104:         @configuration.delete(key)
105:       end

Retrieve the value of a config entry, returning the provided default if the key is not present

Parameters

key<Object>:The key to retrieve the parameter for.
default<Object>:The default value to return if the parameter is not set.

Returns

Object:The value of the configuration parameter or the default.

:api: public

[Source]

     # File merb-core/lib/merb-core/config.rb, line 118
118:       def fetch(key, default)
119:         @configuration.fetch(key, default)
120:       end

Detects whether the provided key is in the config.

Parameters

key<Object>:The key to check.

Returns

Boolean:True if the key exists in the config.

:api: public

[Source]

    # File merb-core/lib/merb-core/config.rb, line 66
66:       def key?(key)
67:         @configuration.key?(key)
68:       end

Allows retrieval of single key config values via Merb.config.<key> Allows single key assignment via Merb.config.<key> = …

Parameters

method<~to_s>:Method name as hash key value.
*args:Value to set the configuration parameter to.

Returns

The value of the entry fetched or assigned to.

:api: public

[Source]

     # File merb-core/lib/merb-core/config.rb, line 444
444:       def method_missing(method, *args)
445:         if method.to_s[-1,1] == '='
446:           @configuration[method.to_s.tr('=','').to_sym] = *args
447:         else
448:           @configuration[method]
449:         end
450:       end

Parses the command line arguments and stores them in the config.

Parameters

argv<String>:The command line arguments. Defaults to ARGV.

Returns

The configuration as a hash.

:api: private

[Source]

     # File merb-core/lib/merb-core/config.rb, line 181
181:       def parse_args(argv = ARGV)
182:         @configuration ||= {}
183:         # Our primary configuration hash for the length of this method
184:         options = {}
185: 
186:         # Environment variables always win
187:         options[:environment] = ENV["MERB_ENV"] if ENV["MERB_ENV"]
188:         
189:         # Build a parser for the command line arguments
190:         opts = OptionParser.new do |opts|
191:           opts.version = Merb::VERSION
192: 
193:           opts.banner = "Usage: merb [uGdcIpPhmailLerkKX] [argument]"
194:           opts.define_head "Merb. Pocket rocket web framework"
195:           opts.separator '*' * 80
196:           opts.separator "If no flags are given, Merb starts in the " \
197:             "foreground on port 4000."
198:           opts.separator '*' * 80
199: 
200:           opts.on("-u", "--user USER", "This flag is for having merb run " \
201:                   "as a user other than the one currently logged in. Note: " \
202:                   "if you set this you must also provide a --group option " \
203:                   "for it to take effect.") do |user|
204:             options[:user] = user
205:           end
206: 
207:           opts.on("-G", "--group GROUP", "This flag is for having merb run " \
208:                   "as a group other than the one currently logged in. Note: " \
209:                   "if you set this you must also provide a --user option " \
210:                   "for it to take effect.") do |group|
211:             options[:group] = group
212:           end
213: 
214:           opts.on("-d", "--daemonize", "This will run a single merb in the " \
215:                   "background.") do |daemon|
216:             options[:daemonize] = true
217:           end
218:           
219:           opts.on("-N", "--no-daemonize", "This will allow you to run a " \
220:                   "cluster in console mode") do |no_daemon|
221:             options[:daemonize] = false
222:           end
223: 
224:           opts.on("-c", "--cluster-nodes NUM_MERBS", Integer, 
225:                   "Number of merb daemons to run.") do |nodes|
226:             options[:daemonize] = true unless options.key?(:daemonize)
227:             options[:cluster] = nodes
228:           end
229: 
230:           opts.on("-I", "--init-file FILE", "File to use for initialization " \
231:                   "on load, defaults to config/init.rb") do |init_file|
232:             options[:init_file] = init_file
233:           end
234: 
235:           opts.on("-p", "--port PORTNUM", Integer, "Port to run merb on, " \
236:                   "defaults to 4000.") do |port|
237:             options[:port] = port
238:           end
239: 
240:           opts.on("-o", "--socket-file FILE", "Socket file to run merb on, " \
241:                   "defaults to [Merb.root]/log/merb.sock. This is for " \
242:                   "web servers, like thin, that use sockets." \
243:                   "Specify this *only* if you *must*.") do |port|
244:             options[:socket_file] = port
245:           end
246: 
247:           opts.on("-s", "--socket SOCKNUM", Integer, "Socket number to run " \
248:                   "merb on, defaults to 0.") do |port|
249:             options[:socket] = port
250:           end
251: 
252:           opts.on("-n", "--name NAME", String, "Set the name of the application. "\
253:                   "This is used in the process title and log file names.") do |name|
254:             options[:name] = name
255:           end
256: 
257:           opts.on("-P", "--pid PIDFILE", "PID file, defaults to " \
258:                   "[Merb.root]/log/merb.main.pid for the master process and" \
259:                   "[Merb.root]/log/merb.[port number].pid for worker " \
260:                   "processes. For clusters, use %s to specify where " \
261:                   "in the file merb should place the port number. For " \
262:                   "instance: -P myapp.%s.pid") do |pid_file|
263:             options[:pid_file] = pid_file
264:           end
265: 
266:           opts.on("-h", "--host HOSTNAME", "Host to bind to " \
267:                   "(default is 0.0.0.0).") do |host|
268:             options[:host] = host
269:           end
270: 
271:           opts.on("-m", "--merb-root /path/to/approot", "The path to the " \
272:                   "Merb.root for the app you want to run " \
273:                   "(default is current working directory).") do |root|
274:             options[:merb_root] = File.expand_path(root)
275:           end
276: 
277:           adapters = [:mongrel, :emongrel, :thin, :ebb, :fastcgi, :webrick]
278: 
279:           opts.on("-a", "--adapter ADAPTER",
280:                   "The rack adapter to use to run merb (default is mongrel)" \
281:                   "[#{adapters.join(', ')}]") do |adapter|
282:             options[:adapter] ||= adapter
283:           end
284: 
285:           opts.on("-R", "--rackup FILE", "Load an alternate Rack config " \
286:                   "file (default is config/rack.rb)") do |rackup|
287:             options[:rackup] = rackup
288:           end
289: 
290:           opts.on("-i", "--irb-console", "This flag will start merb in " \
291:                   "irb console mode. All your models and other classes will " \
292:                   "be available for you in an irb session.") do |console|
293:             options[:adapter] = 'irb'
294:           end
295: 
296:           opts.on("-S", "--sandbox", "This flag will enable a sandboxed irb " \
297:                   "console. If your ORM supports transactions, all edits will " \
298:                   "be rolled back on exit.") do |sandbox|
299:             options[:sandbox] = true
300:           end
301: 
302:           opts.on("-l", "--log-level LEVEL", "Log levels can be set to any of " \
303:                   "these options: debug < info < warn < error < " \
304:                   "fatal (default is info)") do |log_level|
305:             options[:log_level] = log_level.to_sym
306:             options[:force_logging] = true
307:           end
308: 
309:           opts.on("-L", "--log LOGFILE", "A string representing the logfile to " \
310:                   "use. Defaults to [Merb.root]/log/merb.[main].log for the " \
311:                   "master process and [Merb.root]/log/merb[port number].log" \
312:                   "for worker processes") do |log_file|
313:             options[:log_file] = log_file
314:             options[:force_logging] = true
315:           end
316: 
317:           opts.on("-e", "--environment STRING", "Environment to run Merb " \
318:                   "under [development, production, testing] " \
319:                   "(default is development)") do |env|
320:             options[:environment] = env
321:           end
322: 
323:           opts.on("-r", "--script-runner ['RUBY CODE'| FULL_SCRIPT_PATH]",
324:                   "Command-line option to run scripts and/or code in the " \
325:                   "merb app.") do |code_or_file|
326:             options[:runner_code] = code_or_file
327:             options[:adapter] = 'runner'
328:           end
329: 
330:           opts.on("-K", "--graceful PORT or all", "Gracefully kill one " \
331:                   "merb proceses by port number.  Use merb -K all to " \
332:                   "gracefully kill all merbs.") do |ports|
333:             options[:action] = :kill
334:             ports = "main" if ports == "all"
335:             options[:port] = ports
336:           end
337: 
338:           opts.on("-k", "--kill PORT", "Force kill one merb worker " \
339:                   "by port number. This will cause the worker to" \
340:                   "be respawned.") do |port|
341:             options[:action] = :kill_9
342:             port = "main" if port == "all"
343:             options[:port] = port
344:           end
345:           
346:           opts.on("--fast-deploy", "Reload the code, but not your" \
347:             "init.rb or gems") do
348:               options[:action] = :fast_deploy
349:           end
350: 
351:           # @todo Do we really need this flag? It seems unlikely to want to
352:           #   change the mutex from the command-line.
353:           opts.on("-X", "--mutex on/off", "This flag is for turning the " \
354:                   "mutex lock on and off.") do |mutex|
355:             if mutex == "off"
356:               options[:use_mutex] = false
357:             else
358:               options[:use_mutex] = true
359:             end
360:           end
361: 
362:           opts.on("-D", "--debugger", "Run merb using rDebug.") do
363:             begin
364:               require "ruby-debug"
365:               Debugger.start
366: 
367:               # Load up any .rdebugrc files we find
368:               [".", ENV["HOME"], ENV["HOMEPATH"]].each do |script_dir|
369:                 script_file = "#{script_dir}/.rdebugrc"
370:                 Debugger.run_script script_file, StringIO.new if File.exists?(script_file)
371:               end
372: 
373:               if Debugger.respond_to?(:settings)
374:                 Debugger.settings[:autoeval] = true
375:               end
376:               puts "Debugger enabled"
377:             rescue LoadError
378:               puts "You need to install ruby-debug to run the server in " \
379:                 "debugging mode. With gems, use `gem install ruby-debug'"
380:               exit
381:             end
382:           end
383: 
384:           opts.on("-V", "--verbose", "Print extra information") do
385:             options[:verbose] = true
386:           end
387: 
388:           opts.on("-C", "--console-trap", "Enter an irb console on ^C") do
389:             options[:console_trap] = true
390:           end
391: 
392:           opts.on("-?", "-H", "--help", "Show this help message") do
393:             puts opts
394:             exit
395:           end
396:         end
397: 
398:         # Parse what we have on the command line
399:         begin
400:           opts.parse!(argv)
401:         rescue OptionParser::InvalidOption => e
402:           Merb.fatal! e.message, e
403:         end
404:         Merb::Config.setup(options)
405:       end

Returns stores list constructed from configured session stores (:session_stores config option) or default one (:session_store config option).

:api: private

[Source]

    # File merb-core/lib/merb-core/dispatch/session.rb, line 11
11:     def self.session_stores
12:       @session_stores ||= begin
13:         config_stores = Array(
14:           Merb::Config[:session_stores] || Merb::Config[:session_store]
15:         )
16:         config_stores.map { |name| name.to_sym }
17:       end
18:     end

Sets up the configuration by storing the given settings.

Parameters

settings<Hash>:Configuration settings to use. These are merged with the defaults.

Returns

The configuration as a hash.

:api: private

[Source]

     # File merb-core/lib/merb-core/config.rb, line 153
153:       def setup(settings = {})
154:         config = defaults.merge(settings)
155:         
156:         unless config[:reload_classes]
157:           config[:fork_for_class_load] = false
158:         end
159: 
160:         dev_mode = config[:environment] == "development"
161:         unless config.key?(:reap_workers_quickly)
162:           config[:reap_workers_quickly] = dev_mode & !config[:cluster]
163:         end
164:         
165:         unless config.key?(:bind_fail_fatal)
166:           config[:bind_fail_fatal] = dev_mode
167:         end
168:         
169:         @configuration = config
170:       end

Returns the configuration as a hash.

Returns

Hash:The config as a hash.

:api: public

[Source]

     # File merb-core/lib/merb-core/config.rb, line 128
128:       def to_hash
129:         @configuration
130:       end

Returns the config as YAML.

Returns

String:The config as YAML.

:api: public

[Source]

     # File merb-core/lib/merb-core/config.rb, line 138
138:       def to_yaml
139:         require "yaml"
140:         @configuration.to_yaml
141:       end

Yields the configuration.

Block parameters

c<Hash>:The configuration parameters.

Examples

  Merb::Config.use do |config|
    config[:exception_details] = false
    config[:log_stream]        = STDOUT
  end

Returns

nil

:api: public

[Source]

    # File merb-core/lib/merb-core/config.rb, line 51
51:       def use
52:         @configuration ||= {}
53:         yield @configuration
54:         nil
55:       end

[Validate]