Class: Mycmd::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/mycmd/configuration.rb

Constant Summary collapse

CONFIG_FILE =
".mycmd.yml"
VALID_OPTIONS_KEYS =
[
  :host,
  :username,
  :password,
  :port,
  :database,
  :socket,
  :flags,
  :encoding,
  :read_timeout,
  :write_timeout,
  :connect_timeout,
  :reconnect,
  :local_infile,
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



27
28
29
# File 'lib/mycmd/configuration.rb', line 27

def initialize
  reset
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



25
26
27
# File 'lib/mycmd/configuration.rb', line 25

def path
  @path
end

#tasksObject

Returns the value of attribute tasks.



24
25
26
# File 'lib/mycmd/configuration.rb', line 24

def tasks
  @tasks
end

Class Method Details

.config_find(path = File.expand_path(".")) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mycmd/configuration.rb', line 80

def config_find(path = File.expand_path("."))
  file = File.join(path, CONFIG_FILE)
  if File.exists?(file)
    file
  else
    if path == "/"
      file = File.join(ENV["HOME"], CONFIG_FILE)
      File.exists?(file) ? file : nil
    else
      config_find(File.expand_path("..", path))
    end
  end
end

.connectObject



65
66
67
68
# File 'lib/mycmd/configuration.rb', line 65

def connect
  conf = Configuration.new
  conf.connect
end

.get_variablesObject



70
71
72
73
74
75
76
77
78
# File 'lib/mycmd/configuration.rb', line 70

def get_variables
  sql = "SELECT GV.* FROM INFORMATION_SCHEMA.GLOBAL_VARIABLES AS GV"
  client = self.connect
  variables = {}
  client.query(sql).each do |row|
    variables.store(row["VARIABLE_NAME"].downcase.to_sym, row["VARIABLE_VALUE"])
  end
  variables
end

Instance Method Details

#connectObject



45
46
47
# File 'lib/mycmd/configuration.rb', line 45

def connect
  Mysql2::Client.new(to_hash)
end

#defaultObject



58
59
60
61
62
# File 'lib/mycmd/configuration.rb', line 58

def default
  self.host = "localhost"
  self.port = 3306
  self.username = "root"
end

#merge(params) ⇒ Object



31
32
33
34
35
36
# File 'lib/mycmd/configuration.rb', line 31

def merge(params)
  default
  params.each do |k,v|
    self.send("#{k.to_s}=", v)
  end
end

#resetObject



49
50
51
52
53
54
55
56
# File 'lib/mycmd/configuration.rb', line 49

def reset
  @path = Configuration.config_find
  if @path
    merge YAML.load_file(@path)
  else
    default
  end
end

#to_hashObject



38
39
40
41
42
43
# File 'lib/mycmd/configuration.rb', line 38

def to_hash
  VALID_OPTIONS_KEYS.inject({}) do |c,k|
    c.store(k.to_sym, self.send(k.to_s)) unless self.send(k.to_s).nil?
    c
  end
end