2011-04-14 80 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