Class: Mysqlknife::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mysqlknife/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Config

Returns a new instance of Config.



7
8
9
10
11
12
# File 'lib/mysqlknife/config.rb', line 7

def initialize(file = nil)
  unless file.nil?
    path     = File.expand_path(File.join(Dir.pwd, file))
    @configs = YAML.load_file(path) if File.exist?(path)
  end
end

Instance Method Details

#defaults(options) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/mysqlknife/config.rb', line 31

def defaults(options)
  { host: 'localhost',
    port: '3306',
    user: 'root',
    password: '' }
  .merge(options)
end

#dsn(options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mysqlknife/config.rb', line 39

def dsn(options)
  options.split(',')
         .map { |c| c.split('=', 2) }
         .reduce({}) do |m, (key, value)|
    case key.to_sym
    when :h
      key = :host
    when :u
      key = :user
    when :p
      key = :password
    when :P
      key = :port
    when :d
      key = :database
    end
    m[key] = value
    m
  end
end

#getObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mysqlknife/config.rb', line 14

def get
  if !@configs.nil? && !ENV['ENV'].nil?
    # Convert string keys to symbols in hash:
    @configs = @configs[ENV['ENV']].reduce({}) do |config, (k, v)|
      config[k.to_sym] = v
      config
    end

    # Add defaults values:
    defaults(@configs)
  end
end

#symbolize_keys(options) ⇒ Object



27
28
29
# File 'lib/mysqlknife/config.rb', line 27

def symbolize_keys(options)
  options.inject({}){|hash,(k,v)| hash[k.to_sym] = v; hash}
end