Class: Wackamole::Control
- Inherits:
-
Object
- Object
- Wackamole::Control
- Defined in:
- lib/wackamole/models/control.rb
Class Method Summary collapse
-
.collection(cltn_name, db_name = nil, opts = {:strict => true}) ⇒ Object
————————————————————————- Fetch a collection on a given database by name.
-
.extract_app_info(db_name) ⇒ Object
————————————————————————- extract app_name + env from db_name.
-
.init_config(config_file, env) ⇒ Object
———————————————————————– Initialize app by reading off mongo configuration parameters if necessary.
-
.mole_databases ⇒ Object
————————————————————————- Inspect current connection databases and weed out mole_xxx databases.
-
.molex ⇒ Object
————————————————————————- Defines mole db identity.
-
.switch_mole_db!(app_name, env) ⇒ Object
————————————————————————- Switch db instance given db_name NOTE : This assumes mole db naming convention ie mole_in lower case_env_mdb.
Class Method Details
.collection(cltn_name, db_name = nil, opts = {:strict => true}) ⇒ Object
Fetch a collection on a given database by name
29 30 31 32 |
# File 'lib/wackamole/models/control.rb', line 29 def self.collection( cltn_name, db_name=nil, opts={:strict => true} ) # reset_db!( db_name ) if db_name db( db_name, opts ).collection( cltn_name ) end |
.extract_app_info(db_name) ⇒ Object
extract app_name + env from db_name
36 37 38 39 |
# File 'lib/wackamole/models/control.rb', line 36 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, env) ⇒ Object
Initialize app by reading off mongo configuration parameters if necessary
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/wackamole/models/control.rb', line 9 def self.init_config( config_file, env ) begin config = YAML.load_file( config_file ) @config = config[env] raise "Invalid environment `#{env}" unless @config raise "Unable to find host in - #{@config.inspect}" unless @config.has_key?('host') raise "Unable to find port in - #{@config.inspect}" unless @config.has_key?('port') rescue => boom @config = nil raise "Hoy! An error occur loading the config file `#{config_file} -- #{boom}" end @config end |
.mole_databases ⇒ Object
Inspect current connection databases and weed out mole_xxx databases
56 57 58 59 60 |
# File 'lib/wackamole/models/control.rb', line 56 def self.mole_databases connection.database_names.select do |db_name| db_name if mole_db?( db_name ) end end |
.molex ⇒ Object
Defines mole db identity
25 |
# File 'lib/wackamole/models/control.rb', line 25 def self.molex() @molex || /mole_(.*)?_(.*)?_mdb/; end |
.switch_mole_db!(app_name, env) ⇒ Object
Switch db instance given db_name NOTE : This assumes mole db naming convention ie mole_in lower case_env_mdb
45 46 47 48 49 50 51 52 |
# File 'lib/wackamole/models/control.rb', line 45 def self.switch_mole_db!( app_name, env ) raise "You must specify an app name and environment" unless app_name and env app = app_name.gsub( /\s/, '_' ).downcase db_name = to_mole_db( app_name, env ) raise "Invalid mole database #{db_name}" unless mole_db?( db_name ) reset_db!( db_name ) @db end |