Class: BitlbeeConfig::User
- Inherits:
-
Object
- Object
- BitlbeeConfig::User
- Includes:
- XmlBuildable
- Defined in:
- lib/bitlbee_config/user.rb
Overview
A BitlBee user account - has channels for control / chat, and accounts with various IM services
Instance Attribute Summary collapse
-
#accounts ⇒ Object
Returns the value of attribute accounts.
-
#channels ⇒ Object
Returns the value of attribute channels.
-
#cleartext_password ⇒ Object
Returns the value of attribute cleartext_password.
-
#nick ⇒ Object
Returns the value of attribute nick.
-
#password ⇒ Object
Returns the value of attribute password.
-
#settings ⇒ Object
Returns the value of attribute settings.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
-
.from_xml(xml) ⇒ BitlbeeConfig::User
Rubocop thinks this method is too long, but I think it’s more readable than split up rubocop:disable MethodLength.
-
.username_to_filename(username) ⇒ String
XML configuration files are currently saved with the downcased username and an xml extension.
Instance Method Summary collapse
-
#add_or_replace_account(new_account) ⇒ Object
Add an account.
- #build_xml(xml_builder) ⇒ Object
-
#initialize(options = {}) ⇒ User
constructor
A new instance of User.
-
#regenerate_password_if_needed ⇒ Object
If a cleartext password is given, hash it and set password attribute.
-
#remove_account(account_to_remove) ⇒ Object
Remove an account.
-
#remove_account_by_id(id_to_remove) ⇒ Object
Remove an account by its id.
Methods included from XmlBuildable
Constructor Details
#initialize(options = {}) ⇒ User
Returns a new instance of User.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bitlbee_config/user.rb', line 60 def initialize( = {}) @nick = .delete(:nick) @password = .delete(:password) @cleartext_password = .delete(:cleartext_password) @version = .delete(:version) || "1" @channels = .delete(:channels) || [] @accounts = .delete(:accounts) || [] @settings = || {} end |
Instance Attribute Details
#accounts ⇒ Object
Returns the value of attribute accounts.
6 7 8 |
# File 'lib/bitlbee_config/user.rb', line 6 def accounts @accounts end |
#channels ⇒ Object
Returns the value of attribute channels.
6 7 8 |
# File 'lib/bitlbee_config/user.rb', line 6 def channels @channels end |
#cleartext_password ⇒ Object
Returns the value of attribute cleartext_password.
6 7 8 |
# File 'lib/bitlbee_config/user.rb', line 6 def cleartext_password @cleartext_password end |
#nick ⇒ Object
Returns the value of attribute nick.
6 7 8 |
# File 'lib/bitlbee_config/user.rb', line 6 def nick @nick end |
#password ⇒ Object
Returns the value of attribute password.
6 7 8 |
# File 'lib/bitlbee_config/user.rb', line 6 def password @password end |
#settings ⇒ Object
Returns the value of attribute settings.
6 7 8 |
# File 'lib/bitlbee_config/user.rb', line 6 def settings @settings end |
#version ⇒ Object
Returns the value of attribute version.
6 7 8 |
# File 'lib/bitlbee_config/user.rb', line 6 def version @version end |
Class Method Details
.from_xml(xml) ⇒ BitlbeeConfig::User
Rubocop thinks this method is too long, but I think it’s more readable than split up rubocop:disable MethodLength
14 15 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 |
# File 'lib/bitlbee_config/user.rb', line 14 def from_xml(xml) new_user = {} %w(nick password version).each do |att| new_user[att.to_sym] = xml.attributes[att].value end xml.xpath("setting").each do |setting| new_user[setting.attributes["name"].value] = setting.text end new_user[:channels] = xml.xpath("channel").collect do |item| BitlbeeConfig::Channel.from_xml(item) end new_user[:accounts] = xml.xpath("account").collect do |item| BitlbeeConfig::Account.from_xml(item) end user = BitlbeeConfig::User.new(new_user) user.accounts.each do |account| account.user = user end user end |
.username_to_filename(username) ⇒ String
XML configuration files are currently saved with the downcased username and an xml extension
47 48 49 |
# File 'lib/bitlbee_config/user.rb', line 47 def username_to_filename(username) "#{ username.downcase }.xml" end |
Instance Method Details
#add_or_replace_account(new_account) ⇒ Object
Add an account. If an account with the same id already exists, it will be replaced
74 75 76 77 78 79 |
# File 'lib/bitlbee_config/user.rb', line 74 def add_or_replace_account(new_account) @accounts.reject! { |account| account.id == new_account.id } new_account.user = self @accounts << new_account end |
#build_xml(xml_builder) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/bitlbee_config/user.rb', line 101 def build_xml(xml_builder) regenerate_password_if_needed (xml_builder, nick: @nick, password: @password, version: @version) do |user_xml| @accounts.each do |account| account.build_xml(user_xml) end @channels.each do |channel| channel.build_xml(user_xml) end end end |
#regenerate_password_if_needed ⇒ Object
If a cleartext password is given, hash it and set password attribute
96 97 98 |
# File 'lib/bitlbee_config/user.rb', line 96 def regenerate_password_if_needed @password = @cleartext_password.to_bitlbee_password_hash if @cleartext_password end |
#remove_account(account_to_remove) ⇒ Object
Remove an account
91 92 93 |
# File 'lib/bitlbee_config/user.rb', line 91 def remove_account(account_to_remove) remove_account_by_id(account_to_remove.id) end |
#remove_account_by_id(id_to_remove) ⇒ Object
Remove an account by its id
84 85 86 |
# File 'lib/bitlbee_config/user.rb', line 84 def remove_account_by_id(id_to_remove) @accounts.reject! { |account| account.id == id_to_remove } end |