Class: Tableflip::DatabaseHandle

Inherits:
Object
  • Object
show all
Defined in:
lib/tableflip/database_handle.rb

Constant Summary collapse

DATABASE_CONFIG_FILE =

Constants ============================================================

'database.yml'
DEFAULT_OPTIONS =
{
  :symbolize_keys => true,
  :encoding => 'utf-8'
}.freeze
PARAM_MAP =
Hash.new do |h, k|
  k.to_sym
end

Class Method Summary collapse

Class Method Details

.configObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tableflip/database_handle.rb', line 39

def self.config
  @config ||= begin
    _config_path = self.config_path

    if (!_config_path)
      STDERR.puts("Could not find #{DATABASE_CONFIG_FILE}")
      exit(-1)
    elsif (File.exists?(_config_path))
      File.open(_config_path) do |f|
        YAML.load(f)
      end
    else
      STDERR.puts "Could not open #{_config_path}"
      exit(-1)
    end
  end
end

.config_pathObject

Class Methods ========================================================



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tableflip/database_handle.rb', line 21

def self.config_path
  path = Dir.pwd
  last_path = nil

  while (path != last_path)
    config_path = File.expand_path("config/#{DATABASE_CONFIG_FILE}", path)

    if (File.exist?(config_path))
      return config_path
    end

    last_path = path
    path = File.expand_path('..', path)
  end

  nil
end

.connect(env, options) ⇒ Object



79
80
81
# File 'lib/tableflip/database_handle.rb', line 79

def self.connect(env, options)
  Mysql2::EM::Client.new(self.environment_config(env).merge(options))
end

.environment_config(env) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tableflip/database_handle.rb', line 61

def self.environment_config(env)
  _config = self.config[env]

  unless (_config)
    raise "No environment #{env} defined in #{self.config_path}"
  end

  options = DEFAULT_OPTIONS.dup

  _config.each do |k, v|
    options[PARAM_MAP[k]] = v
  end

  options[:loggers] = [ ]

  options
end

.runtime_environmentObject



57
58
59
# File 'lib/tableflip/database_handle.rb', line 57

def self.runtime_environment
  DAEMON_ENV or 'development'
end