Sha256: 77db4b6b2785d87457330611e8f1ee88d9b6c7f79ad3acbb6044e504b95bf517

Contents?: true

Size: 1.89 KB

Versions: 10

Compression:

Stored size: 1.89 KB

Contents

module CouchRest
  module Model
    module Connection
      extend ActiveSupport::Concern

      def server
        self.class.server
      end

      module ClassMethods

        # Overwrite the normal use_database method so that a database
        # name can be provided instead of a full connection.
        def use_database(db)
          @database = prepare_database(db)
        end

        # Overwrite the default database method so that it always
        # provides something from the configuration
        def database
          super || (@database ||= prepare_database)
        end

        def server
          @server ||= CouchRest::Server.new(prepare_server_uri)
        end

        def prepare_database(db = nil)
          unless db.is_a?(CouchRest::Database)
            conf = connection_configuration
            db = [conf[:prefix], db.to_s, conf[:suffix]].reject{|s| s.to_s.empty?}.join(conf[:join])
            self.server.database!(db)
          else
            db
          end
        end

        protected

        def prepare_server_uri
          conf = connection_configuration
          userinfo = [conf[:username], conf[:password]].compact.join(':')
          userinfo += '@' unless userinfo.empty?
          "#{conf[:protocol]}://#{userinfo}#{conf[:host]}:#{conf[:port]}"
        end

        def connection_configuration
          @connection_configuration ||=
            self.connection.update(
              (load_connection_config_file[environment.to_sym] || {}).symbolize_keys
            )
        end

        def load_connection_config_file
          file = connection_config_file
          connection_config_cache[file] ||=
            (File.exists?(file) ?
              YAML::load(ERB.new(IO.read(file)).result) :
              { }).symbolize_keys
        end

        def connection_config_cache
          Thread.current[:connection_config_cache] ||= {}
        end

      end

    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
couchrest_model-2.0.0 lib/couchrest/model/connection.rb
couchrest_model-2.0.0.beta2 lib/couchrest/model/connection.rb
couchrest_model-2.0.0.beta lib/couchrest/model/connection.rb
couchrest_model-1.2.0.beta lib/couchrest/model/connection.rb
openlogic-couchrest_model-1.0.0 lib/couchrest/model/connection.rb
couchrest_model-1.1.2 lib/couchrest/model/connection.rb
couchrest_model-1.1.1 lib/couchrest/model/connection.rb
couchrest_model-1.1.0 lib/couchrest/model/connection.rb
couchrest_model-1.1.0.rc1 lib/couchrest/model/connection.rb
couchrest_model-1.1.0.beta5 lib/couchrest/model/connection.rb