module Configuration::Tasks::Generate

Provides a method to generate yaml files from sample or example files. This module and methods are intended to use with a rake task and are not available for the rest of application.

Examples:

# The following files
config/application.yml.sample
config/database.yml.sample
config/resque.yml.example

# will generate:
config/application.yml
config/database.yml
config/resque.yml

If the .yml file already exists and has the same content of the related .sample file, the .yml will remain untouched. Otherwise, user will be asked whether the content will be replaced or not.

Public Class Methods

generate_ymls() click to toggle source
# File lib/configuration/tasks/generate.rb, line 25
def generate_ymls
  grouped_files.each do |group, files|
    sample = files.find { |file| file =~ /\.yml\.(s|ex)ample$/ }
    destination = files.find { |file| file =~ /\.yml$/ }

    begin
      case files.count
      when 1
        generate(sample)
      when 2
        replace(sample, destination)
      else
        raise
      end
    rescue
      handle_error(group)
    end
  end
end
grouped_files() click to toggle source
# File lib/configuration/tasks/generate.rb, line 45
def grouped_files
  @grouped_files ||= Dir.glob("config/*.yml*").group_by do |file|
    file.match(/((config\/)([^.]*))/i)[3]
  end
end

Private Class Methods

ask(message) { || ... } click to toggle source
# File lib/configuration/tasks/generate.rb, line 78
def ask(message, &block)
  print "#{message} (yes/no): "
  yield if %w{y yes}.include? STDIN.gets.strip
end
generate(sample) click to toggle source
# File lib/configuration/tasks/generate.rb, line 53
def generate(sample)
  if sample =~ /\.(s|ex)ample$/
    destination = sample.sub(/\.(s|ex)ample/, '')
    FileUtils.cp(sample, destination)
    puts "#{destination} generated from #{sample}"
  end
end
handle_error(group) click to toggle source
# File lib/configuration/tasks/generate.rb, line 74
def handle_error(group)
  puts "An error ocurred when trying to copy config/#{group}.* files."
end
read_content(file) click to toggle source
# File lib/configuration/tasks/generate.rb, line 70
def read_content(file)
  File.open(file, 'r').readlines
end
replace(sample, destination) click to toggle source
# File lib/configuration/tasks/generate.rb, line 61
def replace(sample, destination)
  if read_content(sample) != read_content(destination)
    ask "Do you want to replace existing #{destination}?" do
      FileUtils.cp(sample, destination)
      puts "#{destination} content have been replaced by #{sample} content"
    end
  end
end