Class: ListTool::App::ReplaceItemCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/list_tool/app/commands/replace_item_command.rb

Class Method Summary collapse

Class Method Details

.execute(options, lister) ⇒ Object

Raises:



22
23
24
25
# File 'lib/list_tool/app/commands/replace_item_command.rb', line 22

def self.execute options, lister
  args = [ options[:item], options[:name] ]
  raise(ItemNotFoundError, 'no item with given number') if lister.change_item(*args).nil?
end

.helpObject



27
28
29
# File 'lib/list_tool/app/commands/replace_item_command.rb', line 27

def self.help
  "  r,  replace-item ITEM, TEXT\tSet ITEM text to TEXT"
end

.match?(arg) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/list_tool/app/commands/replace_item_command.rb', line 6

def self.match? arg
  ['r', 'replace-item'].include? arg
end

.parse(argv) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/list_tool/app/commands/replace_item_command.rb', line 10

def self.parse argv
  raise ArgumentError, "expected argument to be an array, #{argv.class} given" unless argv.is_a? Array
  item, name = argv.shift(2)
  raise ArgumentError, 'item number not specified' if item.nil?
  raise ArgumentError, 'new name not specified' if name.nil?
  
  item = Integer(item) - 1 rescue raise(ArgumentError, 'item number must be an integer')
  raise ArgumentError, "item number can't be less than 1" if item < 0

  {name: name, item: item}
end