kor 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd2fdf2bb00f0d166762cc23c182273f237606f1
4
- data.tar.gz: 6700bcd1a6abdeb9f6314fb995325bfc50eac6a9
3
+ metadata.gz: f945e02d461ae630c93756a59d37a1adbc2c3783
4
+ data.tar.gz: 6eb078d973ea6ea8e185461501518639951949f5
5
5
  SHA512:
6
- metadata.gz: 6a6e55a09cede35a09fece484ad381715f7d64ddd758437e4308e4393b05c9da1866cb08c9af900e243dffc04da0598fd2f99c317d41b04935457397d8df1b20
7
- data.tar.gz: a49f5751f123785943068bfdea8a8cc0668a311d3a1f3ee8d523e190dad2a7acf14c6a712d4b791a7d020574c0a4da7544bb58ea3ac0cb6ffe73f0274f01bf17
6
+ metadata.gz: bccf7c8361ee7937e60d8740b98b906e2ae10444298fb386a231f12a3280614ba99b166923ce92c21bdcfc5bc6d1c9d81420a1c75cd2fc30048a3f152fa7cd1c
7
+ data.tar.gz: fffda929139840318ea989547d663675124bd5022c42cb14cea70be5a8f42f5c76a599702167f1b2e7c51b717b28f8faccc978213c05e98bbebb5f915d66c425
data/README.md CHANGED
@@ -42,9 +42,9 @@ Input from stdin and output to stdout.
42
42
 
43
43
  ```
44
44
  $ kor
45
- kor [input-plugin] [input-option] [output-plugin] [output-option]
45
+ kor [option] [input-plugin] [input-option] [output-plugin] [output-option]
46
46
  example:
47
- $ kor csv markdown
47
+ $ kor --sync csv markdown --strip
48
48
  ```
49
49
 
50
50
  ### input-plugin
data/lib/kor.rb CHANGED
@@ -1,2 +1,7 @@
1
- require "kor/cli"
2
- require "kor/version"
1
+ module Kor
2
+ class NotKeyError < StandardError
3
+ end
4
+
5
+ require "kor/cli"
6
+ require "kor/version"
7
+ end
@@ -1,52 +1,55 @@
1
1
  require 'optparse'
2
- require 'thread'
3
2
  require "kor/input/base"
4
3
  require "kor/output/base"
5
4
 
6
5
  module Kor
7
6
  class Cli
7
+ USAGE = <<-USAGE
8
+ kor [option] [input-plugin] [input-option] [output-plugin] [output-option]
9
+ example:
10
+ $ kor --sync csv markdown --strip
11
+ USAGE
12
+
8
13
  def initialize(argv = ARGV, input_io = $stdin, output_io = $stdout)
9
14
  @argv = argv
10
15
  @input_io = input_io
11
16
  @output_io = output_io
17
+ end
12
18
 
13
- @input_plugin = argv.shift
14
- @input_args = []
15
- while argv.first && argv.first[0] == "-"
16
- @input_args << argv.shift
19
+ def run
20
+ cli_args = shift
21
+ cli_opt = OptionParser.new
22
+ cli_opt.on("--sync", "I/O sync mode") do |arg|
23
+ @input_io.sync = true
24
+ @output_io.sync = true
17
25
  end
26
+ cli_opt.parse(cli_args)
18
27
 
19
- @output_plugin = argv.shift
20
- @output_args = []
21
- while argv.first && argv.first[0] == "-"
22
- @output_args << argv.shift
28
+ input_plugin = @argv.shift
29
+ input_args = shift
30
+ unless input_plugin
31
+ @output_io.puts Cli::USAGE
32
+ exit 0
23
33
  end
24
- end
34
+ require_plugin "kor/input/#{input_plugin}"
35
+ in_class = Kor::Input.const_get(input_plugin.capitalize)
36
+ in_obj = in_class.new(@input_io)
37
+ in_opt = OptionParser.new
38
+ in_obj.parse(in_opt)
39
+ in_opt.parse(input_args)
25
40
 
26
- def run
27
- unless @input_plugin && @output_plugin
28
- @output_io.puts <<-USAGE
29
- kor [input-plugin] [input-option] [output-plugin] [output-option]
30
- example:
31
- $ kor csv markdown
32
- USAGE
41
+ output_plugin = @argv.shift
42
+ output_args = shift
43
+ unless output_plugin
44
+ @output_io.puts Cli::USAGE
33
45
  exit 0
34
46
  end
