Sha256: 9a721a79eee53a3a591b114bd46d3a9a7d08c65e41c42655e6b6e313c609810f

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

module Redwood

class ContactManager
  include Singleton

  def initialize fn
    @fn = fn
    @p2a = {} # person to alias map
    @a2p = {} # alias to person map

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

    self.class.i_am_the_instance self
  end

  def contacts; @p2a.keys; end
  def set_contact person, aalias
    if(pold = @a2p[aalias]) && (pold != person)
      drop_contact pold
    end
    @p2a[person] = aalias
    @a2p[aalias] = person
  end
  def drop_contact person
    if(aalias = @p2a[person])
      @p2a.delete person
      @a2p.delete aalias
    end
  end    
  def person_with aalias; @a2p[aalias]; end
  def alias_for person; @p2a[person]; end
  def is_contact? person; @p2a.member? person; end
  def save
    File.open(@fn, "w") do |f|
      @p2a.each do |p, a|
        f.puts "#{a}: #{p.full_address}"
      end
    end
  end
end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sup-0.0.8 lib/sup/contact.rb
sup-0.0.7 lib/sup/contact.rb