Class: Redwood::AccountManager
- Includes:
- Singleton
- Defined in:
- lib/sup/account.rb
Instance Attribute Summary collapse
-
#default_account ⇒ Object
Returns the value of attribute default_account.
Instance Method Summary collapse
- #account_for(email) ⇒ Object
-
#add_account(hash, default = false) ⇒ Object
must be called first with the default account.
- #full_address_for(email) ⇒ Object
-
#initialize(accounts) ⇒ AccountManager
constructor
A new instance of AccountManager.
- #is_account?(p) ⇒ Boolean
- #is_account_email?(email) ⇒ Boolean
- #user_accounts ⇒ Object
- #user_emails ⇒ Object
Methods included from Singleton
Constructor Details
#initialize(accounts) ⇒ AccountManager
Returns a new instance of AccountManager.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/sup/account.rb', line 32 def initialize accounts @email_map = {} @accounts = {} @regexen = {} @default_account = nil fail "default account missing in config" unless accounts[:default].kind_of? Hash add_account accounts[:default], true accounts.each { |k, v| add_account v, false unless k == :default } end |
Instance Attribute Details
#default_account ⇒ Object
Returns the value of attribute default_account.
30 31 32 |
# File 'lib/sup/account.rb', line 30 def default_account @default_account end |
Instance Method Details
#account_for(email) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/sup/account.rb', line 79 def account_for email if(a = @email_map[email]) a else @regexen.argfind { |re, a| re =~ email && a } end end |
#add_account(hash, default = false) ⇒ Object
must be called first with the default account. fills in missing values from the default account.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sup/account.rb', line 48 def add_account hash, default=false raise ArgumentError, "no email specified for account" unless hash[:email] unless default [:name, :sendmail, :signature, :gpgkey].each { |k| hash[k] ||= @default_account.send(k) } end hash[:alternates] ||= [] fail "alternative emails are not an array: #{hash[:alternates]}" unless hash[:alternates].kind_of? Array raise ArgumentError, "no sendmail command specified for account" unless hash[:sendmail] [:name, :signature].each { |x| hash[x] ? hash[x].fix_encoding! : nil } a = Account.new hash @accounts[a] = true if default raise ArgumentError, "multiple default accounts" if @default_account @default_account = a end ([hash[:email]] + hash[:alternates]).each do |email| next if @email_map.member? email @email_map[email] = a end hash[:regexen].each do |re| @regexen[Regexp.new(re)] = a end if hash[:regexen] end |
#full_address_for(email) ⇒ Object
86 87 88 89 |
# File 'lib/sup/account.rb', line 86 def full_address_for email a = account_for email Person.full_address a.name, email end |
#is_account?(p) ⇒ Boolean
77 |
# File 'lib/sup/account.rb', line 77 def is_account? p; is_account_email? p.email end |
#is_account_email?(email) ⇒ Boolean
78 |
# File 'lib/sup/account.rb', line 78 def is_account_email? email; !account_for(email).nil? end |
#user_accounts ⇒ Object
43 |
# File 'lib/sup/account.rb', line 43 def user_accounts; @accounts.keys; end |