Module: MyStuff::MultiDB::Mangling

Defined in:
lib/my_stuff/multidb/mangling.rb

Class Method Summary collapse

Class Method Details

.mangle(spec) ⇒ Object

Create a valid constant name from an ActiveRecord spec.

This can be revered by #unmangle.



27
28
29
30
31
32
33
34
35
# File 'lib/my_stuff/multidb/mangling.rb', line 27

def self.mangle spec
  'MYSTUFF_MULTIDB_DB_' + (
    "%s!%s!%s" % [
      spec[:host] || spec['host'],
      spec[:port] || spec['port'],
      spec[:database] || spec['database'],
    ]
  ).each_byte.map{ |x| "%x" % x }.join
end

.unmangle(name) ⇒ Object

Takes a module name and converts it to connection details.

Example:

mangled

MyStuff::MultiDB::MYSTUFF_MULTIDB_DB_747970686f6e2e66726564656d6d6f74742e636f2e756b2c333330362c747474::ServiceLocator::Tier

unmanagled

MyStuff::MultiDB::<mysql://typhon.fredemmott.co.uk:3306/ttt>::ServiceLocator::Tier

The initscript cat-log and tail-log commands will do this unmanggling for you.



13
14
15
16
17
18
19
20
21
22
# File 'lib/my_stuff/multidb/mangling.rb', line 13

def self.unmangle name
  db = name.sub /^:?MYSTUFF_MULTIDB_DB_/, ''
  # Format: "MYSTUFF_MULTIDB_DB_" + hex("host,port,database")
  host, port, database = db.each_char.each_slice(2).reduce(String.new){ |m,*nibbles| m += "%c" % nibbles.join.hex }.split('!')
  {
    :host     => host,
    :port     => port.to_i,
    :database => database,
  }
end