rework part of configure mechanism
[coquelicot.git] / lib / coquelicot / configure.rb
1 module Coquelicot::Configure
2   def self.included(c)
3     c.extend ClassMethods
4     c.configure
5   end
6
7   module ClassMethods
8     def configure
9       user_settings = load_user_settings if File.exists?(settings_path)
10       merged_settings = default_settings.merge(user_settings || {})
11       merged_settings.each { |k,v| set k,v }
12     end
13
14     def authentication_method=(options)
15       method = options.delete('name') || options.delete(:name)
16       method = method.to_s if method.is_a? Symbol
17       require "coquelicot/auth/#{method}"
18       set :authenticator, Coquelicot::Auth.
19          const_get("#{method.to_s.capitalize}Authenticator").new(self)
20       options.each{|k,v| set k,v }
21     end
22
23   private
24
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         'about_text' => '',
33         'additional_css' => '',
34         'depot_path' => Proc.new { File.join(root, 'files') },
35         'authentication_method' => {
36           'name' => 'simplepass',
37           'upload_password' => 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'
38         },
39       }
40     end
41
42     def settings_path
43       @settings_path ||= File.join(File.dirname(__FILE__),'..','..','conf','settings.yml')
44     end
45
46     def load_user_settings
47       YAML.load(File.read(settings_path))
48     end
49   end
50 end