Sha256: c140900380dce18531c89ab2b98eb19700f18d3b75f5089e83783fed4c500b3b

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

module Mongo3
  class Zone

    attr_reader :config
        
    def initialize( config_file )
      @config_file = config_file
    end
    
    # =========================================================================
    protected
    
      def zone_for_path( path )
        path.split( "|" )[1]
      end
      
      # Connects to mongo given an zone
      def connect_for( path, &block )
        zone = zone_for_path( path )    
        info = config[zone]
        # puts ">>> Connecting for #{zone} -- #{info['host']}-#{info['port']}"
        con = Mongo::Connection.new( info['host'], info['port'], { :slave_ok => true } )
        
        if info['user'] and info['password']
          con.db( 'admin' ).authenticate( info['user'], info['password'] )
        end
        yield con      
        con.close()
      end
    
      # find zone matching the host and port combination
      def zone_for( host, port )
        config.each_pair do |zone, info|
          return zone if info['host'] == host and info['port'] == port.to_i
        end
        nil
      end
      
      # Initialize the mongo installation landscape
      def config
        unless @config
          @config = YAML.load_file( @config_file )
        end
        @config
      end
    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongo3-0.0.9 lib/mongo3/zone.rb
mongo3-0.0.8 lib/mongo3/zone.rb
mongo3-0.0.7 lib/mongo3/zone.rb