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 +4 -4
- data/README.md +6 -0
- data/lib/kor/input/yaml.rb +47 -24
- data/lib/kor/input/yaml/version.rb +1 -1
- data/lib/kor/input/yaml_test.rb +38 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe43c8a5a7e5ac8c61d32f8f627d05df1fcfb715
|
4
|
+
data.tar.gz: 8486005be3f33555dced1c0b7fd2d70ee5e8a54d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b5f9656b7acd1634183c0698eaa22860911985921589a5e5db7f7d996888423e9ceb9d7f5e0fdee5ba7f9b65e05935b4280c1f2d86801cee84d88f2622f08d7
|
7
|
+
data.tar.gz: d587c0c7c60f52b22791f259a9df5ba47536fe32c2e9e8067e37eb646174889617b372f2a159936ee981229cef1a58d7b52bbe0c39da39db787705d6fe2479ca
|
data/README.md
CHANGED
data/lib/kor/input/yaml.rb
CHANGED
@@ -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", "
|
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
|
-
|
36
|
-
|
37
|
-
|
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.
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
57
|
-
|
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
|
data/lib/kor/input/yaml_test.rb
CHANGED
@@ -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.
|
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-
|
11
|
+
date: 2015-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kor
|