Skip to content

Instantly share code, notes, and snippets.

@Oshuma
Created February 12, 2009 06:26
Show Gist options
  • Select an option

  • Save Oshuma/62514 to your computer and use it in GitHub Desktop.

Select an option

Save Oshuma/62514 to your computer and use it in GitHub Desktop.
# Return the given key's value from the (CakePHP) database config
# file. This will read through the config string looking for
# 'key' => 'value' pairs in the PHP array. Then, it will
# strip newlines, spaces, and substitute any quotes.
#
# I use this in a Rakefile I created to help ease the
# pain of working with CakePHP.
#
# Usage:
# File.open('database.php') do |f|
# config = f.read
# host = fetch_from(config, 'host')
# password = fetch_from(config, 'password')
# # ...etc...
# end
#
# Yes, it is completely hackish.
def fetch_from(config, key)
value = config.grep(/'#{key}' => (\w*)/).first.split('=>')[1]
value.gsub!(',', '').gsub!("'", '').chomp!.strip!
value
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment