ccp 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/ccp.gemspec +1 -2
- data/lib/ccp.rb +1 -1
- data/lib/ccp/data.rb +2 -88
- data/lib/ccp/version.rb +1 -1
- data/spec/commands_spec.rb +1 -1
- data/spec/data_spec.rb +6 -117
- metadata +13 -27
data/Gemfile
CHANGED
data/ccp.gemspec
CHANGED
@@ -18,8 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
s.add_dependency "
|
22
|
-
s.add_dependency "must", ">= 0.2.5"
|
21
|
+
s.add_dependency "typed", ">= 0.1.0"
|
23
22
|
s.add_dependency "dsl_accessor", ">= 0.4.0"
|
24
23
|
|
25
24
|
s.add_development_dependency "rspec"
|
data/lib/ccp.rb
CHANGED
data/lib/ccp/data.rb
CHANGED
@@ -4,100 +4,14 @@ require 'pathname'
|
|
4
4
|
|
5
5
|
module Ccp
|
6
6
|
module Data
|
7
|
-
|
8
|
-
|
9
|
-
class KVS
|
10
|
-
delegate :[]=, :to=>"@hash"
|
11
|
-
|
12
|
-
LazyValue = Struct.new(:block)
|
13
|
-
|
14
|
-
class Default
|
15
|
-
def initialize(kvs)
|
16
|
-
@kvs = kvs
|
17
|
-
end
|
18
|
-
|
19
|
-
def []=(key, val)
|
20
|
-
return if @kvs.exist?(key)
|
21
|
-
@kvs[key] = val
|
22
|
-
end
|
23
|
-
|
24
|
-
def regsiter_lazy(key, block)
|
25
|
-
return if @kvs.exist?(key)
|
26
|
-
raise ArgumentError, "Lazy default value needs block: #{key}" unless block
|
27
|
-
@kvs[key] = LazyValue.new(block)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def initialize
|
32
|
-
@hash = {}
|
33
|
-
@default = Default.new(self)
|
34
|
-
end
|
35
|
-
|
36
|
-
######################################################################
|
37
|
-
### Default values
|
38
|
-
|
39
|
-
def default(key = nil, &block)
|
40
|
-
if key
|
41
|
-
@default.regsiter_lazy(key, block)
|
42
|
-
else
|
43
|
-
@default
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
######################################################################
|
48
|
-
### Accessor
|
49
|
-
|
50
|
-
def [](key)
|
51
|
-
if exist?(key)
|
52
|
-
return load(key)
|
53
|
-
else
|
54
|
-
from = caller.is_a?(Array) ? caller.first : self.class
|
55
|
-
raise NotDefined, "'#{key}' is not initialized\n#{from}"
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
######################################################################
|
60
|
-
### Testing
|
61
|
-
|
62
|
-
def exist?(key)
|
63
|
-
@hash.has_key?(key)
|
64
|
-
end
|
65
|
-
|
66
|
-
def set?(key)
|
67
|
-
!! (exist?(key) && self[key])
|
68
|
-
end
|
69
|
-
|
70
|
-
def check(key, type)
|
71
|
-
self[key].must.struct(type) {
|
72
|
-
summary = self[key].inspect
|
73
|
-
summary = summary[0,200] + "..." if summary.size > 200
|
74
|
-
raise TypeError, "'#{key}' expects '#{type.inspect}', but got #{summary}"
|
75
|
-
}
|
76
|
-
end
|
77
|
-
|
78
|
-
######################################################################
|
79
|
-
### Utils
|
80
|
-
|
7
|
+
module Pathable
|
81
8
|
def path(key)
|
82
9
|
self[key].must.coerced(Pathname, String=>proc{|i| Pathname(i)})
|
83
10
|
end
|
84
|
-
|
85
|
-
def inspect
|
86
|
-
keys = @hash.keys.map(&:to_s).sort.join(',')
|
87
|
-
"{#{keys}}"
|
88
|
-
end
|
89
|
-
|
90
|
-
private
|
91
|
-
def load(key)
|
92
|
-
# LazyValue should be evaluated at runtime
|
93
|
-
value = @hash[key]
|
94
|
-
value = value.block.call if value.is_a?(LazyValue)
|
95
|
-
return value
|
96
|
-
end
|
97
11
|
end
|
98
12
|
|
99
13
|
def data
|
100
|
-
@data ||=
|
14
|
+
@data ||= Typed::Hash.new.extend Pathable
|
101
15
|
end
|
102
16
|
|
103
17
|
def data?(key)
|
data/lib/ccp/version.rb
CHANGED
data/spec/commands_spec.rb
CHANGED
@@ -6,7 +6,7 @@ describe Ccp::Commands::Core do
|
|
6
6
|
# data container
|
7
7
|
it { should respond_to(:data?) }
|
8
8
|
it { should respond_to(:data) }
|
9
|
-
its(:data) { should be_kind_of(
|
9
|
+
its(:data) { should be_kind_of(Typed::Hash) }
|
10
10
|
|
11
11
|
# executable
|
12
12
|
it { should respond_to(:execute) }
|
data/spec/data_spec.rb
CHANGED
@@ -1,128 +1,17 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Ccp::Data
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
it "should behave like hash" do
|
8
|
-
subject[:foo] = 1
|
9
|
-
subject[:foo].should == 1
|
10
|
-
end
|
11
|
-
|
12
|
-
describe "#[]" do
|
13
|
-
it "should raise NotDefined if no data exist for the key" do
|
14
|
-
lambda {
|
15
|
-
subject[:foo]
|
16
|
-
}.should raise_error(Ccp::Data::NotDefined)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
describe "#[]=" do
|
21
|
-
it "can override values" do
|
22
|
-
subject[:foo] = 1
|
23
|
-
subject[:foo] = 2
|
24
|
-
subject[:foo].should == 2
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
######################################################################
|
29
|
-
### Testing
|
30
|
-
|
31
|
-
describe "#exist?" do
|
32
|
-
it "should return true if the value is set" do
|
33
|
-
subject[:foo] = 1
|
34
|
-
subject.exist?(:foo).should == true
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should return false if the value is not set" do
|
38
|
-
subject.exist?(:foo).should == false
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
describe "#set?" do
|
43
|
-
it "should return true if the value is set and not (nil|false)" do
|
44
|
-
subject[:foo] = 0
|
45
|
-
subject.set?(:foo).should == true
|
46
|
-
|
47
|
-
subject.default[:bar] = 0
|
48
|
-
subject.set?(:bar).should == true
|
49
|
-
|
50
|
-
subject.default(:baz) { 0 }
|
51
|
-
subject.set?(:baz).should == true
|
52
|
-
end
|
53
|
-
|
54
|
-
it "should return false if the value is set but (nil|false)" do
|
55
|
-
subject[:foo] = nil
|
56
|
-
subject.set?(:foo).should == false
|
57
|
-
|
58
|
-
subject[:foo] = false
|
59
|
-
subject.set?(:foo).should == false
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe "#check" do
|
64
|
-
it "should satisfy its type" do
|
65
|
-
subject[:foo] = 1
|
66
|
-
lambda {
|
67
|
-
subject.check(:foo, Integer)
|
68
|
-
}.should_not raise_error
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should satisfy its struct" do
|
72
|
-
subject[:foo] = {:a => "text"}
|
73
|
-
lambda {
|
74
|
-
subject.check(:foo, {Symbol => String})
|
75
|
-
}.should_not raise_error
|
76
|
-
end
|
77
|
-
|
78
|
-
it "should raise TypeError if not satisfied" do
|
79
|
-
subject[:foo] = {:a => "text"}
|
80
|
-
lambda {
|
81
|
-
subject.check(:foo, Integer)
|
82
|
-
}.should raise_error(TypeError)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
######################################################################
|
87
|
-
### Default values
|
88
|
-
|
89
|
-
describe "#default" do
|
90
|
-
it "should set default value" do
|
91
|
-
subject.default[:foo] = 1
|
92
|
-
subject.exist?(:foo).should == true
|
93
|
-
subject[:foo].should == 1
|
94
|
-
end
|
95
|
-
|
96
|
-
it "should be overriden by []=" do
|
97
|
-
subject.default[:foo] = 1
|
98
|
-
subject[:foo] = 2
|
99
|
-
subject[:foo].should == 2
|
100
|
-
end
|
101
|
-
|
102
|
-
it "should not affect data when already set" do
|
103
|
-
subject[:foo] = 1
|
104
|
-
subject.default[:foo] = 2
|
105
|
-
subject[:foo].should == 1
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
describe "#default(&block)" do
|
110
|
-
it "should set lazy default value" do
|
111
|
-
@a = 1
|
112
|
-
subject.default(:foo) { @a }
|
113
|
-
@a = 2
|
114
|
-
subject[:foo].should == 2
|
115
|
-
end
|
116
|
-
end
|
3
|
+
describe Ccp::Data do
|
4
|
+
subject { Object.new.extend Ccp::Data }
|
5
|
+
def data; subject.data; end
|
117
6
|
|
118
7
|
######################################################################
|
119
8
|
### Utils
|
120
9
|
|
121
10
|
describe "#path" do
|
122
11
|
it "should return pathname" do
|
123
|
-
|
124
|
-
|
125
|
-
|
12
|
+
data[:foo] = __FILE__
|
13
|
+
data.path(:foo).should be_kind_of(Pathname)
|
14
|
+
data.path(:foo).basename.should == Pathname("data_spec.rb")
|
126
15
|
end
|
127
16
|
end
|
128
17
|
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:
|
4
|
+
hash: 31
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- maiha
|
@@ -15,43 +15,29 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-02-
|
18
|
+
date: 2012-02-10 00:00:00 +09:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
name:
|
22
|
+
name: typed
|
23
23
|
prerelease: false
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
hash:
|
29
|
+
hash: 27
|
30
30
|
segments:
|
31
31
|
- 0
|
32
|
-
|
33
|
-
type: :runtime
|
34
|
-
version_requirements: *id001
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: must
|
37
|
-
prerelease: false
|
38
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
|
-
requirements:
|
41
|
-
- - ">="
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
hash: 29
|
44
|
-
segments:
|
32
|
+
- 1
|
45
33
|
- 0
|
46
|
-
|
47
|
-
- 5
|
48
|
-
version: 0.2.5
|
34
|
+
version: 0.1.0
|
49
35
|
type: :runtime
|
50
|
-
version_requirements: *
|
36
|
+
version_requirements: *id001
|
51
37
|
- !ruby/object:Gem::Dependency
|
52
38
|
name: dsl_accessor
|
53
39
|
prerelease: false
|
54
|
-
requirement: &
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
55
41
|
none: false
|
56
42
|
requirements:
|
57
43
|
- - ">="
|
@@ -63,11 +49,11 @@ dependencies:
|
|
63
49
|
- 0
|
64
50
|
version: 0.4.0
|
65
51
|
type: :runtime
|
66
|
-
version_requirements: *
|
52
|
+
version_requirements: *id002
|
67
53
|
- !ruby/object:Gem::Dependency
|
68
54
|
name: rspec
|
69
55
|
prerelease: false
|
70
|
-
requirement: &
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
71
57
|
none: false
|
72
58
|
requirements:
|
73
59
|
- - ">="
|
@@ -77,7 +63,7 @@ dependencies:
|
|
77
63
|
- 0
|
78
64
|
version: "0"
|
79
65
|
type: :development
|
80
|
-
version_requirements: *
|
66
|
+
version_requirements: *id003
|
81
67
|
description: CCP
|
82
68
|
email:
|
83
69
|
- maiha@wota.jp
|