Class: DatabaseYML
- Inherits:
-
Object
- Object
- DatabaseYML
- Defined in:
- lib/database_yml.rb
Constant Summary collapse
- SCRIPT_RAILS =
File.join('script', 'rails')
- DRIVERS =
%W(mysql postgresql sqlite3)
- TEMPLATES_DIR =
File.join(File.(File.dirname(__FILE__)), "templates")
Class Method Summary collapse
Instance Method Summary collapse
- #create! ⇒ Object
- #generate_database_yml! ⇒ Object
-
#initialize(database) ⇒ DatabaseYML
constructor
A new instance of DatabaseYML.
Constructor Details
#initialize(database) ⇒ DatabaseYML
Returns a new instance of DatabaseYML.
10 11 12 |
# File 'lib/database_yml.rb', line 10 def initialize(database) @database = database || 'sqlite3' end |
Class Method Details
.in_rails_application? ⇒ Boolean
27 28 29 30 31 32 |
# File 'lib/database_yml.rb', line 27 def self.in_rails_application? file_exist = File.exist?('Gemfile') return false unless file_exist open('Gemfile') { |f| !f.grep(/rails/).empty? } end |
Instance Method Details
#create! ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/database_yml.rb', line 14 def create! return unless DRIVERS.include? @database if self.class.in_rails_application? generate_database_yml! return else puts "Must be executed in a rails directory" end end |
#generate_database_yml! ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/database_yml.rb', line 34 def generate_database_yml! template = File.open(File.join(TEMPLATES_DIR, @database)).read output_file = File.new(File.join(Dir.pwd, "config", "database.yml"), "w") username = Etc.getlogin application_name = File.basename(Dir.getwd) template.gsub!("USERNAME", username) template.gsub!("APPLICATION_NAME", application_name) output_file.write(template) puts "database.yml created for #{@database}" end |