Class: RubybenchRunner::Configurations

Inherits:
Object
  • Object
show all
Defined in:
lib/rubybench_runner/configurations.rb

Constant Summary collapse

CONFIG_PATH =
File.join(Dir.home, ".rubybench_runner_config")
CONFIG_VERSION =
1
MYSQL_MAPPING =
{
  user: :username,
  dbname: :database
}
DEFAULTS =
{
  postgres: {
    user: "postgres",
    dbname: "rubybench",
    host: "localhost",
    port: 5432,
    password: nil
  },
  mysql2: {
    user: "root",
    dbname: "rubybench",
    host: "localhost",
    port: 3306,
    password: nil
  },
  config_version: CONFIG_VERSION
}

Instance Method Summary collapse

Constructor Details

#initialize(mysql_map: false) ⇒ Configurations

Returns a new instance of Configurations.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubybench_runner/configurations.rb', line 26

def initialize(mysql_map: false)
  @mysql_map = mysql_map
  if !File.exists?(CONFIG_PATH)
    File.write(CONFIG_PATH, YAML.dump(DEFAULTS))
  end

  if !config_changed?
    puts "Error: You haven't configured how RubybenchRunner should connect to the database servers on your machine. Please update the #{CONFIG_PATH} file to have the right configurations."
    exit 1
  end
end

Instance Method Details

#[](key) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/rubybench_runner/configurations.rb', line 38

def [](key)
  key = key.to_s
  result = config
  key.split(".").each do |k|
    result = result[k] || result[k.to_sym]
  end
  result
end

#configObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubybench_runner/configurations.rb', line 47

def config
  content = File.read(CONFIG_PATH)
  if content != @content
    @content = content
    @parsed = YAML.load(@content)
    if @mysql_map
      MYSQL_MAPPING.each do |old, new|
        next if !@parsed[:mysql2].key?(old)
        val = @parsed[:mysql2][old]
        @parsed[:mysql2][new] = val
        @parsed[:mysql2].delete(old)
      end
    end
  end
  @parsed
end

#config_changed?Boolean

Returns:



64
65
66
# File 'lib/rubybench_runner/configurations.rb', line 64

def config_changed?
  File.read(CONFIG_PATH) != YAML.dump(DEFAULTS)
end