Class: Command::Alias

Inherits:
CommandBase show all
Defined in:
lib/command/alias.rb

Instance Method Summary collapse

Methods inherited from CommandBase

execute_and_rescue_exit

Constructor Details

#initializeAlias

Returns a new instance of Alias.



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

def initialize
  super("[<alias_name>=<target> ...] [options]")
  @opt.separator "\n  \u30FB\u5C0F\u8AAC\u306EID\u306B\u7D10\u4ED8\u3051\u305F\u597D\u304D\u306A\u5225\u540D\u3092\u4F5C\u308B\u3053\u3068\u304C\u51FA\u6765\u307E\u3059\u3002ID\u3084N\u30B3\u30FC\u30C9\u7B49\u3092\u899A\u3048\u308B\u5FC5\u8981\u304C\u306A\u304F\u306A\u308A\u307E\u3059\u3002\n  \u30FB<alias_name>\u306B\u306F\u30A2\u30EB\u30D5\u30A1\u30D9\u30C3\u30C8\u53CA\u3073\u6570\u5B57\u3001\u30A2\u30F3\u30C0\u30FC\u30B9\u30B3\u30A2\u304C\u4F7F\u7528\u51FA\u6765\u307E\u3059\u3002\n  \u30FB<target>\u306F\u4ED6\u306E\u30B3\u30DE\u30F3\u30C9\u3067\u6307\u5B9A\u51FA\u6765\u308B\u3082\u306E\u304C\u305D\u306E\u307E\u307E\u4F7F\u3048\u307E\u3059\u304C\u3001\u3059\u3067\u306B\u30C0\u30A6\u30F3\u30ED\u30FC\u30C9\u6E08\u307F\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059\u3002\n\n  Example:\nnarou alias --list\nnarou alias musyoku=n9669bk\nnarou alias harem=1\nnarou convert harem    # \u4ED6\u306E\u30B3\u30DE\u30F3\u30C9\u3067\u5225\u540D\u304C\u4F7F\u3048\u308B\u3088\u3046\u306B\u306A\u308B\nnarou alias harem=     # \u53F3\u8FBA\u306B\u4F55\u3082\u66F8\u304B\u306A\u3044\u3068\u305D\u306E\u5225\u540D\u3092\u89E3\u9664\u3067\u304D\u308B\n\n  Options:\n  EOS\n  @opt.on(\"-l\", \"--list\", \"\u73FE\u5728\u306E\u5272\u308A\u5F53\u3066\u4E00\u89A7\u3092\u8868\u793A\u3059\u308B\") {\n    output_aliases_list\n    exit 0\n  }\nend\n"

Instance Method Details

#execute(argv) ⇒ Object



42
43
44
45
46
47
48
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
# File 'lib/command/alias.rb', line 42

def execute(argv)
  super
  if argv.empty?
    puts @opt.help
    return
  end
  aliases = LocalSetting.get["alias"]
  argv.each_with_index do |arg, i|
    Helper.print_horizontal_rule if i > 0
    alias_name, target = arg.split("=", 2)
    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
  LocalSetting.get.save_settings
end

#oneline_helpObject



78
79
80
# File 'lib/command/alias.rb', line 78

def oneline_help
  "小説のIDに紐付けた別名を作成します"
end

#output_aliases_listObject



33
34
35
36
37
38
39
40
# File 'lib/command/alias.rb', line 33

def output_aliases_list
  aliases = LocalSetting.get["alias"]
  database = Database.instance
  aliases.each do |name, id|
    title = database[id]["title"] rescue "(すでに削除されています)"
    puts "<bold><green>#{name}</green></bold>=#{title}".termcolor
  end
end