Class: Ppl::Command::Mv

Inherits:
Application::Command show all
Defined in:
lib/ppl/command/mv.rb

Instance Attribute Summary

Attributes inherited from Application::Command

#storage

Instance Method Summary collapse

Methods inherited from Application::Command

add_property

Instance Method Details

#execute(input, output) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ppl/command/mv.rb', line 10

def execute(input, output)
  old_id  = input.arguments.shift
  new_id  = input.arguments.shift

  if old_id.nil?
    raise Ppl::Error::IncorrectUsage, "No contact specified"
  end

  if new_id.nil?
    raise Ppl::Error::IncorrectUsage, "No new ID specified"
  end

  old_contact = @storage.require_contact(old_id)
  new_contact = @storage.load_contact(new_id)

  if !new_contact.nil?
    output.error("There is already a contact with ID '#{new_id}'")
    return false
  end

  @storage.delete_contact(old_contact)

  old_contact.id = new_id
  @storage.save_contact(old_contact)

  return true
end

#options(parser, options) ⇒ Object



6
7
8
# File 'lib/ppl/command/mv.rb', line 6

def options(parser, options)
  parser.banner = "usage: ppl mv <contact> <new ID>"
end