Sha256: 1db1a093217b550bce362576d060a3415a76da037acf67d16a0265584d3a0e6e

Contents?: true

Size: 840 Bytes

Versions: 5

Compression:

Stored size: 840 Bytes

Contents

class Ppl::Format::AddressBook::OneLine < Ppl::Format::AddressBook

  attr_writer :table

  def initialize(colors={})
    @table = Ppl::Format::Table.new([:id, :name, :email], colors)
  end

  def process(address_book)
    address_book.contacts.each { |contact| add_row(contact) }
    @table.to_s
  end

  private

  def add_row(contact)
    id    = sprintf("%s:", contact.id)
    name  = contact.name
    email = nil

    if !contact.email_addresses.empty?
      email = sprintf("<%s>", choose_email_address(contact).address)
    end

    @table.add_row({
      :id    => id,
      :name  => name,
      :email => email,
    })
  end

  def choose_email_address(contact)
    preferred = contact.email_addresses.find { |e| e.preferred }
    if preferred.nil?
      contact.email_addresses.first
    else
      preferred
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ppl-4.0.5 lib/ppl/format/address_book/one_line.rb
ppl-4.0.3 lib/ppl/format/address_book/one_line.rb
ppl-4.0.2 lib/ppl/format/address_book/one_line.rb
ppl-4.0.1 lib/ppl/format/address_book/one_line.rb
ppl-4.0.0 lib/ppl/format/address_book/one_line.rb