Add configuration mechanism
[coquelicot.git] / lib / coquelicot / configure.rb
1 module Coquelicot::Configure
2   
3   def self.included(c)
4     c.extend ClassMethods
5     c.configure
6   end
7   
8   module ClassMethods
9     def configure
10       user_settings = File.exists?(settings_path) ? load_user_settings : {}
11       default_settings.delete('authentication_method') if user_settings['authentication_method']
12       merged_settings = default_settings.merge(user_settings)
13       if (authm=merged_settings.delete('authentication_method'))
14         authentication_method authm.delete('name'), authm
15       end
16       merged_settings.each{|k,v| set k,v }
17     end
18     private
19     def authentication_method(method,options={})
20       require "coquelicot/auth/#{method}"
21       set :auth_method, method
22       include (eval "Coquelicot::Auth::#{method.to_s.capitalize}")
23       options.each{|k,v| set k,v }
24     end
25     def default_settings
26       {
27         'default_expire' => 60,
28         'maximum_expire' => 60 * 24 * 30, # 1 month
29         'gone_period' => 10080,
30         'filename_length' => 20,
31         'random_pass_length' => 16,
32         'depot_path' => Proc.new { File.join(root, 'files') },
33         'authentication_method' => {
34           'name' => 'simplepass',
35           'upload_password' => 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'          
36         },
37       }
38     end
39     
40     def settings_path
41       @settings_path ||= File.join(File.dirname(__FILE__),'..','..','conf','settings.yml')
42     end
43     
44     def load_user_settings
45       YAML.load(File.read(settings_path))
46     end
47   end
48 end