kor-input-ltsv 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/kor-input-ltsv.gemspec +1 -0
- data/lib/kor/input/ltsv.rb +34 -15
- data/lib/kor/input/ltsv/version.rb +1 -1
- data/lib/kor/input/ltsv_test.rb +26 -4
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6212739aa66563f7e318f2cef919c8f83ac58a2
|
4
|
+
data.tar.gz: 45c50fc227d75b80483978e13d624d2feab185ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9faba9eb923cbd5ab08252457aa36e0464ed31be67a9a362481279453caa6f05dd15e6c9695ea145aae115bd03f10c4bc4e0b9bc409962edef21c8581399e1c
|
7
|
+
data.tar.gz: 36ea2d4db59ee0339616c03cb796523727a6f41af7c62005ae539ca611f6bbb2f11569f3bb9b7173967cd04548b872519bbcafb4b44160bffe5ee3a7b0b2afad
|
data/kor-input-ltsv.gemspec
CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
20
|
spec.add_runtime_dependency "kor"
|
21
|
+
spec.add_runtime_dependency "ltsv"
|
21
22
|
spec.add_development_dependency "bundler", "~> 1.10"
|
22
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
24
|
spec.add_development_dependency "rgot"
|
data/lib/kor/input/ltsv.rb
CHANGED
@@ -1,22 +1,19 @@
|
|
1
1
|
require 'kor'
|
2
|
+
require 'ltsv'
|
2
3
|
|
3
4
|
module Kor
|
4
5
|
module Input
|
5
6
|
class Ltsv
|
6
|
-
|
7
|
-
DELIM = "\t".freeze
|
8
|
-
SPLIT = ":".freeze
|
9
|
-
|
10
|
-
def self.parse(line)
|
11
|
-
LTSV[line.split(DELIM).map{ |i| i.split(SPLIT, 2) }]
|
12
|
-
end
|
13
|
-
end
|
7
|
+
DEFAULT_GUESS_TIME = 5
|
14
8
|
|
15
9
|
def initialize(io)
|
16
10
|
@io = io
|
17
11
|
@keys = []
|
18
12
|
@prekeys = nil
|
19
13
|
@ltsvs = []
|
14
|
+
@guess = true
|
15
|
+
@guess_time = DEFAULT_GUESS_TIME
|
16
|
+
@count = 0
|
20
17
|
@fiber = Fiber.new do
|
21
18
|
@ltsvs.each do |ltsv|
|
22
19
|
Fiber.yield @keys.map{ |k| ltsv[k] }
|
@@ -30,6 +27,9 @@ module Kor
|
|
30
27
|
opt.on("--key=KEY", "define keys preset (e.g. foo,bar,baz) (default auto)") do |arg|
|
31
28
|
@prekeys = arg
|
32
29
|
end
|
30
|
+
opt.on("--guess-time=NUM", "load lines this time for guess. no guess if under 0 (default #{DEFAULT_GUESS_TIME})") do |arg|
|
31
|
+
@guess_time = arg.to_i
|
32
|
+
end
|
33
33
|
end
|
34
34
|
|
35
35
|
def head
|
@@ -38,7 +38,10 @@ module Kor
|
|
38
38
|
else
|
39
39
|
while line = @io.gets
|
40
40
|
line.strip!
|
41
|
-
@ltsvs <<
|
41
|
+
@ltsvs << parse_line(line)
|
42
|
+
if 0 < @guess_time && @guess_time <= @ltsvs.length
|
43
|
+
break
|
44
|
+
end
|
42
45
|
end
|
43
46
|
@keys = @ltsvs.map { |ltsv| ltsv.keys }
|
44
47
|
@keys.flatten!.uniq!
|
@@ -50,19 +53,35 @@ module Kor
|
|
50
53
|
if @prekeys
|
51
54
|
if line = @io.gets
|
52
55
|
line.strip!
|
53
|
-
ltsv =
|
56
|
+
ltsv = parse_line(line)
|
54
57
|
@keys.map { |k| ltsv[k] }
|
55
58
|
else
|
56
59
|
nil
|
57
60
|
end
|
58
|
-
|
59
|
-
|
60
|
-
@
|
61
|
-
|
62
|
-
nil
|
61
|
+
elsif 0 < @guess_time
|
62
|
+
if @count < @guess_time
|
63
|
+
@count += 1
|
64
|
+
return resume
|
63
65
|
end
|
66
|
+
if line = @io.gets
|
67
|
+
line.strip!
|
68
|
+
ltsv = parse_line(line)
|
69
|
+
@keys.map { |k| ltsv[k] }
|
70
|
+
end
|
71
|
+
else
|
72
|
+
resume
|
64
73
|
end
|
65
74
|
end
|
75
|
+
|
76
|
+
def parse_line(line)
|
77
|
+
LTSV.parse(line, symbolize_keys: false).first
|
78
|
+
end
|
79
|
+
|
80
|
+
def resume
|
81
|
+
@fiber.resume
|
82
|
+
rescue FiberError
|
83
|
+
nil
|
84
|
+
end
|
66
85
|
end
|
67
86
|
|
68
87
|
require "kor/input/ltsv/version"
|
data/lib/kor/input/ltsv_test.rb
CHANGED
@@ -5,6 +5,10 @@ module KorInputLtsvTest
|
|
5
5
|
ltsv = <<-LTSV
|
6
6
|
foo:100\tbar:200\tbaz:qux:300
|
7
7
|
\tbar:500\tfoo:600
|
8
|
+
foo:110\tbar:120\tbaz:130
|
9
|
+
foo:140\tbar:150\tbaz:160
|
10
|
+
foo:170\tbar:180\tbaz:190
|
11
|
+
foo:200\tbar:210\tbaz:220
|
8
12
|
LTSV
|
9
13
|
|
10
14
|
actual = `echo '#{ltsv.chomp}' | kor ltsv csv`
|
@@ -12,16 +16,34 @@ LTSV
|
|
12
16
|
foo,bar,baz
|
13
17
|
100,200,qux:300
|
14
18
|
600,500,
|
19
|
+
110,120,130
|
20
|
+
140,150,160
|
21
|
+
170,180,190
|
22
|
+
200,210,220
|
15
23
|
CSV
|
16
24
|
if actual != expect
|
17
25
|
t.error("expect #{expect} got #{actual}")
|
18
26
|
end
|
19
27
|
|
20
|
-
actual = `echo '#{ltsv.chomp}' | kor ltsv --
|
28
|
+
actual = `echo '#{ltsv.chomp}' | kor ltsv --guess-time=100 csv`
|
29
|
+
if actual != expect
|
30
|
+
t.error("expect #{expect} got #{actual}")
|
31
|
+
end
|
32
|
+
|
33
|
+
actual = `echo '#{ltsv.chomp}' | kor ltsv --guess-time=0 csv`
|
34
|
+
if actual != expect
|
35
|
+
t.error("expect #{expect} got #{actual}")
|
36
|
+
end
|
37
|
+
|
38
|
+
actual = `echo '#{ltsv.chomp}' | kor ltsv --key=bar,foo,non csv`
|
21
39
|
expect = <<-CSV
|
22
|
-
bar,foo
|
23
|
-
200,100
|
24
|
-
500,600
|
40
|
+
bar,foo,non
|
41
|
+
200,100,
|
42
|
+
500,600,
|
43
|
+
120,110,
|
44
|
+
150,140,
|
45
|
+
180,170,
|
46
|
+
210,200,
|
25
47
|
CSV
|
26
48
|
if actual != expect
|
27
49
|
t.error("expect #{expect} got #{actual}")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kor-input-ltsv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
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-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kor
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: ltsv
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|