Sha256: 5220fde16f91ff29504c750e4955b7683fd09783c61a3762d501a7e83ab7e26f

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

module MongoDoc
  class NoConnectionError < RuntimeError; end
  class UnsupportedServerVersionError < RuntimeError; end

  module Connection

    extend self

    attr_writer :config_path, :env, :host, :name, :options, :port, :strict

    def config_path
      @config_path || default_path
    end

    def configuration
      @configuration ||= File.exists?(config_path) ? YAML.load_file(config_path)[env] : {}
    end

    def connection
      @connection ||= connect
    end

    def database
      @database ||= connection.db(name, :strict => strict)
    end

    def env
      if rails?
        Rails.env
      else
        @env ||= 'development'
      end
    end

    def host
      @host ||= configuration['host']
    end

    def name
      @name ||= configuration['name'] || default_name
    end

    def options
      @options ||= configuration['options'] || {}
    end

    def port
      @port ||= configuration['port']
    end

    def strict
      @strict ||= configuration['strict'] || false
    end

    private

    def connect
      connection = Mongo::Connection.new(host, port, options)
      raise NoConnectionError unless connection
      verify_server_version(connection)
      connection
    end

    def default_name
      if rails?
        "#{Rails.root.basename}_#{Rails.env}"
      else
        "mongo_doc"
      end
    end

    def default_path
      if rails?
        Rails.root + 'config/mongodb.yml'
      else
        './mongodb.yml'
      end
    end

    def rails?
      Object.const_defined?("Rails")
    end

    def verify_server_version(connection)
      raise UnsupportedServerVersionError.new('MongoDoc requires at least mongoDB version 1.4.0') unless connection.server_version >= "1.4.0"
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
mongo_doc-0.6.0 lib/mongo_doc/connection.rb
mongo_doc_rails2-0.6.1 lib/mongo_doc/connection.rb
mongo_doc-0.5.5 lib/mongo_doc/connection.rb