Sha256: 4703dde42a7dd88284216724db08eb655a35cafd738d1d7f6dc7c41ccbbf580d

Contents?: true

Size: 1.48 KB

Versions: 9

Compression:

Stored size: 1.48 KB

Contents

require 'mongo-db-utils/models/db'
module MongoDbUtils

  module Model
    class Config
      attr_reader :dbs, :buckets
      attr_writer :writer
      attr_accessor :backup_folder

      def initialize
        @dbs = []
        @buckets = []
      end

      def empty?
        @dbs.nil? || @dbs.empty?
      end

      def has_buckets?
        !@buckets.nil? && !@buckets.empty?
      end

      def flush
        @dbs = []
        @writer.flush
      end

      def remove_db(db)
        @dbs = @dbs - [db]
        save
      end

      def add_replica_set(uri, name)
        add_db ReplicaSetDb.new(uri,name)
      end


      def add_single_db(uri)
        add_db Db.new(uri)
      end

      def already_contains(db)
        !@dbs.find{|current| current.uri == db.uri }.nil?
      end

      # because we are serializing the config - the bucket may be nil
      # at this point
      def add_bucket(bucket)
        @buckets = [] if @buckets.nil?
        unless already_contains_bucket?(bucket)
          @buckets << bucket
          save
        end
      end

      def already_contains_bucket?(bucket)
        !@buckets.find{ |b| b.to_s == bucket.to_s}.nil?
      end

      private
      def save
        @writer.save(self) unless @writer.nil?
      end

      def add_db(db)
        raise "nil db" if db.nil?
        @dbs = [] if @dbs.nil?
        unless already_contains(db)
          @dbs << db
          @dbs.sort!
          save
        end
        @dbs.include?(db)
      end

    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mongo-db-utils-0.2.1 lib/mongo-db-utils/models/config.rb
mongo-db-utils-0.2.0 lib/mongo-db-utils/models/config.rb
mongo-db-utils-0.1.6 lib/mongo-db-utils/models/config.rb
mongo-db-utils-0.1.5 lib/mongo-db-utils/models/config.rb
mongo-db-utils-0.1.4 lib/mongo-db-utils/models/config.rb
mongo-db-utils-0.1.3 lib/mongo-db-utils/models/config.rb
mongo-db-utils-0.1.2 lib/mongo-db-utils/models/config.rb
mongo-db-utils-0.1.0 lib/mongo-db-utils/models/config.rb
mongo-db-utils-0.0.9.3 lib/mongo-db-utils/models/config.rb