ccp 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,15 +7,37 @@ class Ccp::Persistent::Tsv < Ccp::Persistent::Dir
7
7
 
8
8
  def load_tsv(path)
9
9
  hash = {}
10
+ prev = []
11
+
10
12
  path.readlines.each_with_index do |line, i|
11
13
  no = i+1
12
14
  key, val = line.split(/\t/,2)
13
- unless val
14
- $stderr.puts "#{self.class}#load_tsv: value not found. key='#{key}' (line: #{no})"
15
- next
15
+
16
+ if val
17
+ # flush previous line
18
+ if prev.size == 2
19
+ hash[prev[0]] = decode(prev[1])
20
+ prev = []
21
+ end
22
+
23
+ # push prev line
24
+ prev = [key, val]
25
+
26
+ else
27
+ # maybe sequencial lines for one value
28
+ if prev.size == 2
29
+ prev[1] = prev[1] + "\n" + key
30
+
31
+ else
32
+ $stderr.puts "#{self.class}#load_tsv: value not found. key='#{key}' (line: #{no})"
33
+ next
34
+ end
16
35
  end
17
- obj = decode(val)
18
- hash[key] = obj
36
+ end
37
+
38
+ # flush last line
39
+ if prev.size == 2
40
+ hash[prev[0]] = decode(prev[1])
19
41
  end
20
42
 
21
43
  return hash
@@ -33,7 +55,8 @@ class Ccp::Persistent::Tsv < Ccp::Persistent::Dir
33
55
  end
34
56
 
35
57
  keys.each do |key|
36
- f.puts "%s\t%s\n" % [key, encode(hash[key])]
58
+ value = encode(hash[key]).sub(/\n+\Z/m, '') # strip last LF for human-readability
59
+ f.puts "%s\t%s\n" % [key, value]
37
60
  end
38
61
  end
39
62
 
@@ -1,3 +1,3 @@
1
1
  module Ccp
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.7"
3
3
  end
@@ -16,4 +16,37 @@ describe Ccp::Persistent::Tsv do
16
16
  kvs.tsv_path_for(:states).should == Pathname(root) + "states.json.tsv"
17
17
  end
18
18
  end
19
+
20
+ context "(with json)" do
21
+ let(:kvs) { Ccp::Persistent::Tsv.new(root, :json)}
22
+
23
+ it "should save and load hash" do
24
+ kvs["a"] = {"int"=>1, "str"=>"x"}
25
+ kvs["a"].should == {"int"=>1, "str"=>"x"}
26
+ end
27
+
28
+ it "should save and load complex-valued hash" do
29
+ kvs["a"] = {"int"=>[1,2], "str"=>["x","y"]}
30
+ kvs["a"].should == {"int"=>[1,2], "str"=>["x","y"]}
31
+ end
32
+ end
33
+
34
+ context "(with yaml)" do
35
+ let(:kvs) { Ccp::Persistent::Tsv.new(root, :yaml) }
36
+
37
+ it "should save and load hash" do
38
+ kvs["a"] = {"int"=>1, "str"=>"x"}
39
+ kvs["a"].should == {"int"=>1, "str"=>"x"}
40
+ end
41
+
42
+ it "should save and load complex-valued hash" do
43
+ kvs["a"] = {"int"=>[1,2], "str"=>["x","y"]}
44
+ kvs["a"].should == {"int"=>[1,2], "str"=>["x","y"]}
45
+ end
46
+
47
+ it "should save and load tab-contained hash" do
48
+ kvs["a"] = {"str"=>"a\tb"}
49
+ kvs["a"].should == {"str"=>"a\tb"}
50
+ end
51
+ end
19
52
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ccp
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 6
10
- version: 0.2.6
9
+ - 7
10
+ version: 0.2.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - maiha
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-05-15 00:00:00 +09:00
18
+ date: 2012-05-22 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency