Module: Merb::Orms::CouchRest
- Defined in:
- lib/merb/orms/couchrest/connection.rb
Defined Under Namespace
Classes: Connect
Class Method Summary collapse
- .config ⇒ Object
- .config_file ⇒ Object
- .connect ⇒ Object
- .copy_sample_config ⇒ Object
- .sample_dest ⇒ Object
- .sample_source ⇒ Object
Class Method Details
.config ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/merb/orms/couchrest/connection.rb', line 21 def config @config ||= begin # Convert string keys to symbols full_config = Erubis.load_yaml_file(config_file) config = (Merb::Plugins.config[:merb_couchrest] = {}) (full_config[Merb.environment.to_sym] || full_config[Merb.environment] || full_config[:development]).each do |key, value| config[key.to_sym] = value end config end end |
.config_file ⇒ Object
12 |
# File 'lib/merb/orms/couchrest/connection.rb', line 12 def config_file() Merb.dir_for(:config) / "database.yml" end |
.connect ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/merb/orms/couchrest/connection.rb', line 33 def connect if File.exists?(config_file) # it is possible that we have been passed an array of hosts - attempt each in turn until one is found config[:host].each do |host| begin Merb.logger.info!("Attempting connection to the '#{config[:database]}' database on '#{host}' ...") # Merb.logger.info!("using string #{host}/#{config[:database]}") server = ::CouchRest.new(host) # calling this will cause an exception if the host is invalid info = server.info() Merb.logger.info!("Connected to '#{host}' - version #{info['version']}") begin if config[:create_db_if_absent] database_connection = server.database!( config[:database] ) else database_connection = server.database( config[:database] ) end # this will test the connection and raise a ResourceNotFound exception # if it is not valid database_connection.info() # set the default database ::CouchRest::Document.use_database( database_connection ) # also store this config ? Merb::Config[:couchrest_default_database] = database_connection # we are done - return with the result return database_connection rescue RestClient::ResourceNotFound Merb.logger.info!("#{config[:database]} is not available on #{host}") end rescue Errno::ECONNREFUSED Merb.logger.info!("#{host} not available") rescue => e Merb.logger.info!("Connection Error: #{e}") Merb.print_colorized_backtrace(e) exit(1) end end # if we have reached this point, then a connection wasn't found Merb.logger.info!("Unable to connect") exit(1) else copy_sample_config Merb.logger.set_log(STDERR) Merb.logger.error! "No database.yml file found at #{config_file}." Merb.logger.error! "A sample file was created called #{sample_dest} for you to copy and edit." exit(1) end end |
.copy_sample_config ⇒ Object
16 17 18 |
# File 'lib/merb/orms/couchrest/connection.rb', line 16 def copy_sample_config FileUtils.cp sample_source, sample_dest unless File.exists?(sample_dest) end |
.sample_dest ⇒ Object
13 |
# File 'lib/merb/orms/couchrest/connection.rb', line 13 def sample_dest() Merb.dir_for(:config) / "database.yml.sample" end |
.sample_source ⇒ Object
14 |
# File 'lib/merb/orms/couchrest/connection.rb', line 14 def sample_source() File.dirname(__FILE__) / "database.yml.sample" end |