diff --git a/README.md b/README.md index 8f471af7..2d40914d 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,9 @@ To generate config you can use `rails generate couchbase_orm:config`: It will generate this `config/couchbase.yml` for you: +```yaml common: &common - hosts: localhost + connection_string: couchbase://localhost username: dev_user password: dev_password @@ -26,10 +27,23 @@ It will generate this `config/couchbase.yml` for you: # set these environment variables on your production server production: - hosts: <%= ENV['COUCHBASE_HOST'] || ENV['COUCHBASE_HOSTS'] %> - bucket: <%= ENV['COUCHBASE_BUCKET'] %> - username: <%= ENV['COUCHBASE_USER'] %> - password: <%= ENV['COUCHBASE_PASSWORD'] %> + connection_string: <%= ENV['COUCHBASE_CONNECTION_STRING'] %> + bucket: <%= ENV['COUCHBASE_BUCKET'] %> + username: <%= ENV['COUCHBASE_USER'] %> + password: <%= ENV['COUCHBASE_PASSWORD'] %> +``` + +## Setup without Rails +If you are not using Rails, you can configure couchbase-orm with an initializer: +```ruby +# config/initializers/couchbase_orm.rb +CouchbaseOrm::Connection.config = { + connection_string: "couchbase://localhost" + username: "dev_user" + password: "dev_password" + bucket: "dev_bucket" +} +``` Views are generated on application load if they don't exist or mismatch. This works fine in production however by default in development models are lazy loaded. diff --git a/lib/couchbase-orm/connection.rb b/lib/couchbase-orm/connection.rb index 9886c640..c959393e 100644 --- a/lib/couchbase-orm/connection.rb +++ b/lib/couchbase-orm/connection.rb @@ -1,23 +1,31 @@ - require 'couchbase' module CouchbaseOrm class Connection - @options = {} - class << self - attr_accessor :options + + def self.config + @@config || raise('Missing CouchbaseOrm connection config') + end + + def self.config=(config) + @@config = config end def self.cluster @cluster ||= begin - options = Couchbase::Cluster::ClusterOptions.new - options.authenticate(ENV["COUCHBASE_USER"], ENV["COUCHBASE_PASSWORD"]) - cluster = Couchbase::Cluster.connect('couchbase://127.0.0.1', options) - end + cb_config = Couchbase::Configuration.new + cb_config.connection_string = config[:connection_string] || raise('Missing CouchbaseOrm connection string') + cb_config.username = config[:username] || raise('Missing CouchbaseOrm username') + cb_config.password = config[:password] || raise('Missing CouchbaseOrm password') + Couchbase::Cluster.connect(cb_config) + end end def self.bucket - @bucket ||= cluster.bucket(ENV["COUCHBASE_BUCKET"]) + @bucket ||= begin + bucket_name = config[:bucket] || raise('Missing CouchbaseOrm bucket name') + cluster.bucket(bucket_name) + end end end -end +end \ No newline at end of file diff --git a/lib/couchbase-orm/railtie.rb b/lib/couchbase-orm/railtie.rb index 70a0cbe1..391512e9 100644 --- a/lib/couchbase-orm/railtie.rb +++ b/lib/couchbase-orm/railtie.rb @@ -24,8 +24,8 @@ module Rails #:nodoc: module Couchbase #:nodoc: class Railtie < Rails::Railtie #:nodoc: - config.couchbase2 = ActiveSupport::OrderedOptions.new - config.couchbase2.ensure_design_documents = true + config.couchbase_orm = ActiveSupport::OrderedOptions.new + config.couchbase_orm.ensure_design_documents = true # Maping of rescued exceptions to HTTP responses # @@ -44,14 +44,8 @@ def self.rescue_responses config.action_dispatch.rescue_responses.merge!(rescue_responses) end - # Initialize Couchbase Mode. This will look for a couchbase.yml in the - # config directory and configure Couchbase connection appropriately. - initializer 'couchbase.setup_connection' do - config_file = Rails.root.join('config', 'couchbase.yml') - if config_file.file? && - config = YAML.load(ERB.new(File.read(config_file)).result)[Rails.env] - ::CouchbaseOrm::Connection.options = config.deep_symbolize_keys - end + initializer 'couchbase_orm.setup_connection_config' do + CouchbaseOrm::Connection.config = Rails.application.config_for(:couchbase) end # After initialization we will warn the user if we can't find a couchbase.yml and @@ -79,7 +73,7 @@ def self.rescue_responses # Check (and upgrade if needed) all design documents config.after_initialize do |app| - if config.couchbase2.ensure_design_documents + if config.couchbase_orm.ensure_design_documents begin ::CouchbaseOrm::Base.descendants.each do |model| model.ensure_design_document! diff --git a/lib/rails/generators/couchbase_orm/config/templates/couchbase.yml b/lib/rails/generators/couchbase_orm/config/templates/couchbase.yml index fefc7479..52821e1c 100644 --- a/lib/rails/generators/couchbase_orm/config/templates/couchbase.yml +++ b/lib/rails/generators/couchbase_orm/config/templates/couchbase.yml @@ -1,5 +1,6 @@ common: &common - hosts: localhost + connection_string: couchbase://localhost + bucket: <%= bucket_name || app_name %> username: <%= username || bucket_name || app_name %> password: <%= password %> @@ -13,7 +14,7 @@ test: # set these environment variables on your production server production: - hosts: <%%= ENV['COUCHBASE_HOST'] || ENV['COUCHBASE_HOSTS'] %> + connection_string: <%%= ENV['COUCHBASE_CONNECTION_STRING'] %> bucket: <%%= ENV['COUCHBASE_BUCKET'] %> username: <%%= ENV['COUCHBASE_USER'] %> password: <%%= ENV['COUCHBASE_PASSWORD'] %>