]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/configuration.rb
Factor out the description-message-building in the runners.
[mailshears.git] / lib / common / configuration.rb
1 require 'yaml'
2
3 # A configuration object that knows how to read options out of a file
4 # in <tt>~/.mailshears.conf.yml</tt>. The configuration options can be
5 # accessed via methods even though the internal representation is a
6 # hash.
7 #
8 # === Examples
9 #
10 # >> cfg = Configuration.new()
11 # >> cfg.i_mean_business()
12 # => true
13 #
14 class Configuration
15
16 # The default path to the user's configuration file.
17 USERCONF_PATH = ENV['HOME'] + '/.mailshears.conf.yml'
18
19 # The hash structure in which we store our configuration options
20 # internally.
21 @dict = {}
22
23
24 # Initialize a {Configuration} object with the config file at *path*.
25 #
26 # @param path [String] the path to the configuration file to
27 # load. We check for a file named ".mailshears.conf.yml" in the
28 # user's home directory by default.
29 #
30 def initialize(path = USERCONF_PATH)
31 cfg = default_configuration()
32
33 # Now, load the user configuration which will override the
34 # variables defined above.
35 begin
36 user_config = YAML.load(File.open(path))
37
38 # Write our own update() method for Ruby 1.8.
39 user_config.each do |key, value|
40 cfg[key] = value
41 end
42 rescue Errno::ENOENT
43 # If the user config file doesn't exist, whatever.
44 end
45
46 # Convert all of the keys to symbols.
47 cfg = cfg.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
48
49 @dict = cfg
50 end
51
52
53 # Replace all missing method calls with hash lookups. This lets us
54 # retrieve the values in our option hash by using methods named
55 # after the associated keys.
56 #
57 # @param sym [Symbol] the method that was called.
58 #
59 # @return [Object] the config file value associated with *sym*.
60 #
61 def method_missing(sym, *args)
62 return @dict[sym]
63 end
64
65
66 private;
67
68
69 # A default config hash.
70 #
71 # @return [Hash] sensible default configuration values.
72 #
73 def default_configuration()
74 d = {}
75
76 d['i_mean_business'] = false
77 d['plugins'] = ['agendav',
78 'davical',
79 'dovecot',
80 'postfixadmin',
81 'roundcube']
82
83 d['agendav_dbhost'] = 'localhost'
84 d['agendav_dbport'] = 5432
85 d['agendav_dbopts'] = ''
86 d['agendav_dbtty'] = ''
87 d['agendav_dbuser'] = 'postgres'
88 d['agendav_dbpass'] = ''
89 d['agendav_dbname'] = 'agendav'
90
91 d['davical_dbhost'] = 'localhost'
92 d['davical_dbport'] = 5432
93 d['davical_dbopts'] = ''
94 d['davical_dbtty'] = ''
95 d['davical_dbuser'] = 'postgres'
96 d['davical_dbpass'] = ''
97 d['davical_dbname'] = 'davical'
98
99 d['dovecot_mail_root'] = '/var/spool/mail/vhosts'
100
101 d['postfixadmin_dbhost'] = 'localhost'
102 d['postfixadmin_dbport'] = 5432
103 d['postfixadmin_dbopts'] = ''
104 d['postfixadmin_dbtty'] = ''
105 d['postfixadmin_dbuser'] = 'postgres'
106 d['postfixadmin_dbpass'] = ''
107 d['postfixadmin_dbname'] = 'postfixadmin'
108
109 d['roundcube_dbhost'] = 'localhost'
110 d['roundcube_dbport'] = 5432
111 d['roundcube_dbopts'] = ''
112 d['roundcube_dbtty'] = ''
113 d['roundcube_dbuser'] = 'postgres'
114 d['roundcube_dbpass'] = ''
115 d['roundcube_dbname'] = 'roundcube'
116
117 return d
118 end
119
120 end