ccp 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/ccp.gemspec +1 -1
  2. data/lib/ccp.rb +4 -2
  3. data/lib/ccp/invokers/base.rb +2 -2
  4. data/lib/ccp/persistent.rb +23 -0
  5. data/lib/ccp/persistent/base.rb +51 -0
  6. data/lib/ccp/persistent/dir.rb +48 -0
  7. data/lib/ccp/persistent/file.rb +59 -0
  8. data/lib/ccp/persistent/tsv.rb +76 -0
  9. data/lib/ccp/persistent/versioned.rb +90 -0
  10. data/lib/ccp/receivers.rb +4 -1
  11. data/lib/ccp/receivers/base.rb +6 -7
  12. data/lib/ccp/receivers/commentable.rb +1 -1
  13. data/lib/ccp/receivers/core.rb +14 -0
  14. data/lib/ccp/receivers/fixtures.rb +75 -0
  15. data/lib/ccp/receivers/profileable.rb +1 -1
  16. data/lib/ccp/receivers/settings.rb +21 -0
  17. data/lib/ccp/receivers/variables.rb +21 -0
  18. data/lib/ccp/serializers.rb +19 -0
  19. data/lib/ccp/serializers/core.rb +13 -0
  20. data/lib/ccp/serializers/json.rb +15 -0
  21. data/lib/ccp/serializers/yaml.rb +15 -0
  22. data/lib/ccp/utils.rb +6 -0
  23. data/lib/ccp/utils/colorize.rb +15 -0
  24. data/lib/ccp/version.rb +1 -1
  25. data/spec/{commands_base_spec.rb → commands/base_spec.rb} +0 -0
  26. data/spec/{commands_composite_spec.rb → commands/composite_spec.rb} +0 -0
  27. data/spec/{commands_core_spec.rb → commands/core_spec.rb} +0 -0
  28. data/spec/{commands_executable_spec.rb → commands/executable_spec.rb} +0 -0
  29. data/spec/{invokers_spec.rb → invokers/base_spec.rb} +0 -0
  30. data/spec/persistent/base_spec.rb +49 -0
  31. data/spec/persistent/dir_spec.rb +111 -0
  32. data/spec/persistent/file_spec.rb +124 -0
  33. data/spec/persistent/tsv_spec.rb +19 -0
  34. data/spec/persistent/versioned_spec.rb +110 -0
  35. data/spec/receivers/fixture_save_spec.rb +79 -0
  36. data/spec/serializers/core_spec.rb +19 -0
  37. data/spec/serializers/json_spec.rb +27 -0
  38. data/spec/serializers/spec.rb +39 -0
  39. data/spec/serializers/yaml_spec.rb +27 -0
  40. metadata +39 -18
  41. data/lib/ccp/colorize.rb +0 -13
  42. data/lib/ccp/data.rb +0 -37
  43. data/lib/ccp/receivers/save_fixture.rb +0 -45
  44. data/spec/data_spec.rb +0 -18
  45. data/spec/save_fixture_spec.rb +0 -47
