Class: Mongar

Inherits:
Object
  • Object
show all
Defined in:
lib/mongar.rb,
lib/mongar/mongo.rb,
lib/mongar/column.rb,
lib/mongar/replica.rb

Defined Under Namespace

Classes: Column, Mongo, Replica, UnknownLogLevel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMongar

Returns a new instance of Mongar.



23
24
25
26
27
# File 'lib/mongar.rb', line 23

def initialize
  self.log_level = :debug
  self.logger = Logger.new(STDOUT)
  self.replicas = []
end

Instance Attribute Details

#log_level(level = nil) ⇒ Object

Returns the value of attribute log_level.



13
14
15
# File 'lib/mongar.rb', line 13

def log_level
  @log_level
end

#loggerObject

Returns the value of attribute logger.



13
14
15
# File 'lib/mongar.rb', line 13

def logger
  @logger
end

#replicasObject

Returns the value of attribute replicas.



13
14
15
# File 'lib/mongar.rb', line 13

def replicas
  @replicas
end

#status_collectionObject

Returns the value of attribute status_collection.



13
14
15
# File 'lib/mongar.rb', line 13

def status_collection
  @status_collection
end

Class Method Details

.configure(&block) ⇒ Object



16
17
18
19
20
# File 'lib/mongar.rb', line 16

def configure &block
  mongar = self.new
  mongar.instance_eval(&block)
  mongar
end

Instance Method Details

#log(destination) ⇒ Object



82
83
84
85
86
87
88
89
# 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) ⇒ Object



62
63
64
65
66
67
# 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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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

#runObject



29
30
31
32
33
# File 'lib/mongar.rb', line 29

def run
  replicas.each do |replica|
    replica.run
  end
end

#set_log_levelObject



91
92
93
# 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) ⇒ Object



69
70
71
# File 'lib/mongar.rb', line 69

def set_status_collection(val)
  self.status_collection = val
end