class Mongar

Attributes

log_level[RW]
logger[RW]
replicas[RW]
status_collection[RW]

Public Class Methods

configure(&block) click to toggle source
# File lib/mongar.rb, line 16
def configure &block
  mongar = self.new
  mongar.instance_eval(&block)
  mongar
end
new() click to toggle source
# File lib/mongar.rb, line 23
def initialize
  self.log_level = :debug
  self.logger = Logger.new(STDOUT)
  self.replicas = []
end

Public Instance Methods

log(destination) click to toggle source
# File lib/mongar.rb, line 82
def log(destination)
  if destination == :stdout
    @logger = Logger.new(STDOUT)
  else
    @logger = Logger.new(destination, 'daily')
  end
  set_log_level
end
mongo(name, &block) click to toggle source
# File lib/mongar.rb, line 62
def mongo(name, &block)
  mongo_db = Mongar::Mongo.new(:name => name)
  mongo_db.instance_eval(&block)
  Mongar::Mongo.databases[name] = mongo_db
  mongo_db
end
replicate(what, &block) click to toggle source
# File lib/mongar.rb, line 35
def replicate(what, &block)
  if what.is_a?(Hash)
    source = what.keys.first
    destinations = what.values.first
  else
    source = what
    destinations = what.to_s.downcase.en.plural
  end  
  destinations = [destinations] unless destinations.is_a?(Array)
  
  self.replicas ||= []
  
  destinations.each do |destination|
    database = nil
    collection = if destination.is_a?(Hash)
      database = destination.keys.first
      Mongar::Mongo::Collection.new(:name => destination.values.first, :logger => logger)
    else
      Mongar::Mongo::Collection.new(:name => destination, :logger => logger)
    end
    
    replica = Replica.new(:source => source, :destination => collection, :mongodb_name => database, :logger => logger)
    replica.instance_eval(&block)
    self.replicas << replica
  end
end
run() click to toggle source
# File lib/mongar.rb, line 29
def run
  replicas.each do |replica|
    replica.run
  end
end
set_log_level() click to toggle source
# File lib/mongar.rb, line 91
def set_log_level
  @logger.level = Logger.const_get(@log_level.to_s.upcase)
end
set_status_collection(val) click to toggle source
# File lib/mongar.rb, line 69
def set_status_collection(val)
  self.status_collection = val
end