@@ -0,0 +1,19 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'fileutils'
4
+
5
+ describe Ccp::Persistent::Tsv do
6
+ def root; Pathname("tmp/spec/ccp/persistent/tsv"); end
7
+
8
+ before do
9
+ FileUtils.rm_rf(root)
10
+ root.mkpath
11
+ end
12
+
13
+ describe "#tsv_path_for" do
14
+ it "should return tsv pathname" do
15
+ kvs = Ccp::Persistent::Tsv.new(root, :json)
16
+ kvs.tsv_path_for(:states).should == Pathname(root) + "states.json.tsv"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,110 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'fileutils'
4
+
5
+ describe Ccp::Persistent::Versioned do
6
+ def root; Pathname("tmp/spec/ccp/persistent/versioned"); end
7
+
8
+ before do
9
+ FileUtils.rm_rf(root)
10
+ root.mkpath
11
+ end
12
+
13
+ describe "#latest" do
14
+ it "should return a kvs with the latest dated file" do
15
+ # create directories
16
+ (root + "1.json" ).mkpath
17
+ (root + "10.xxx" ).mkpath
18
+ (root + "5.json" ).mkpath
19
+ (root + "20.yaml").mkpath
20
+
21
+ ver = Ccp::Persistent::Versioned.new(root, :kvs=>:dir, :ext=>:json)
22
+ kvs = ver.latest
23
+
24
+ kvs.should be_kind_of(Ccp::Persistent::Dir)
25
+ kvs.source.should == root + "20.yaml"
26
+ kvs.ext.should == "yaml"
27
+ end
28
+
29
+ it "should lookup timestamp when same dated files exist" do
30
+ # create directories
31
+ (root + "1.json").mkpath
32
+ (root + "1.yaml").mkpath
33
+
34
+ (root + "1.json").utime(100,100)
35
+ ver = Ccp::Persistent::Versioned.new(root, :kvs=>:dir, :ext=>:json)
36
+ ver.latest.ext.should == "yaml"
37
+
38
+ (root + "1.yaml").utime(10,10)
39
+ ver = Ccp::Persistent::Versioned.new(root, :kvs=>:dir, :ext=>:json)
40
+ ver.latest.ext.should == "json"
41
+ end
42
+
43
+ it "should return nil when no files" do
44
+ ver = Ccp::Persistent::Versioned.new(root, :kvs=>:dir, :ext=>:json)
45
+ ver.latest.should == nil
46
+ end
47
+ end
48
+
49
+ describe "#latest!" do
50
+ it "should return a kvs with the latest dated file" do
51
+ # create directories
52
+ [1, 10, 5, 100].each{|i| (root + "#{i}.json").mkpath}
53
+
54
+ ver = Ccp::Persistent::Versioned.new(root, :kvs=>:dir, :ext=>:json)
55
+ kvs = ver.latest!
56
+
57
+ kvs.should be_kind_of(Ccp::Persistent::Dir)
58
+ kvs.source.should == root + "100.json"
59
+ end
60
+
61
+ it "should return StorageNotFound when no files exist" do
62
+ ver = Ccp::Persistent::Versioned.new(root, :kvs=>:dir, :ext=>:json)
63
+
64
+ lambda {
65
+ ver.latest!
66
+ }.should raise_error(Ccp::Persistent::NotFound)
67
+ end
68
+ end
69
+
70
+ describe "#default" do
71
+ it "should return a kvs with the latest dated file" do
72
+ # create directories
73
+ [1, 10, 5, 100].each{|i| (root + "#{i}.json").mkpath}
74
+
75
+ ver = Ccp::Persistent::Versioned.new(root, :kvs=>:dir, :ext=>:json)
76
+ kvs = ver.default
77
+
78
+ kvs.should be_kind_of(Ccp::Persistent::Dir)
79
+ kvs.source.should == root + "100.json"
80
+ end
81
+
82
+ it "should create a new kvs when no files exist" do
83
+ ver = Ccp::Persistent::Versioned.new(root, :kvs=>:dir, :ext=>:json)
84
+ kvs = ver.default
85
+ kvs.should be_kind_of(Ccp::Persistent::Dir)
86
+ kvs.ext.should == "json"
87
+ end
88
+ end
89
+
90
+ describe "#now" do
91
+ it "should create a new kvs with current time" do
92
+ Time.stub!(:now) { Time.at(12345) }
93
+ ver = Ccp::Persistent::Versioned.new(root, :kvs=>:dir, :ext=>:json)
94
+ kvs = ver.now
95
+ kvs.should be_kind_of(Ccp::Persistent::Dir)
96
+ kvs.path.basename.to_s.should == "12345.json"
97
+ kvs.ext.should == "json"
98
+ end
99
+ end
100
+
101
+ describe "#[]" do
102
+ it "should return a kvs with given base name" do
103
+ ver = Ccp::Persistent::Versioned.new(root, :kvs=>:dir, :ext=>:json)
104
+ kvs = ver[:data]
105
+ kvs.should be_kind_of(Ccp::Persistent::Dir)
106
+ kvs.path.basename.to_s.should == "data.json"
107
+ kvs.ext.should == "json"
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,79 @@
1
+ require "spec_helper"
2
+ require 'fileutils'
3
+
4
+ describe Ccp::Receivers::Fixtures do
5
+ class TSFC # TestSaveFixtureCmd
6
+ include Ccp::Commands::Core
7
+
8
+ def execute
9
+ data[:a] # read
10
+ data[:x] = 10 # write
11
+ end
12
+ end
13
+
14
+ def load(path)
15
+ case path.extname
16
+ when ".json"; JSON.load(Pathname(path).read{})
17
+ when ".yaml"; YAML.load(Pathname(path).read{})
18
+ else; raise "load doesn't support #{path.extname}"
19
+ end
20
+ end
21
+
22
+ describe ".execute" do
23
+ before do
24
+ FileUtils.rm_rf("tmp")
25
+ end
26
+
27
+ context "(:fixture_save=>true)" do
28
+ it "should generate read/write fixtures in tmp/fixtures as json files" do
29
+ path = Pathname("tmp/fixtures")
30
+ data = {:a=>"a", :b=>"b", :x=>1, :y=>2}
31
+ opts = {:fixture_save=>true}
32
+
33
+ TSFC.execute(data.merge(opts))
34
+
35
+ load(path + "tsfc/read.json" ).should == {"a" => "a"}
36
+ load(path + "tsfc/write.json").should == {"x" => 10}
37
+ end
38
+ end
39
+
40
+ context "(:fixture_save=>true, :fixture_ext=>:yaml)" do
41
+ it "should generate read/write fixtures in tmp/fixtures as yaml files" do
42
+ path = Pathname("tmp/fixtures")
43
+ data = {:a=>"a", :b=>"b", :x=>1, :y=>2}
44
+ opts = {:fixture_save=>true, :fixture_ext=>:yaml}
45
+
46
+ TSFC.execute(data.merge(opts))
47
+
48
+ load(path + "tsfc/read.yaml" ).should == {"a" => "a"}
49
+ load(path + "tsfc/write.yaml").should == {"x" => 10}
50
+ end
51
+ end
52
+
53
+ context "(:fixture_save=>true, :fixture_dir=>...)" do
54
+ it "should generate read/write fixtures in <save_fixture_dir> as json files" do
55
+ path = Pathname("tmp/test/fixtures")
56
+ data = {:a=>"a", :b=>"b", :x=>1, :y=>2}
57
+ opts = {:fixture_save=>true, :fixture_dir=>path.to_s}
58
+
59
+ TSFC.execute(data.merge(opts))
60
+
61
+ load(path + "tsfc/read.json" ).should == {"a" => "a"}
62
+ load(path + "tsfc/write.json").should == {"x" => 10}
63
+ end
64
+ end
65
+
66
+ context "(:fixture_save=>true, :fixture_kvs=>:dir)" do
67
+ it "should generate json files in read/write dir" do
68
+ path = Pathname("tmp/fixtures")
69
+ data = {:a=>"a", :b=>"b", :x=>1, :y=>2}
70
+ opts = {:fixture_save=>true, :fixture_kvs=>:dir}
71
+
72
+ TSFC.execute(data.merge(opts))
73
+
74
+ load(path + "tsfc/read.json/a.json" ).should == "a"
75
+ load(path + "tsfc/write.json/x.json").should == 10
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,19 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'fileutils'
4
+
5
+ describe Ccp::Serializers::Core do
6
+ subject { Object.new.extend Ccp::Serializers::Core }
7
+
8
+ it "should provide ext" do
9
+ subject.methods.include?("ext").should == true
10
+ end
11
+
12
+ it "should provide encode" do
13
+ subject.methods.include?("encode").should == true
14
+ end
15
+
16
+ it "should provide decode" do
17
+ subject.methods.include?("decode").should == true
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'fileutils'
4
+
5
+ describe Ccp::Serializers::Json do
6
+ its(:ext) { should == "json" }
7
+
8
+ describe "with Array(Integer)" do
9
+ it "should encode" do
10
+ subject.encode([1,2,3]).should == "[1,2,3]"
11
+ end
12
+
13
+ it "should decode" do
14
+ subject.decode("[1,2,3]").should == [1,2,3]
15
+ end
16
+ end
17
+
18
+ describe "with {String => Integer}" do
19
+ it "should encode" do
20
+ subject.encode("foo" => 1).should == '{"foo":1}'
21
+ end
22
+
23
+ it "should decode" do
24
+ subject.decode('{"foo":1}').should == {"foo" => 1}
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,39 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'fileutils'
4
+
5
+ describe Ccp::Serializers do
6
+ describe "#lookup" do
7
+ context ":json" do
8
+ it "should return Ccp::Serializers::Json" do
9
+ subject.lookup(:json).should == Ccp::Serializers::Json
10
+ end
11
+ end
12
+
13
+ context "'json'" do
14
+ it "should return Ccp::Serializers::Json" do
15
+ subject.lookup('json').should == Ccp::Serializers::Json
16
+ end
17
+ end
18
+
19
+ context ":yaml" do
20
+ it "should return Ccp::Serializers::Yaml" do
21
+ subject.lookup(:yaml).should == Ccp::Serializers::Yaml
22
+ end
23
+ end
24
+
25
+ context "'yaml'" do
26
+ it "should return Ccp::Serializers::Yaml" do
27
+ subject.lookup('yaml').should == Ccp::Serializers::Yaml
28
+ end
29
+ end
30
+
31
+ context ":xxx" do
32
+ it "should raise NotFound" do
33
+ lambda {
34
+ subject.lookup(:xxx)
35
+ }.should raise_error(Ccp::Serializers::NotFound)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require 'fileutils'
4
+
5
+ describe Ccp::Serializers::Yaml do
6
+ its(:ext) { should == "yaml" }
7
+
8
+ describe "with Array(Integer)" do
9
+ it "should encode" do
10
+ subject.encode([1,2,3]).should == "--- \n- 1\n- 2\n- 3\n"
11
+ end
12
+
13
+ it "should decode" do
14
+ subject.decode("--- \n- 1\n- 2\n- 3\n").should == [1,2,3]
15
+ end
16
+ end
17
+
18
+ describe "with {String => Integer}" do
19
+ it "should encode" do
20
+ subject.encode("foo" => 1).should == "--- \nfoo: 1\n"
21
+ end
22
+
23
+ it "should decode" do
24
+ subject.decode("--- \nfoo: 1\n").should == {"foo" => 1}
25
+ end
26
+ end
27
+ 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: 21
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 7
10
- version: 0.1.7
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
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-01 00:00:00 +09:00
18
+ date: 2012-05-02 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -26,12 +26,12 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- hash: 17
29
+ hash: 21
30
30
  segments:
31
31
  - 0
32
+ - 2
32
33
  - 1
33
- - 5
34
- version: 0.1.5
34
+ version: 0.2.1
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
37
  - !ruby/object:Gem::Dependency
@@ -96,7 +96,6 @@ files:
96
96
  - Rakefile
97
97
  - ccp.gemspec
98
98
  - lib/ccp.rb
99
- - lib/ccp/colorize.rb
100
99
  - lib/ccp/commands.rb
101
100
  - lib/ccp/commands/base.rb
102
101
  - lib/ccp/commands/command_not_found.rb
@@ -106,30 +105,52 @@ files:
106
105
  - lib/ccp/commands/executable.rb
107
106
  - lib/ccp/commands/receivable.rb
108
107
  - lib/ccp/commands/resolvable.rb
109
- - lib/ccp/data.rb
110
108
  - lib/ccp/fixtures.rb
111
109
  - lib/ccp/fixtures/observer.rb
112
110
  - lib/ccp/fixtures/writers.rb
113
111
  - lib/ccp/invokers.rb
114
112
  - lib/ccp/invokers/base.rb
113
+ - lib/ccp/persistent.rb
114
+ - lib/ccp/persistent/base.rb
115
+ - lib/ccp/persistent/dir.rb
116
+ - lib/ccp/persistent/file.rb
117
+ - lib/ccp/persistent/tsv.rb
118
+ - lib/ccp/persistent/versioned.rb
115
119
  - lib/ccp/receivers.rb
116
120
  - lib/ccp/receivers/aroundable.rb
117
121
  - lib/ccp/receivers/base.rb
118
122
  - lib/ccp/receivers/commentable.rb
123
+ - lib/ccp/receivers/core.rb
119
124
  - lib/ccp/receivers/executable.rb
125
+ - lib/ccp/receivers/fixtures.rb
120
126
  - lib/ccp/receivers/global.rb
121
127
  - lib/ccp/receivers/none.rb
122
128
  - lib/ccp/receivers/profileable.rb
123
- - lib/ccp/receivers/save_fixture.rb
129
+ - lib/ccp/receivers/settings.rb
130
+ - lib/ccp/receivers/variables.rb
131
+ - lib/ccp/serializers.rb
132
+ - lib/ccp/serializers/core.rb
133
+ - lib/ccp/serializers/json.rb
134
+ - lib/ccp/serializers/yaml.rb
135
+ - lib/ccp/utils.rb
136
+ - lib/ccp/utils/colorize.rb
124
137
  - lib/ccp/version.rb
125
- - spec/commands_base_spec.rb
126
- - spec/commands_composite_spec.rb
127
- - spec/commands_core_spec.rb
128
- - spec/commands_executable_spec.rb
129
- - spec/data_spec.rb
130
- - spec/invokers_spec.rb
138
+ - spec/commands/base_spec.rb
139
+ - spec/commands/composite_spec.rb
140
+ - spec/commands/core_spec.rb
141
+ - spec/commands/executable_spec.rb
142
+ - spec/invokers/base_spec.rb
131
143
  - spec/models.rb
132
- - spec/save_fixture_spec.rb
144
+ - spec/persistent/base_spec.rb
145
+ - spec/persistent/dir_spec.rb
146
+ - spec/persistent/file_spec.rb
147
+ - spec/persistent/tsv_spec.rb
148
+ - spec/persistent/versioned_spec.rb
149
+ - spec/receivers/fixture_save_spec.rb
150
+ - spec/serializers/core_spec.rb
151
+ - spec/serializers/json_spec.rb
152
+ - spec/serializers/spec.rb
153
+ - spec/serializers/yaml_spec.rb
133
154
  - spec/spec_helper.rb
134
155
  has_rdoc: true
135
156
  homepage: http://github.com/maiha/ccp
data/lib/ccp/colorize.rb DELETED
@@ -1,13 +0,0 @@
1
- module Ccp
2
- module Colorize
3
- def colorize(text, ansi); "#{ansi}#{text}\e[0m"; end
4
- def red (text); colorize(text, "\e[31m"); end
5
- def green (text); colorize(text, "\e[32m"); end
6
- def yellow(text); colorize(text, "\e[33m"); end
7
- def blue (text); colorize(text, "\e[34m"); end
8
- def pink (text); colorize(text, "\e[35m"); end
9
- def aqua (text); colorize(text, "\e[36m"); end
10
-
11
- extend self
12
- end
13
- end