Class: BitlbeeConfig::Config
- Inherits:
-
Object
- Object
- BitlbeeConfig::Config
- Defined in:
- lib/bitlbee_config/config.rb
Overview
A configuration file for BitlBee. Has exactly one user
Instance Attribute Summary collapse
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
-
.delete_from_directory_for_user(directory, username) ⇒ Object
Deletes file for a specified user name from the given directory.
-
.from_directory(directory) ⇒ Array<BitlbeeConfig::User>
Create a config for every user XML in the given directory.
-
.from_directory_for_user(directory, username) ⇒ BitlbeeConfig::User|nil
Create a config for one specific user, reading the XML from a directory.
-
.from_file(file_path) ⇒ BitlbeeConfig::Config
Create a config from an XML file.
-
.from_xml(xml) ⇒ BitlbeeConfig::Config
The created configuration object.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Config
constructor
A new instance of Config.
-
#save_to_directory(path_to_dir) ⇒ Object
Saves the configuration to a specified directory User files are named <username.downcase>.xml.
- #to_xml ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Config
Returns a new instance of Config.
57 58 59 |
# File 'lib/bitlbee_config/config.rb', line 57 def initialize( = {}) @user = .delete(:user) end |
Instance Attribute Details
#user ⇒ Object
Returns the value of attribute user.
4 5 6 |
# File 'lib/bitlbee_config/config.rb', line 4 def user @user end |
Class Method Details
.delete_from_directory_for_user(directory, username) ⇒ Object
Deletes file for a specified user name from the given directory
49 50 51 52 |
# File 'lib/bitlbee_config/config.rb', line 49 def delete_from_directory_for_user(directory, username) file_to_delete = File.join(directory, BitlbeeConfig::User.username_to_filename(username)) File.delete(file_to_delete) if File.exist?(file_to_delete) end |
.from_directory(directory) ⇒ Array<BitlbeeConfig::User>
Create a config for every user XML in the given directory
11 12 13 14 15 |
# File 'lib/bitlbee_config/config.rb', line 11 def from_directory(directory) Dir.glob(File.join(directory, "*.xml")).each_with_object([]) do |path_to_file, config_collection| config_collection << BitlbeeConfig::Config.from_file(path_to_file) end end |
.from_directory_for_user(directory, username) ⇒ BitlbeeConfig::User|nil
Create a config for one specific user, reading the XML from a directory
22 23 24 |
# File 'lib/bitlbee_config/config.rb', line 22 def from_directory_for_user(directory, username) from_file(File.join(directory, BitlbeeConfig::User.username_to_filename(username))) end |
.from_file(file_path) ⇒ BitlbeeConfig::Config
Create a config from an XML file
30 31 32 |
# File 'lib/bitlbee_config/config.rb', line 30 def from_file(file_path) from_xml(File.read(file_path)) end |
.from_xml(xml) ⇒ BitlbeeConfig::Config
Returns The created configuration object.
36 37 38 39 40 41 42 43 |
# File 'lib/bitlbee_config/config.rb', line 36 def from_xml(xml) doc = Nokogiri::XML(xml) # Bitlbee config says there can be only one user per XML file user_xml = doc.xpath("//user").first BitlbeeConfig::Config.new(user: BitlbeeConfig::User.from_xml(user_xml)) end |
Instance Method Details
#save_to_directory(path_to_dir) ⇒ Object
Saves the configuration to a specified directory User files are named <username.downcase>.xml
73 74 75 76 77 78 79 |
# File 'lib/bitlbee_config/config.rb', line 73 def save_to_directory(path_to_dir) user_file_path = File.join(path_to_dir, "#{ @user.nick.downcase }.xml") File.open(user_file_path, "w") do |file| file.write(to_xml) end end |
#to_xml ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/bitlbee_config/config.rb', line 61 def to_xml builder = Nokogiri::XML::Builder.new do |xml_builder| @user.build_xml(xml_builder) end builder.doc.root.to_xml end |