Sha256: 4a28a1eb5582da9c2bde285d4815fde4ef1dc307ac588e0d40c686612d489c19

Contents?: true

Size: 1.7 KB

Versions: 13

Compression:

Stored size: 1.7 KB

Contents

class Ppl::Command::Bday < Ppl::Application::Command

  attr_writer :list_format
  attr_writer :show_format

  def initialize
    @name        = "bday"
    @description = "List, show or change birthdays"
    @list_format = Ppl::Format::AddressBook::Birthdays.new
    @show_format = Ppl::Format::Contact::Birthday.new
  end

  def options(parser, options)
    parser.banner = "usage: ppl birthday <contact> [<date>]"
  end

  def execute(input, output)
    action = determine_action(input)
    send(action, input, output)
  end


  private

  def determine_action(input)
    if input.arguments[0].nil?
      :list_birthdays
    elsif input.arguments[1].nil?
      :show_birthday
    else
      :set_birthday
    end
  end

  def list_birthdays(input, output)
    address_book  = @storage.load_address_book
    birthday_list = @list_format.process(address_book)
    output.line(birthday_list)
  end

  def show_birthday(input, output)
    contact_id = input.arguments.shift
    if contact_id.nil?
      raise Ppl::Error::IncorrectUsage, "No contact specified"
    end
    contact = @storage.require_contact(contact_id)

    birthday = @show_format.process(contact)
    if birthday != ""
      output.line(birthday)
      true
    else
      false
    end
  end

  def set_birthday(input, output)
    contact_id = input.arguments.shift
    birthday   = input.arguments.shift

    if contact_id.nil?
      raise Ppl::Error::IncorrectUsage, "No contact specified"
    end
    contact = @storage.require_contact(contact_id)

    begin
      date = Date.parse birthday
    rescue ArgumentError
      raise Ppl::Error::IncorrectUsage, "Invalid date '#{birthday}'"
    end
    contact.birthday = date
    @storage.save_contact(contact)
  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ppl-1.5.2 lib/ppl/command/bday.rb
ppl-1.5.1 lib/ppl/command/bday.rb
ppl-1.5.0 lib/ppl/command/bday.rb
ppl-1.4.1 lib/ppl/command/bday.rb
ppl-1.3.0 lib/ppl/command/bday.rb
ppl-1.2.0 lib/ppl/command/bday.rb
ppl-1.1.0 lib/ppl/command/bday.rb
ppl-1.0.6 lib/ppl/command/bday.rb
ppl-1.0.5 lib/ppl/command/bday.rb
ppl-1.0.4 lib/ppl/command/bday.rb
ppl-1.0.3 lib/ppl/command/bday.rb
ppl-1.0.1 lib/ppl/command/bday.rb
ppl-1.0.0 lib/ppl/command/bday.rb