ccp 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -48,7 +48,7 @@ class Ccp::Persistent::Base
48
48
  end
49
49
 
50
50
  def read!
51
- keys.inject({}) {|h,k| h[k] = v; h}
51
+ keys.inject({}) {|h,k| h[k] = self[k]; h}
52
52
  end
53
53
 
54
54
  def keys
@@ -29,7 +29,7 @@ class Ccp::Persistent::Dir < Ccp::Persistent::Base
29
29
  end
30
30
 
31
31
  def keys
32
- Dir["#{path}/*.#{ext}"].map{|i| File.basename(i, ".*")}.sort
32
+ Dir["#{path!}/*.#{ext}"].map{|i| File.basename(i, ".*")}.sort
33
33
  end
34
34
 
35
35
  def truncate
@@ -41,6 +41,14 @@ class Ccp::Persistent::Dir < Ccp::Persistent::Base
41
41
  end
42
42
 
43
43
  private
44
+ def path!
45
+ if path.exist?
46
+ path
47
+ else
48
+ raise Ccp::Persistent::NotFound, path.to_s
49
+ end
50
+ end
51
+
44
52
  def path_for(key, mkdir = true)
45
53
  path.mkpath if mkdir
46
54
  path + "#{key}.#{ext}"
@@ -36,7 +36,7 @@ class Ccp::Persistent::File < Ccp::Persistent::Base
36
36
  end
37
37
 
38
38
  def keys
39
- read.keys.sort
39
+ read!.keys.sort
40
40
  end
41
41
 
42
42
  def truncate
@@ -1,3 +1,3 @@
1
1
  module Ccp
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
@@ -76,6 +76,36 @@ describe Ccp::Persistent::Dir do
76
76
  end
77
77
  end
78
78
 
79
+ describe "#read" do
80
+ it "should fetch all data if exists" do
81
+ (root + "foo.json").open("w+"){|f| f.print "1"}
82
+ (root + "bar.json").open("w+"){|f| f.print "2"}
83
+ kvs = Ccp::Persistent::Dir.new(root, :json)
84
+ kvs.read.should == {"foo"=>1, "bar"=>2}
85
+ end
86
+
87
+ it "should return {} if the path doesn't exist" do
88
+ kvs = Ccp::Persistent::Dir.new(root + "no-such-path", :json)
89
+ kvs.read.should == {}
90
+ end
91
+ end
92
+
93
+ describe "#read!" do
94
+ it "should fetch all data if exists" do
95
+ (root + "foo.json").open("w+"){|f| f.print "1"}
96
+ (root + "bar.json").open("w+"){|f| f.print "2"}
97
+ kvs = Ccp::Persistent::Dir.new(root, :json)
98
+ kvs.read!.should == {"foo"=>1, "bar"=>2}
99
+ end
100
+
101
+ it "should raise NotFound if the path doesn't exist" do
102
+ kvs = Ccp::Persistent::Dir.new(root + "no-such-path", :json)
103
+ lambda {
104
+ kvs.read!
105
+ }.should raise_error(Ccp::Persistent::NotFound)
106
+ end
107
+ end
108
+
79
109
  describe "#keys" do
80
110
  it "should return a sorted array of key names filtered by given ext" do
81
111
  ["1.json", "2.yaml", "3.json"].each do |i|
@@ -85,6 +115,13 @@ describe Ccp::Persistent::Dir do
85
115
  kvs = Ccp::Persistent::Dir.new(root, :json)
86
116
  kvs.keys.should == ["1","3"]
87
117
  end
118
+
119
+ it "should raise NotFound if the path doesn't exist" do
120
+ kvs = Ccp::Persistent::Dir.new(root + "no-such-path", :json)
121
+ lambda {
122
+ kvs.keys
123
+ }.should raise_error(Ccp::Persistent::NotFound)
124
+ end
88
125
  end
89
126
 
90
127
  describe "#truncate" do
@@ -103,12 +103,47 @@ describe Ccp::Persistent::File do
103
103
  end
104
104
  end
105
105
 
106
+ describe "#read" do
107
+ it "should fetch all data if exists" do
108
+ db.open("w+"){|f| f.print(JSON.dump({"foo" => 1, "bar" => 2}))}
109
+ kvs = Ccp::Persistent::File.new(db, :json)
110
+ kvs.read.should == {"foo" =>1, "bar" =>2}
111
+ end
112
+
113
+ it "should raise NotFound if the path doesn't exist" do
114
+ kvs = Ccp::Persistent::File.new(db + "no-such-file", :json)
115
+ kvs.read.should == {}
116
+ end
117
+ end
118
+
119
+ describe "#read!" do
120
+ it "should fetch all data if exists" do
121
+ db.open("w+"){|f| f.print(JSON.dump({"foo" => 1, "bar" => 2}))}
122
+ kvs = Ccp::Persistent::File.new(db, :json)
123
+ kvs.read!.should == {"foo" =>1, "bar" =>2}
124
+ end
125
+
126
+ it "should raise NotFound if the path doesn't exist" do
127
+ kvs = Ccp::Persistent::File.new(db + "no-such-file", :json)
128
+ lambda {
129
+ kvs.read!
130
+ }.should raise_error(Ccp::Persistent::NotFound)
131
+ end
132
+ end
133
+
106
134
  describe "#keys" do
107
135
  it "should return a sorted array of key names filtered by given ext" do
108
136
  db.open("w+"){|f| f.print(JSON.dump({"foo" => 1, "bar" => "xxx"}))}
109
137
  kvs = Ccp::Persistent::File.new(db, :json)
110
138
  kvs.keys.should == ["bar", "foo"]
111
139
  end
140
+
141
+ it "should raise NotFound if the path doesn't exist" do
142
+ kvs = Ccp::Persistent::File.new(db + "no-such-file", :json)
143
+ lambda {
144
+ kvs.keys
145
+ }.should raise_error(Ccp::Persistent::NotFound)
146
+ end
112
147
  end
113
148
 
114
149
  describe "#truncate" do
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: 17
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 3
10
- version: 0.2.3
9
+ - 4
10
+ version: 0.2.4
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-10 00:00:00 +09:00
18
+ date: 2012-05-11 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency