Class: Wackamole::Control

Inherits:
Object
  • Object
show all
Defined in:
lib/wackamole/models/control.rb

Class Method Summary collapse

Class Method Details

.collection(cltn_name, db_name = nil, opts = {:strict => true}) ⇒ Object


Fetch a collection on a given database by name



33
34
35
36
# File 'lib/wackamole/models/control.rb', line 33

def self.collection( cltn_name, db_name=nil, opts={:strict => true} )
  raise "Unable to locate current database" unless @db
  @db.collection( cltn_name )
end

.current_db(zone, app_name, env, reset = false) ⇒ Object


Set current database



54
55
56
57
58
59
60
# File 'lib/wackamole/models/control.rb', line 54

def self.current_db( zone, app_name, env, reset=false )
  return @db if @db and !reset
  app     = app_name.gsub( /\s/, '_' ).downcase
  db_name = to_mole_db( app_name, env )
  
  @db = connection( zone ).db( db_name )
end

.ensure_db(context) ⇒ Object


Makes sure we’re in the right context



47
48
49
50
# File 'lib/wackamole/models/control.rb', line 47

def self.ensure_db( context )
  tokens = context.split( "." )
  current_db( *tokens )
end

.extract_app_info(db_name) ⇒ Object


extract app_name + env from db_name



40
41
42
43
# File 'lib/wackamole/models/control.rb', line 40

def self.extract_app_info( db_name )
  raise "Invalid mole db specification #{db_name}" unless db_name =~ molex
  db_name.match( molex ).captures
end

.init_config(config_file) ⇒ Object


Initialize app by reading off mongo configuration parameters if necessary



10
11
12
13
14
15
16
17
18
19
# File 'lib/wackamole/models/control.rb', line 10

def self.init_config( config_file )
  begin
    @config = YAML.load_file( config_file )
    raise "Unable to find zones definition" unless @config['zones']
  rescue => boom
    @config = nil          
    raise "Hoy! An error occur loading the config file `#{config_file} -- #{boom}"
  end
  @config
end

.mole_databases(zone) ⇒ Object


Inspect current connection databases and weed out mole_xxx databases



77
78
79
80
81
# File 'lib/wackamole/models/control.rb', line 77

def self.mole_databases( zone )
  connection( zone ).database_names.select do |db_name|
    db_name if mole_db?( zone, db_name )
  end
end

.molexObject


Defines mole db identity



29
# File 'lib/wackamole/models/control.rb', line 29

def self.molex() @molex ||= /mole_(.*)?_(.*)?_mdb/; end

.zonesObject


List all available zones



23
24
25
# File 'lib/wackamole/models/control.rb', line 23

def self.zones
  @config['zones'].keys
end