kor-input-yaml 0.0.1 → 0.0.2

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: c5dd331e9dda4ba71e23393dc1bf05c0b7ce7d43
4
- data.tar.gz: 33bf11a35ab58d6bfc16e3679dcf2baf6f36a897
3
+ metadata.gz: fe43c8a5a7e5ac8c61d32f8f627d05df1fcfb715
4
+ data.tar.gz: 8486005be3f33555dced1c0b7fd2d70ee5e8a54d
5
5
  SHA512:
6
- metadata.gz: b8b031fa5158e3d901d0773b0751fae89c9929aa864656fd6b7d5b3312b1fccc92f60eb0e394a1f0cfe551abb4fb3c0211dec61f91ed1d36f6eeb47ccf87e054
7
- data.tar.gz: b0fc4540c9c3dd2c0d2cc95b5ec2aa37e3ee06b1e4051b29bf1aa4873bbc8d93195b14ccac72f1a11c97e8a6c965ef4edb7c236bfef28d27754a3a969139c0b6
6
+ metadata.gz: 6b5f9656b7acd1634183c0698eaa22860911985921589a5e5db7f7d996888423e9ceb9d7f5e0fdee5ba7f9b65e05935b4280c1f2d86801cee84d88f2622f08d7
7
+ data.tar.gz: d587c0c7c60f52b22791f259a9df5ba47536fe32c2e9e8067e37eb646174889617b372f2a159936ee981229cef1a58d7b52bbe0c39da39db787705d6fe2479ca
data/README.md CHANGED
@@ -26,6 +26,12 @@ $ kor yaml markdown < table.yml
26
26
  | --- | --- | --- |
27
27
  | 100 | 200 | |
28
28
  | | 500 | 600 |
29
+
30
+ $ kor yaml --key=bar,foo markdown < table.yml
31
+ | bar | foo |
32
+ | --- | --- |
33
+ | 200 | 100 |
34
+ | 500 | |
29
35
  ```
30
36
 
31
37
  ## Installation
@@ -3,12 +3,17 @@ require 'yaml'
3
3
  module Kor
4
4
  module Input
5
5
  class Yaml
6
+ DEFAULT_GUESS_TIME = 5
7
+ START = "---"
8
+
6
9
  def initialize(io)
7
10
  @io = io
8
11
  @keys = []
9
12
  @yamls = []
10
13
  @prekeys = nil
11
14
  @count = 0
15
+ @stock = []
16
+ @guess_time = DEFAULT_GUESS_TIME
12
17
  @fiber = Fiber.new do
13
18
  @yamls.each do |yaml|
14
19
  Fiber.yield @keys.map{ |k| yaml[k] }
@@ -19,46 +24,48 @@ module Kor
19
24
  end
20
25
 
21
26
  def parse(opt)
22
- opt.on("--key=KEY", "preset keys (e.g. foo,bar,baz)") do |arg|
27
+ opt.on("--key=KEY", "select keys (e.g. foo,bar,baz)") do |arg|
23
28
  @prekeys = arg
24
29
  end
30
+ opt.on("--guess-time=NUM", "guess number of times (default #{DEFAULT_GUESS_TIME})") do |arg|
31
+ @guess_time = arg.to_i
32
+ end
25
33
  end
26
34
 
27
35
  def head
28
36
  if @prekeys
29
- line = @io.gets
30
- if line.index(START) != 0
31
- @io.seek(line.length, IO::SEEK_CUR)
32
- end
33
37
  @keys = @prekeys.split(",")
34
38
  else
35
- @yamls = YAML.load_stream(@io.read)
36
- @keys = @yamls.map do |yaml|
37
- yaml.keys
39
+ while yaml = read_yaml
40
+ @yamls << yaml
41
+ if 0 < @guess_time && @guess_time <= @yamls.length
42
+ break
43
+ end
38
44
  end
39
- @keys.flatten!.uniq!
40
- @keys
45
+ @keys = @yamls.map { |yaml| yaml.keys }
46
+ @keys.flatten!
47
+ @keys.uniq!
41
48
  end
49
+ @keys
42
50
  end
43
51
 
44
- START = "---"
45
- EOF = "\n"
46
-
47
52
  def gets
48
53
  if @prekeys
49
- stock = ""
50
- while line = @io.gets
51
- stock << line
52
- if line.index(START) == 0
53
- break
54
- end
54
+ if yaml = read_yaml
55
+ @keys.map{ |key| yaml[key] }
56
+ else
57
+ nil
58
+ end
59
+ elsif 0 < @guess_time
60
+ if @count < @guess_time
61
+ @count += 1
62
+ return resume
55
63
  end
56
- if stock == ""
57
- return nil
64
+ if yaml = read_yaml
65
+ @keys.map { |k| yaml[k] }
66
+ else
67
+ nil
58
68
  end
59
- yaml = YAML.load(stock)
60
- @count += 1
61
- @keys.map{ |key| yaml[key] }
62
69
  else
63
70
  resume
64
71
  end
@@ -66,6 +73,22 @@ module Kor
66
73
 
67
74
  private
68
75
 
76
+ def read_yaml
77
+ while line = @io.gets
78
+ @stock << line
79
+ if line.index(START) == 0
80
+ if 1 < @stock.length
81
+ break
82
+ end
83
+ end
84
+ end
85
+ if @stock.empty?
86
+ return nil
87
+ end
88
+ last_index = line ? -2 : -1
89
+ YAML.load(@stock.slice!(0..last_index).join)
90
+ end
91
+
69
92
  def resume
70
93
  @fiber.resume
71
94
  rescue FiberError
@@ -1,7 +1,7 @@
1
1
  module Kor
2
2
  module Input
3
3
  class Yaml
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
6
6
  end
7
7
  end
@@ -63,6 +63,25 @@ YAML
63
63
  end
64
64
  end
65
65
 
66
+ def test_gets_with_none_guess(t)
67
+ yaml = ready
68
+ opt = OptionParser.new
69
+ yaml.parse(opt)
70
+ opt.parse ["--guess-time=0"]
71
+ yaml.head
72
+
73
+ expects = [
74
+ [100, 200, nil],
75
+ [nil, 500, 600],
76
+ nil, nil, nil, nil, nil
77
+ ].each do |expect|
78
+ actual = yaml.gets
79
+ if actual != expect
80
+ t.error("expect #{expect} got #{actual}")
81
+ end
82
+ end
83
+ end
84
+
66
85
  def test_gets_with_prekeys(t)
67
86
  yaml = ready
68
87
  opt = OptionParser.new
@@ -82,6 +101,25 @@ YAML
82
101
  end
83
102
  end
84
103
 
104
+ def test_e2e(t)
105
+ actual = `ruby -ryaml -e '3.times { [{"a"=>1},{"b"=>2},{"c"=>3}].each{|h| puts YAML.dump(h)} }' | bundle ex kor yaml --guess-time=3 csv`
106
+ expect = <<-CSV
107
+ a,b,c
108
+ 1,,
109
+ ,2,
110
+ ,,3
111
+ 1,,
112
+ ,2,
113
+ ,,3
114
+ 1,,
115
+ ,2,
116
+ ,,3
117
+ CSV
118
+ if actual != expect
119
+ t.error("expect #{expect.inspect} got #{actual.inspect}")
120
+ end
121
+ end
122
+
85
123
  private
86
124
 
87
125
  def go
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kor-input-yaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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-10 00:00:00.000000000 Z
11
+ date: 2015-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kor