Class: Rubycon::Config

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Config

Returns a new instance of Config.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rubycon/config.rb', line 5

def initialize(path)
  @servers = []
  @path = path

  if File.exists?(path)
    yaml = YAML.load_file(path)
    yaml.each do |server|
      @servers << server
    end
  end
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/rubycon/config.rb', line 3

def path
  @path
end

#serversObject (readonly)

Returns the value of attribute servers.



3
4
5
# File 'lib/rubycon/config.rb', line 3

def servers
  @servers
end

Class Method Details

.load(path) ⇒ Object



17
18
19
# File 'lib/rubycon/config.rb', line 17

def self.load(path)
  Config.new path
end

Instance Method Details

#add_server(server) ⇒ Object

Raises:



30
31
32
33
34
# File 'lib/rubycon/config.rb', line 30

def add_server(server)
  raise ConfigError, "ALIAS #{server.name} already used." if find_by_name server.name
  @servers << server
  save
end

#delete_all_serversObject



36
37
38
39
# File 'lib/rubycon/config.rb', line 36

def delete_all_servers
  @servers = []
  save
end

#delete_server(name) ⇒ Object



41
42
43
44
45
# File 'lib/rubycon/config.rb', line 41

def delete_server(name)
  deleted_server = @servers.delete(find_by_name(name))
  save
  deleted_server
end

#find_by_host_and_port(address, port) ⇒ Object



51
52
53
# File 'lib/rubycon/config.rb', line 51

def find_by_host_and_port(address, port)
  @servers.find {|server| port == server.port and server.address.downcase == address.downcase}
end

#find_by_name(name) ⇒ Object



47
48
49
# File 'lib/rubycon/config.rb', line 47

def find_by_name(name)
  @servers.find {|server| name.downcase == server.name.downcase}
end

#saveObject



21
22
23
24
25
26
27
28
# File 'lib/rubycon/config.rb', line 21

def save
  FileUtils.makedirs File.dirname(@path)

  servers.sort! {|a,b| a.name.downcase <=> b.name.downcase}
  File.open(path, 'w') do |f|
    f.puts servers.to_yaml
  end
end