Class: Fluidfeatures::ConfigGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/fluidfeatures/config_generator.rb

Constant Summary collapse

CONFIG_FILE =
"config/fluidfeatures.yml"

Instance Method Summary collapse

Instance Method Details

#create_initializer_fileObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/generators/fluidfeatures/config_generator.rb', line 6

def create_initializer_file

  if File.file? CONFIG_FILE
    config = YAML.load_file(CONFIG_FILE)
  else
    config = {
      "common" => {
        "base_uri" => "https://www.fluidfeatures.com/service",
        "cache" => {
          "enable" => false,
          "dir" => "tmp/fluidfeatures",
          "limit" => "10MB"
        },
      },
      "development" => {},
      "production" => {},
      "test" => {},
    }
  end

  puts "Enter FluidFeatures credentials [press enter to skip]"
  ["development", "production", "test"].each do |env|
    config[env] ||= {}
    ["app_id", "secret", "base_uri"].each do |key|
      if config[env][key] and config[env][key].size > 0
        default = config[env][key]
      elsif config["common"][key] and config["common"][key].size
        default = config["common"][key]
      end
      print "%s %s %s: " % [
        env,
        key,
        default ? "[#{default}]" : ""
      ]
      value = gets.chomp.strip
      if value and value.size > 0
        config[env][key] = value
      elsif default
        config[env][key] = default
      else
        config[env][key] = nil
      end
    end
    if config[env]["base_uri"] == config["common"]["base_uri"]
      config[env].delete("base_uri")
    end
  end

  yaml = YAML.dump(config)
  # remove "---" at top
  yaml.sub!(/^---\r?\n/, "")
  # add extra line between environments
  yaml.gsub!(/(\r?\n)(\S)/) {|m| $1 + $1 + $2 }

  create_file CONFIG_FILE, yaml
end