35
-
36
- require "kor/input/#{@input_plugin}"
37
- require "kor/output/#{@output_plugin}"
38
-
39
- in_class = Kor::Input.const_get(@input_plugin.capitalize)
40
- out_class = Kor::Output.const_get(@output_plugin.capitalize)
41
- in_obj = in_class.new(@input_io)
47
+ require_plugin "kor/output/#{output_plugin}"
48
+ out_class = Kor::Output.const_get(output_plugin.capitalize)
42
49
  out_obj = out_class.new(@output_io)
43
-
44
- in_opt = OptionParser.new
45
50
  out_opt = OptionParser.new
46
- in_obj.parse(in_opt)
47
51
  out_obj.parse(out_opt)
48
- in_opt.parse(@input_args)
49
- out_opt.parse(@output_args)
52
+ out_opt.parse(output_args)
50
53
 
51
54
  out_obj.head(in_obj.head)
52
55
  while body = in_obj.gets
@@ -54,5 +57,22 @@ USAGE
54
57
  end
55
58
  out_obj.finish
56
59
  end
60
+
61
+ private
62
+
63
+ def shift
64
+ args = []
65
+ while @argv.first && @argv.first[0] == "-"
66
+ args << @argv.shift
67
+ end
68
+ args
69
+ end
70
+
71
+ def require_plugin(name)
72
+ require name
73
+ rescue LoadError
74
+ warn "LoadError: `#{name}' plugin not found"
75
+ exit 1
76
+ end
57
77
  end
58
78
  end
@@ -1,19 +1,39 @@
1
1
  module Kor
2
2
  module Input
3
3
  class Markdown < Base
4
+ def parse(opt)
5
+ opt.on("--key=KEY", "filter key set (e.g. foo,bar,baz)") do |arg|
6
+ @filter_key = arg
7
+ end
8
+ end
9
+
4
10
  def head
5
11
  line = io.gets.strip
6
12
  first_index = line[0] == '|' ? 1 : 0
7
- h = line.split('|')[first_index..-1].map(&:strip)
13
+ keys = line.split('|')[first_index..-1].map(&:strip)
8
14
  # skip separate line
9
15
  io.gets
10
- h
16
+ if @filter_key
17
+ @filter_key = @filter_key.split(",").map do |key|
18
+ if index = keys.index(key)
19
+ index
20
+ else
21
+ raise NotKeyError, "`#{key}' is a not key of this table"
22
+ end
23
+ end
24
+ keys = @filter_key.map{ |index| keys[index] }
25
+ end
26
+ keys
11
27
  end
12
28
 
13
29
  def gets
14
30
  if line = io.gets
15
31
  first_index = line[0] == '|' ? 1 : 0
16
- line.strip.split('|')[first_index..-1].map(&:strip)
32
+ body = line.strip.split('|')[first_index..-1].map(&:strip)
33
+ if @filter_key
34
+ return @filter_key.map{ |index| body[index] }
35
+ end
36
+ body
17
37
  else
18
38
  nil
19
39
  end
@@ -13,7 +13,7 @@ MARKDOWN
13
13
  foo | bar | baz |
14
14
  --- | --- | ---
15
15
  | 1 | 2 | 3
16
- a | b | c |
16
+ a | b | c |
17
17
  MARKDOWN
18
18
  exit m.run
19
19
  end
@@ -34,6 +34,38 @@ MARKDOWN
34
34
  end
35
35
  end
36
36
 
37
+ def test_with_key(t)
38
+ @io.rewind
39
+ md = Kor::Input::Markdown.new(@io)
40
+ opt = OptionParser.new
41
+ md.parse(opt)
42
+
43
+ opt.parse ["--key=bar,nothing"]
44
+ _, err = go { md.head }
45
+ unless Kor::NotKeyError === err
46
+ t.error("expect raise an error Kor::NotKeyError got #{err.class}:#{err}")
47
+ end
48
+
49
+ @io.rewind
50
+ opt.parse ["--key=bar,foo"]
51
+ actual = md.head
52
+ expect = %w(bar foo)
53
+ if actual != expect
54
+ t.error("expect #{expect} got #{actual}")
55
+ end
56
+
57
+ expects = [
58
+ %w(2 1),
59
+ %w(b a),
60
+ nil, nil, nil, nil, nil
61
+ ].each do |expect|
62
+ actual = md.gets
63
+ if actual != expect
64
+ t.error("expect #{expect} got #{actual}")
65
+ end
66
+ end
67
+ end
68
+
37
69
  def test_gets(t)
38
70
  @io.rewind
39
71
  md = Kor::Input::Markdown.new(@io)
@@ -73,4 +105,10 @@ MARKDOWN
73
105
  end
74
106
  end
75
107
  end
108
+
109
+ def go
110
+ [yield, nil]
111
+ rescue Exception => err
112
+ [nil, err]
113
+ end
76
114
  end
@@ -6,17 +6,32 @@ module Kor
6
6
  opt.on("--strip", "strip space") do |arg|
7
7
  @space = ""
8
8
  end
9
+ opt.on("--key=KEY", "select output keys (like foo,bar,baz)") do |arg|
10
+ @select_key = arg
11
+ end
9
12
  end
10
13
 
11
14
  def head(keys)
15
+ if @select_key
16
+ select_keys = @select_key.split(",")
17
+ @select_key = select_keys.map do |key|
18
+ keys.index(key) or raise NotKeyError, "`#{key}' is not a key of this table"
19
+ end
20
+ keys = select_keys
21
+ end
12
22
  s = @space || " "
13
23
  io.puts "|#{s}#{keys.join("#{s}|#{s}")}#{s}|"
14
24
  io.puts "|#{s}#{(["---"] * keys.length).join("#{s}|#{s}")}#{s}|"
15
25
  end
16
26
 
17
27
  def puts(values)
18
- s = @space || " "
19
- io.puts "|#{s}#{values.join("#{s}|#{s}")}#{s}|"
28
+ if @select_key
29
+ s = @space || " "
30
+ io.puts "|#{s}#{@select_key.map{ |index| values[index] }.join("#{s}|#{s}")}#{s}|"
31
+ else
32
+ s = @space || " "
33
+ io.puts "|#{s}#{values.join("#{s}|#{s}")}#{s}|"
34
+ end
20
35
  end
21
36
  end
22
37
  end
@@ -4,19 +4,67 @@ require 'kor/output/markdown'
4
4
  module KorOutputMarkdownTest
5
5
  def test_head(t)
6
6
  io = StringIO.new
7
- csv = Kor::Output::Markdown.new(io)
8
- csv.head(%w(foo bar baz))
7
+ md = Kor::Output::Markdown.new(io)
8
+ md.head(%w(foo bar baz))
9
9
  if io.string != "| foo | bar | baz |\n| --- | --- | --- |\n"
10
10
  t.error("expect output '| foo | bar | baz |\\n| --- | --- | --- |\\n' got #{io.string.inspect}")
11
11
  end
12
12
  end
13
13
 
14
+ def test_head_with_key(t)
15
+ io = StringIO.new
16
+ md = Kor::Output::Markdown.new(io)
17
+ opt = OptionParser.new
18
+ md.parse(opt)
19
+ opt.parse ["--key=foo,nothing"]
20
+ _, err = go { md.head(["foo", "bar", "baz"]) }
21
+ unless Kor::NotKeyError === err
22
+ t.error("expect raise Kor::Output::Markdown::NotKeyError got #{err.class}:#{err}")
23
+ end
24
+
25
+ opt.parse ["--key=bar,foo"]
26
+ _, err = go { md.head(["foo", "bar", "baz"]) }
27
+ t.error("expect not raise got #{err.class}:#{err}") if err
28
+ if io.string != "| bar | foo |\n| --- | --- |\n"
29
+ t.error("expect bar,foo header got #{head}")
30
+ end
31
+ end
32
+
14
33
  def test_puts(t)
15
34
  io = StringIO.new
16
- csv = Kor::Output::Markdown.new(io)
17
- csv.puts(%w(aaa bbb ccc))
35
+ md = Kor::Output::Markdown.new(io)
36
+ md.puts(%w(aaa bbb ccc))
18
37
  if io.string != "| aaa | bbb | ccc |\n"
19
38
  t.error("expect output '| aaa | bbb | ccc |\\n' got #{io.string.inspect}")
20
39
  end
21
40
  end
41
+
42
+ def test_puts_with_key(t)
43
+ io = StringIO.new
44
+ md = Kor::Output::Markdown.new(io)
45
+ opt = OptionParser.new
46
+ md.parse(opt)
47
+ opt.parse ["--key=bar,foo"]
48
+ md.head %w(foo bar baz)
49
+ md.puts %w(aaa bbb ccc)
50
+ md.puts %w(100 200 300)
51
+ io.rewind
52
+ io.gets
53
+ io.gets
54
+ actual = io.gets
55
+ if actual != "| bbb | aaa |\n"
56
+ t.error("expect output '| bbb | aaa |\\n' got #{actual}")
57
+ end
58
+
59
+ actual = io.gets
60
+ if actual != "| 200 | 100 |\n"
61
+ t.error("expect output '| 200 | 100 |\\n' got #{actual}")
62
+ end
63
+ end
64
+
65
+ def go
66
+ [yield, nil]
67
+ rescue Exception => err
68
+ [nil, err]
69
+ end
22
70
  end
@@ -1,3 +1,3 @@
1
1
  module Kor
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - ksss
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-07 00:00:00.000000000 Z
11
+ date: 2015-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler