rgrove / thoth

An awesome Ruby blog engine.

thoth / lib / thoth / config.rb
0c2345e6 » rgrove 2008-02-02 all kinds of awesomeness 1 #--
67e484bd » rgrove 2009-01-24 Hello 2009! 2 # Copyright (c) 2009 Ryan Grove <ryan@wonko.com>
0c2345e6 » rgrove 2008-02-02 all kinds of awesomeness 3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 #
8 # * Redistributions of source code must retain the above copyright notice,
9 # this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright notice,
11 # this list of conditions and the following disclaimer in the documentation
12 # and/or other materials provided with the distribution.
13 # * Neither the name of this project nor the names of its contributors may be
14 # used to endorse or promote products derived from this software without
15 # specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #++
28
c0819ebf » rgrove 2008-03-11 change project name to Thot... 29 module Thoth
0c2345e6 » rgrove 2008-02-02 all kinds of awesomeness 30
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 31 module Config; class << self
5df3bb0a » rgrove 2008-05-04 Clean up end-of-line whites... 32
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 33 # Adds the specified config Hash to Thoth's config lookup chain. Any
34 # configuration values in _config_ will be used as defaults unless they're
35 # specified earlier in the lookup chain (i.e. in Thoth's config file).
36 def <<(config)
37 raise ArgumentError, "config must be a Hash" unless config.is_a?(Hash)
cfc3c20f » rgrove 2008-12-25 Add Gravatar support, enabl... 38
a89e6aff » rgrove 2009-05-05 Walk the config chain and c... 39 (@lookup ||= []) << config
40 cache_config
41
42 @lookup
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 43 end
5df3bb0a » rgrove 2008-05-04 Clean up end-of-line whites... 44
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 45 # Loads the specified configuration file.
46 def load(file)
47 raise Thoth::Error, "Config file not found: #{file}" unless File.file?(file)
5df3bb0a » rgrove 2008-05-04 Clean up end-of-line whites... 48
d30af036 » rgrove 2009-04-25 Config tweaks 49 @live = {
50 'db' => "sqlite:///#{HOME_DIR}/db/live.db",
a6a8d983 » rgrove 2008-04-16 set server.compile_views, s... 51
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 52 'site' => {
53 'name' => "New Thoth Blog",
54 'desc' => "Thoth is awesome.",
55 'url' => "http://localhost:7000/",
56
57 'css' => [],
58 'js' => [],
5df3bb0a » rgrove 2008-05-04 Clean up end-of-line whites... 59
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 60 'enable_comments' => true,
61 'enable_sitemap' => true,
62
63 'gravatar' => {
64 'enabled' => true,
65 'default' => "identicon",
66 'rating' => "g",
67 'size' => 32
a6a8d983 » rgrove 2008-04-16 set server.compile_views, s... 68 }
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 69 },
70
71 'admin' => {
72 'name' => "John Doe",
73 'email' => "",
74 'user' => "thoth",
aad34c14 » rgrove 2009-04-25 More tweaks 75 'pass' => "thoth",
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 76 'seed' => "6d552ac197a862b82b85868d6c245feb"
77 },
78
79 'plugins' => [],
80
81 'media' => File.join(HOME_DIR, 'media'),
82
83 'server' => {
d30af036 » rgrove 2009-04-25 Config tweaks 84 'adapter' => 'webrick',
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 85 'address' => '0.0.0.0',
86 'port' => 7000,
87 'enable_cache' => true,
a89e6aff » rgrove 2009-05-05 Walk the config chain and c... 88 'enable_minify' => true,
db7cad37 » rgrove 2009-05-03 Add memcache support 89 'error_log' => File.join(HOME_DIR, 'log', 'error.log'),
90
91 'memcache' => {
92 'enabled' => false,
93 'servers' => ['localhost:11211:1']
94 }
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 95 },
96
97 'timestamp' => {
98 'long' => "%A %B %d, %Y @ %I:%M %p (%Z)",
99 'short' => "%Y-%m-%d %I:%M"
c8143106 » rgrove 2008-02-20 all kinds of awesomeness 100 }
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 101 }
5df3bb0a » rgrove 2008-05-04 Clean up end-of-line whites... 102
d30af036 » rgrove 2009-04-25 Config tweaks 103 @dev = {
104 'db' => "sqlite:///#{HOME_DIR}/db/dev.db",
105
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 106 'server' => {
8d71294f » rgrove 2009-05-05 Remove Innate::Action monke... 107 'enable_cache' => false,
d30af036 » rgrove 2009-04-25 Config tweaks 108 'enable_minify' => false
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 109 }
110 }
0c2345e6 » rgrove 2008-02-02 all kinds of awesomeness 111
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 112 begin
c1969be1 » rgrove 2009-05-03 Don't die on empty config file 113 config = YAML.load(Erubis::Eruby.new(File.read(file)).result(binding)) || {}
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 114 rescue => e
115 raise Thoth::ConfigError, "Config error in #{file}: #{e}"
c8143106 » rgrove 2008-02-20 all kinds of awesomeness 116 end
5df3bb0a » rgrove 2008-05-04 Clean up end-of-line whites... 117
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 118 @lookup ||= if Thoth.trait[:mode] == :production
d30af036 » rgrove 2009-04-25 Config tweaks 119 [config['live'] || {}, @live]
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 120 else
d30af036 » rgrove 2009-04-25 Config tweaks 121 [config['dev'] || {}, config['live'] || {}, @dev, @live]
c8143106 » rgrove 2008-02-20 all kinds of awesomeness 122 end
a89e6aff » rgrove 2009-05-05 Walk the config chain and c... 123
124 cache_config
76ffdb8a » rgrove 2008-02-12 switch to Ara Howard's conf... 125 end
c8143106 » rgrove 2008-02-20 all kinds of awesomeness 126
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 127 def method_missing(name)
a89e6aff » rgrove 2009-05-05 Walk the config chain and c... 128 (@cached || {})[name.to_s] || {}
db7cad37 » rgrove 2009-05-03 Add memcache support 129 end
130
131 private
132
a89e6aff » rgrove 2009-05-05 Walk the config chain and c... 133 # Merges configs such that those earlier in the lookup chain override those
134 # later in the chain.
135 def cache_config
136 @cached = {}
137
138 @lookup.reverse.each do |c|
139 c.each {|k, v| @cached[k] = config_merge(@cached[k] || {}, v) }
140 end
141 end
142
db7cad37 » rgrove 2009-05-03 Add memcache support 143 def config_merge(master, value)
144 if value.is_a?(Hash)
145 value.each {|k, v| master[k] = config_merge(master[k] || {}, v) }
146 return master
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 147 end
148
db7cad37 » rgrove 2009-05-03 Add memcache support 149 value
057fea6e » rgrove 2009-04-25 Lovely new YAML-based confi... 150 end
151
152 end; end
c8143106 » rgrove 2008-02-20 all kinds of awesomeness 153 end