Sha256: 39252b2617badb9d517411bbbaa2e8c75ebb636bfc81c7a44a0b1a39bd111895

Contents?: true

Size: 1.23 KB

Versions: 13

Compression:

Stored size: 1.23 KB

Contents

class Ppl::Command::Name < Ppl::Application::Command

  attr_writer :show_format
  attr_writer :list_format

  def initialize
    @name        = "name"
    @description = "List, show or change names"
    @show_format = Ppl::Format::Contact::Name.new
    @list_format = Ppl::Format::AddressBook::Names.new
  end

  def options(parser, options)
    parser.banner = "usage: ppl name <contact> [<name>]"
  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_names
    elsif input.arguments[1].nil?
      :show_name
    else
      :set_name
    end
  end

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

  def show_name(input, output)
    contact = @storage.require_contact(input.arguments[0])
    name    = @show_format.process(contact)
    if name != ""
      output.line(name)
      true
    else
      false
    end
  end

  def set_name(input, output)
    contact = @storage.require_contact(input.arguments[0])
    contact.name = input.arguments[1].dup
    @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/name.rb
ppl-1.5.1 lib/ppl/command/name.rb
ppl-1.5.0 lib/ppl/command/name.rb
ppl-1.4.1 lib/ppl/command/name.rb
ppl-1.3.0 lib/ppl/command/name.rb
ppl-1.2.0 lib/ppl/command/name.rb
ppl-1.1.0 lib/ppl/command/name.rb
ppl-1.0.6 lib/ppl/command/name.rb
ppl-1.0.5 lib/ppl/command/name.rb
ppl-1.0.4 lib/ppl/command/name.rb
ppl-1.0.3 lib/ppl/command/name.rb
ppl-1.0.1 lib/ppl/command/name.rb
ppl-1.0.0 lib/ppl/command/name.rb