Class: Command::Csv
- Inherits:
-
CommandBase
- Object
- CommandBase
- Command::Csv
- Defined in:
- lib/command/csv.rb
Overview
ライブラリのCSVコマンドと混同を避けるために小文字混じり
Instance Attribute Summary
Attributes inherited from CommandBase
Class Method Summary collapse
Instance Method Summary collapse
- #execute(argv) ⇒ Object
-
#generate ⇒ Object
小説の情報をCSV形式の文字列で取得.
-
#import(data = nil) ⇒ Object
CSV形式のファイルからインポートする header行にurlという項目が必要 data にはインポートしたいIOオブジェクトかCSV形式の文字列を指定。 nil なら –import オプションで指定されたファイルから入力.
-
#initialize ⇒ Csv
constructor
A new instance of Csv.
-
#output(stream = nil) ⇒ Object
CSV形式で出力する stream には出力先のIOオブジェクトを指定。nilだったら$stdoutに出力.
Methods inherited from CommandBase
#disable_logging, #display_help!, execute!, #execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids
Constructor Details
#initialize ⇒ Csv
Returns a new instance of Csv.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/command/csv.rb', line 17 def initialize super("[optioins]") @opt.separator "\n \u30FB\u73FE\u5728\u7BA1\u7406\u3057\u3066\u3044\u308B\u5C0F\u8AAC\u306E\u60C5\u5831\u3092CSV\u5F62\u5F0F\u3067\u51FA\u529B\u3057\u305F\u308A\u3001\u9006\u306B\u30A4\u30F3\u30DD\u30FC\u30C8\u304C\u51FA\u6765\u307E\u3059\n \u30FB\u30A4\u30F3\u30DD\u30FC\u30C8\u3059\u308BCSV\u30D5\u30A1\u30A4\u30EB\u306B\u306F\u6700\u4F4E\u9650 url \u3068\u3044\u3046\u30D8\u30C3\u30C0\u30FC\u304C\u5FC5\u8981\u3067\u3059\n\n Examples:\nnarou csv # CSV\u5F62\u5F0F\u3067\u305D\u306E\u307E\u307E\u8868\u793A\nnarou csv -o novels.csv # novels.csv \u3068\u3044\u3046\u30D5\u30A1\u30A4\u30EB\u540D\u3067\u4FDD\u5B58\nnarou csv -i novels.csv # \u30D5\u30A1\u30A4\u30EB\u304B\u3089\u5C0F\u8AAC\u3092\u30A4\u30F3\u30DD\u30FC\u30C8\n\n Options:\n EOS\n @opt.on(\"-o\", \"--output FILE\", \"\u6307\u5B9A\u3057\u305F\u30D5\u30A1\u30A4\u30EB\u540D\u3067\u4FDD\u5B58\") { |filename|\n @options[\"filename\"] = filename\n @mode = :output\n }\n @opt.on(\"-i\", \"--import FILE\", \"\u6307\u5B9A\u3057\u305F\u30D5\u30A1\u30A4\u30EB\u304B\u3089\u30A4\u30F3\u30DD\u30FC\u30C8\") { |filename|\n @options[\"filename\"] = filename\n @mode = :import\n }\nend\n" |
Class Method Details
.oneline_help ⇒ Object
13 14 15 |
# File 'lib/command/csv.rb', line 13 def self.oneline_help "小説リストをCSV形式で出力したりインポートしたりします" end |
Instance Method Details
#execute(argv) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/command/csv.rb', line 41 def execute(argv) @mode = :output super self.__send__ @mode end |
#generate ⇒ Object
小説の情報をCSV形式の文字列で取得
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/command/csv.rb', line 61 def generate database_values = Database.instance.get_object.values CSV.generate do |csv| csv << %w(id title author sitename url novel_type tags frozen last_update general_lastup) database_values.each do |data| = data["tags"] || [] csv << [ data["id"], data["title"], data["author"], data["sitename"], data["toc_url"], data["novel_type"] == 2 ? "短編" : "連載", .join(" "), Narou.novel_frozen?(data["id"]), data["last_update"].to_i, data["general_lastup"].to_i ] end end end |
#import(data = nil) ⇒ Object
CSV形式のファイルからインポートするheader行にurlという項目が必要data にはインポートしたいIOオブジェクトかCSV形式の文字列を指定。nil なら –import オプションで指定されたファイルから入力
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/command/csv.rb', line 89 def import(data = nil) if data source = data elsif ["filename"] source = File.read(["filename"]) else raise ArgumentError, "need a CSV data" end csv = CSV.new(source, headers: true, converters: :numeric, header_converters: :symbol) table = csv.read table[:url].each do |url| next unless url Download.execute!(url) Helper.print_horizontal_rule end rescue CSV::MalformedCSVError => e puts "不正なCSVデータです(#{e.message})" exit Narou::EXIT_ERROR_CODE end |
#output(stream = nil) ⇒ Object
CSV形式で出力するstream には出力先のIOオブジェクトを指定。nilだったら$stdoutに出力
52 53 54 55 56 |
# File 'lib/command/csv.rb', line 52 def output(stream = nil) stream ||= ["filename"] ? File.open(["filename"], "w:UTF-8") : $stdout stream.puts(generate) stream.close if ["filename"] end |