2011-04-14 137 views

回答

1

使用例如WatchrGuard您可以监视文件并对它们进行更改。

文件更改时采取的实际操作完全取决于您的具体设置和情况,因此您需要自行处理。或者你需要提供更多信息。

4

Configurabilty(公开:我是作者)中有一个配置对象,你可以单独使用它,也可以将它作为可配置性mixin的一部分。从the documentation

可配置还包括Configurability::Config,可被用于加载YAML配置文件, 然后本哈希样和一个结构一样的界面既用于读 配置节一个相当简单的 配置对象类和价值观;它意味着与可配置性一起使用,但它本身也很有用。

下面是一个演示其功能的简单示例。假设你有一个 配置文件看起来像这样:

--- 
database: 
    development: 
    adapter: sqlite3 
    database: db/dev.db 
    pool: 5 
    timeout: 5000 
    testing: 
    adapter: sqlite3 
    database: db/testing.db 
    pool: 2 
    timeout: 5000 
    production: 
    adapter: postgres 
    database: fixedassets 
    pool: 25 
    timeout: 50 
ldap: 
    uri: ldap://ldap.acme.com/dc=acme,dc=com 
    bind_dn: cn=web,dc=acme,dc=com 
    bind_pass: [email protected]@ge 
branding: 
    header: "#333" 
    title: "#dedede" 
    anchor: "#9fc8d4" 

可以加载此配置,如下所示:

require 'configurability/config' 
config = Configurability::Config.load('examples/config.yml') 
# => #<Configurability::Config:0x1018a7c7016 loaded from 
    examples/config.yml; 3 sections: database, ldap, branding> 

,然后用结构般的方法来访问它:

config.database 
# => #<Configurability::Config::Struct:101806fb816 
    {:development=>{:adapter=>"sqlite3", :database=>"db/dev.db", :pool=>5, 
    :timeout=>5000}, :testing=>{:adapter=>"sqlite3", 
    :database=>"db/testing.db", :pool=>2, :timeout=>5000}, 
    :production=>{:adapter=>"postgres", :database=>"fixedassets", 
    :pool=>25, :timeout=>50}}> 

config.database.development.adapter 
# => "sqlite3" 

config.ldap.uri 
# => "ldap://ldap.acme.com/dc=acme,dc=com" 

config.branding.title 
# => "#dedede" 

或使用使用Symbols,Strings或 的混合的哈希样接口:

config[:branding][:title] 
# => "#dedede" 

config['branding']['header'] 
# => "#333" 

config['branding'][:anchor] 
# => "#9fc8d4" 

您可以通过可配置接口安装:

config.install 

检查,看它是否是从,因为你 加载它改变加载的文件:

config.changed? 
# => false 

# Simulate changing the file by manually changing its mtime 
File.utime(Time.now, Time.now, config.path) 
config.changed? 
# => true 

如果已经更改(或者即使没有更改),您可以重新加载它,然后通过可配置界面自动重新安装它:

config.reload 

您可以通过相同的Struct-或哈希样的接口进行修改和写入修改后的配置背出同一个文件:

config.database.testing.adapter = 'mysql' 
config[:database]['testing'].database = 't_fixedassets' 

然后将其转储到YAML字符串:

config.dump 
# => "--- \ndatabase: \n development: \n adapter: sqlite3\n 
    database: db/dev.db\n pool: 5\n timeout: 5000\n testing: \n 
    adapter: mysql\n database: t_fixedassets\n pool: 2\n timeout: 
    5000\n production: \n adapter: postgres\n database: 
    fixedassets\n pool: 25\n timeout: 50\nldap: \n uri: 
    ldap://ldap.acme.com/dc=acme,dc=com\n bind_dn: 
    cn=web,dc=acme,dc=com\n bind_pass: [email protected]@ge\nbranding: \n 
    header: \"#333\"\n title: \"#dedede\"\n anchor: \"#9fc8d4\"\n" 

或写回它从加载的文件:

config.write 
当涉及到缓存中
+0

那是非常有帮助在运行时在配置文件中形成。我有这种情况,现在很容易处理。谢谢! – maddin2code 2014-02-14 10:32:31