Class: Mysqlknife::Configs

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mysqlknife/configs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigs

Returns a new instance of Configs.



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
# File 'lib/mysqlknife/configs.rb', line 16

def initialize
  # Define path for config file:
  if ENV['ENV'] == 'test'
    path = File.expand_path(File.join(Dir.pwd, '.db.yml'))
  else
    path = File.expand_path("#{Dir.home}/.db.yml")
  end

  # Load config file:
  @configs = YAML.load_file(path) if File.exist?(path)

  if @configs.nil?
    puts "Not exist config file in #{path}"
    exit 1
  end

  # Define generic variables:
  @ssh   = {}
  @mysql = {}

  @ssh[:host]     = @configs['ssh']['host']
  @ssh[:port]     = @configs['ssh']['port']
  @ssh[:user]     = @configs['ssh']['user']
  @ssh[:password] = @configs['ssh']['password']
  @ssh[:key]      = @configs['ssh']['keys']
  @connections    = @configs['connections'].keys.sort
  @commands       = @configs['commands'].keys.sort
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mysqlknife/configs.rb', line 45

def method_missing(name, *args)
  method = name.to_s.split(/_/)
  if method.first == 'db'
    param = method[1]
    conn  = args[0]

    if @configs['connections'].include?(conn)
      @configs['connections'][conn][param]
    end
  end
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



8
9
10
# File 'lib/mysqlknife/configs.rb', line 8

def color
  @color
end

#commandsObject (readonly)

Returns the value of attribute commands.



8
9
10
# File 'lib/mysqlknife/configs.rb', line 8

def commands
  @commands
end

#connection(name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/mysqlknife/configs.rb', line 57

def connection(name)
  @name             = name
  @color            = db_color(@name)
  @ssh[:use]        = db_ssh(@name)
  @mysql[:host]     = db_host(@name)
  @mysql[:port]     = db_port(@name)
  @mysql[:username] = db_username(@name)
  @mysql[:password] = db_password(@name)
  @mysql[:slaves]   = db_slaves(@name)
end

#connectionsObject (readonly)

Returns the value of attribute connections.



8
9
10
# File 'lib/mysqlknife/configs.rb', line 8

def connections
  @connections
end

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/mysqlknife/configs.rb', line 8

def host
  @host
end

#mysqlObject (readonly)

Returns the value of attribute mysql.



8
9
10
# File 'lib/mysqlknife/configs.rb', line 8

def mysql
  @mysql
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/mysqlknife/configs.rb', line 8

def name
  @name
end

#sshObject (readonly)

Returns the value of attribute ssh.



8
9
10
# File 'lib/mysqlknife/configs.rb', line 8

def ssh
  @ssh
end

Instance Method Details

#command(name) ⇒ Object



84
85
86
87
88
# File 'lib/mysqlknife/configs.rb', line 84

def command(name)
  if @configs['commands'].key?(name)
    @configs['commands'][name]
  end
end

#hostsObject



68
69
70
71
72
73
# File 'lib/mysqlknife/configs.rb', line 68

def hosts
  (@mysql[:slaves] = []) if @mysql[:slaves].nil?
  (@mysql[:host]   = '') if @mysql[:host].nil?

  (@mysql[:host] + ',' + @mysql[:slaves].join(',')).split(',')
end

#select(name) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/mysqlknife/configs.rb', line 75

def select(name)
  if hosts.is_a?(Array)
    hosts.each do |host|
      return @host = host if host.include?(name)
    end
  end
  nil
end