Class: Rubycon::Config
- Inherits:
-
Object
- Object
- Rubycon::Config
- Defined in:
- lib/rubycon/config.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#servers ⇒ Object
readonly
Returns the value of attribute servers.
Class Method Summary collapse
Instance Method Summary collapse
- #add_server(server) ⇒ Object
- #delete_all_servers ⇒ Object
- #delete_server(name) ⇒ Object
- #find_by_host_and_port(address, port) ⇒ Object
- #find_by_name(name) ⇒ Object
-
#initialize(path) ⇒ Config
constructor
A new instance of Config.
- #save ⇒ Object
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
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/rubycon/config.rb', line 3 def path @path end |
#servers ⇒ Object (readonly)
Returns the value of attribute servers.
3 4 5 |
# File 'lib/rubycon/config.rb', line 3 def servers @servers end |
Class Method Details
Instance Method Details
#add_server(server) ⇒ Object
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_servers ⇒ Object
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 |
#save ⇒ Object
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 |