02c30d252b796073587ee961011a02ccbebb1936
[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'] = ['postfixadmin']
78
79 d['agendav_dbhost'] = 'localhost'
80 d['agendav_dbport'] = 5432
81 d['agendav_dbopts'] = ''
82 d['agendav_dbtty'] = ''
83 d['agendav_dbuser'] = 'postgres'
84 d['agendav_dbpass'] = ''
85 d['agendav_dbname'] = 'agendav'
86
87 d['davical_dbhost'] = 'localhost'
88 d['davical_dbport'] = 5432
89 d['davical_dbopts'] = ''
90 d['davical_dbtty'] = ''
91 d['davical_dbuser'] = 'postgres'
92 d['davical_dbpass'] = ''
93 d['davical_dbname'] = 'davical'
94
95 d['dovecot_mail_root'] = '/tmp/mailshears-test'
96
97 d['postfixadmin_dbhost'] = 'localhost'
98 d['postfixadmin_dbport'] = 5432
99 d['postfixadmin_dbopts'] = ''
100 d['postfixadmin_dbtty'] = ''
101 d['postfixadmin_dbuser'] = 'postgres'
102 d['postfixadmin_dbpass'] = ''
103 d['postfixadmin_dbname'] = 'postfixadmin'
104
105 d['roundcube_dbhost'] = 'localhost'
106 d['roundcube_dbport'] = 5432
107 d['roundcube_dbopts'] = ''
108 d['roundcube_dbtty'] = ''
109 d['roundcube_dbuser'] = 'postgres'
110 d['roundcube_dbpass'] = ''
111 d['roundcube_dbname'] = 'roundcube'
112
113 return d
114 end
115
116 end