Sha256: e72ee45b10e4d8735a41d0e91f3ccf4e1523b9d400e255b90391800902889cff

Contents?: true

Size: 1.42 KB

Versions: 23

Compression:

Stored size: 1.42 KB

Contents

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

  module Connection

    extend self

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

    def config_path
      @config_path || './mongodb.yml'
    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 default_name
      @default_name ||= "mongo_doc"
    end

    def env
      @env ||= 'development'
    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 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

23 entries across 23 versions & 2 rubygems

Version Path
mongo_doc-0.6.22 lib/mongo_doc/connection.rb
mongo_doc-0.6.21 lib/mongo_doc/connection.rb
mongo_doc-0.6.20 lib/mongo_doc/connection.rb
mongo_doc-0.6.19 lib/mongo_doc/connection.rb
mongo_doc-0.6.18 lib/mongo_doc/connection.rb
mongo_doc-0.6.17 lib/mongo_doc/connection.rb
mongo_doc-0.6.16 lib/mongo_doc/connection.rb
mongo_doc-0.6.15 lib/mongo_doc/connection.rb
mongo_doc-0.6.14 lib/mongo_doc/connection.rb
mongo_doc-0.6.13 lib/mongo_doc/connection.rb
mongo_doc-0.6.12 lib/mongo_doc/connection.rb
mongo_doc-0.6.11 lib/mongo_doc/connection.rb
mongo_doc-0.6.10 lib/mongo_doc/connection.rb
mongo_doc-0.6.9 lib/mongo_doc/connection.rb
mongo_doc-0.6.8 lib/mongo_doc/connection.rb
mongo_doc-0.6.7 lib/mongo_doc/connection.rb
mongo_doc-0.6.6 lib/mongo_doc/connection.rb
mongo_doc-0.6.5 lib/mongo_doc/connection.rb
mongo_doc-0.6.4 lib/mongo_doc/connection.rb
mongo_doc_rails2-0.6.2 lib/mongo_doc/connection.rb