Module: Configuration::Tasks::Generate

Defined in:
lib/configuration/tasks/generate.rb

Overview

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.

Class Method Summary collapse

Class Method Details

.generate_ymlsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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_filesObject



45
46
47
48
49
# 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