2011-10-26 11 views
5

ho il mio file di configurazione YAML mongo.yml:Usa Yaml per MongoMapper Config

development: 
    adapter: mongodb 
    database: fhsclock_development 
    host: localhost 
    port: nil 

test: 
    adapter: mongodb 
    database: fhsclock_test 
    host: localhost 
    port: nil 

production: 
    adapter: mongodb 
    database: fhsclock 
    hosts: 
    - - localhost 
    - nil 
    - - staff.mongohq.com 
    - 10015 

Come posso utilizzare questo file per la configurazione e la connessione con MongoMapper?

risposta

9

MongoMapper utilizzerà il file solo se si sta utilizzando Rails e il file è config/mongo.yml. Se non sei on Rails, è possibile adattare this code from the source:

config_file = Rails.root.join('config/mongo.yml') 
if config_file.file? 
    config = YAML.load(ERB.new(config_file.read).result) 
    MongoMapper.setup(config, Rails.env, :logger => Rails.logger) 
end 

Inoltre, la "Scheda" nel file è estraneo. (Vedi Getting Started documentation). Un mongo.yml da rails g mongo_mapper:config assomiglia:

defaults: &defaults 
    host: 127.0.0.1 
    port: 27017 

development: 
    <<: *defaults 
    database: my_app_development 

test: 
    <<: *defaults 
    database: my_app_test 

# set these environment variables on your prod server 
production: 
    <<: *defaults 
    database: my_app 
    username: <%= ENV['MONGO_USERNAME'] %> 
    password: <%= ENV['MONGO_PASSWORD'] %> 
+0

(vedi risposta Edited) –