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