Method: Command::Alias#execute

Defined in:
lib/command/alias.rb

#execute(argv) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/command/alias.rb', line 49

def execute(argv)
  super
  if argv.empty?
    puts @opt.help
    return
  end
  aliases = Inventory.load("alias")
  argv.each_with_index do |arg, i|
    Helper.print_horizontal_rule if i > 0
    alias_name, target = arg.split("=", 2)
    if BAN_WORD.include?(alias_name)
      error "#{alias_name} は使用禁止ワードです"
      next
    end
    unless alias_name =~ /^\w+$/
      error "別名にはアルファベット・数字・アンダースコアしか使えません"
      next
    end
    if target.nil?
      error "書式が間違っています。#{alias_name}=別名 のように書いて下さい"
      next
    end
    if target == ""
      aliases.delete(alias_name)
      puts "#{alias_name} を解除しました"
      next
    end
    unless Downloader.novel_exists?(target)
      error "#{target} は存在しません"
      next
    end
    data = Downloader.get_data_by_target(target)
    id = data["id"]
    title = data["title"]
    aliases[alias_name] = id
    puts "#{alias_name} を #{title} の別名に設定しました"
  end
  aliases.save
end