Class: Redwood::ContactManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/sup/contact.rb

Instance Method Summary collapse

Methods included from Singleton

included

Constructor Details

#initialize(fn) ⇒ ContactManager

Returns a new instance of ContactManager.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sup/contact.rb', line 6

def initialize fn
  @fn = fn

  ## maintain the mapping between people and aliases. for contacts without
  ## aliases, there will be no @a2p entry, so @p2a.keys should be treated
  ## as the canonical list of contacts.

  @p2a = {} # person to alias
  @a2p = {} # alias to person

  if File.exists? fn
    IO.foreach(fn) do |l|
      l =~ /^([^:]*): (.*)$/ or raise "can't parse #{fn} line #{l.inspect}"
      aalias, addr = $1, $2
      p = Person.from_address addr
      @p2a[p] = aalias
      @a2p[aalias] = p unless aalias.nil? || aalias.empty?
    end
  end
end

Instance Method Details

#alias_for(person) ⇒ Object



47
# File 'lib/sup/contact.rb', line 47

def alias_for person; @p2a[person] end

#contact_for(aalias) ⇒ Object



46
# File 'lib/sup/contact.rb', line 46

def contact_for aalias; @a2p[aalias] end

#contactsObject



27
# File 'lib/sup/contact.rb', line 27

def contacts; @p2a.keys end

#contacts_with_aliasesObject



28
# File 'lib/sup/contact.rb', line 28

def contacts_with_aliases; @a2p.values.uniq end

#drop_contact(person) ⇒ Object

this may not actually be called anywhere, since we still keep contacts around without aliases to override any fullname changes.



40
41
42
43
44
# File 'lib/sup/contact.rb', line 40

def drop_contact person
  aalias = @p2a[person]
  @p2a.delete person
  @a2p.delete aalias if aalias
end

#is_aliased_contact?(person) ⇒ Boolean

Returns:

  • (Boolean)


48
# File 'lib/sup/contact.rb', line 48

def is_aliased_contact? person; !@p2a[person].nil? end

#saveObject



50
51
52
53
54
55
56
# File 'lib/sup/contact.rb', line 50

def save
  File.open(@fn, "w") do |f|
    @p2a.sort_by { |(p, a)| [p.full_address, a] }.each do |(p, a)|
      f.puts "#{a || ''}: #{p.full_address}"
    end
  end
end

#update_alias(person, aalias = nil) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/sup/contact.rb', line 30

def update_alias person, aalias=nil
  if(old_aalias = @p2a[person]) # remove old alias
    @a2p.delete old_aalias
  end
  @p2a[person] = aalias
  @a2p[aalias] = person unless aalias.nil? || aalias.empty?
end