typed 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/typed/default.rb CHANGED
@@ -9,7 +9,13 @@ module Typed
9
9
  @kvs[key.to_s] = val
10
10
  end
11
11
 
12
- def regsiter_lazy(key, block)
12
+ def merge!(hash)
13
+ hash.each_pair do |key, val|
14
+ self[key] = val
15
+ end
16
+ end
17
+
18
+ def register_lazy(key, block)
13
19
  return if @kvs.exist?(key.to_s)
14
20
  raise ArgumentError, "Lazy default value needs block: #{key}" unless block
15
21
  @kvs[key.to_s] = Schema::LazyValue.new(block)
data/lib/typed/hash.rb CHANGED
@@ -24,7 +24,7 @@ module Typed
24
24
 
25
25
  def default(key = nil, &block)
26
26
  if key
27
- @default.regsiter_lazy(key, block)
27
+ @default.register_lazy(key, block)
28
28
  else
29
29
  @default
30
30
  end
@@ -113,6 +113,21 @@ module Typed
113
113
  keys.map{|key| self[key]}
114
114
  end
115
115
 
116
+ def merge!(hash)
117
+ hash.each_pair do |key, val|
118
+ self[key] = val
119
+ end
120
+
121
+ return self
122
+ end
123
+
124
+ ######################################################################
125
+ ### Conversions
126
+
127
+ def path(key)
128
+ self[key].must.coerced(Pathname, String=>proc{|i| Pathname(i)})
129
+ end
130
+
116
131
  ######################################################################
117
132
  ### Utils
118
133
 
data/lib/typed/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Typed
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ describe Typed::Default do
4
+ describe "#merge" do
5
+ it "should merge to hash" do
6
+ data = Typed::Hash.new
7
+ data.default.merge!(:a=>1, :b=>2)
8
+ data.keys.sort.should == ["a", "b"]
9
+ data["a"].should == 1
10
+ data["b"].should == 2
11
+ end
12
+
13
+ it "should merge to hash only when the key is not set" do
14
+ data = Typed::Hash.new
15
+ data[:a] = 10
16
+
17
+ data.default.merge!(:a=>1, :b=>2)
18
+ data.keys.sort.should == ["a", "b"]
19
+ data["a"].should == 10
20
+ data["b"].should == 2
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,28 @@
1
+ require "spec_helper"
2
+
3
+ describe Typed::Hash do
4
+ describe "#merge!" do
5
+ context "(empty)" do
6
+ it "should merge given hash as same as Hash" do
7
+ data = Typed::Hash.new
8
+ data.merge!("a" => 1, :b => "x")
9
+ data.keys.sort.should == ["a", "b"]
10
+ data["a"].should == 1
11
+ data["b"].should == "x"
12
+ end
13
+ end
14
+
15
+ context "(elements exist)" do
16
+ it "should merge given hash as same as Hash" do
17
+ data = Typed::Hash.new
18
+ data["a"] = 1
19
+
20
+ data.merge!("a" => 2, :b => "x", :c => [])
21
+ data.keys.sort.should == ["a", "b", "c"]
22
+ data["a"].should == 2
23
+ data["b"].should == "x"
24
+ data["c"].should == []
25
+ end
26
+ end
27
+ end
28
+ end
data/spec/path_spec.rb ADDED
@@ -0,0 +1,29 @@
1
+ require "spec_helper"
2
+
3
+ describe Typed::Hash do
4
+ describe "#path" do
5
+ it "should return a Pathname when it is a String" do
6
+ data = Typed::Hash.new
7
+ data["dir"] = "tmp/foo"
8
+
9
+ data.path("dir").should == Pathname("tmp/foo")
10
+ end
11
+
12
+ it "should return itself when it is a Pathname" do
13
+ data = Typed::Hash.new
14
+ data["dir"] = Pathname("tmp/foo")
15
+
16
+ data.path("dir").should == Pathname("tmp/foo")
17
+ end
18
+
19
+ it "should raise Must::Invalid if not pathname-able" do
20
+ data = Typed::Hash.new
21
+ data["dir"] = 10
22
+
23
+ lambda {
24
+ data.path("dir")
25
+ }.should raise_error(Must::Invalid)
26
+ end
27
+ end
28
+ end
29
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typed
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - maiha
@@ -84,8 +84,11 @@ files:
84
84
  - lib/typed/schema.rb
85
85
  - lib/typed/version.rb
86
86
  - spec/changes_spec.rb
87
+ - spec/default_spec.rb
87
88
  - spec/events_spec.rb
88
89
  - spec/hash_spec.rb
90
+ - spec/merge_spec.rb
91
+ - spec/path_spec.rb
89
92
  - spec/schema_spec.rb
90
93
  - spec/spec_helper.rb
91
94
  - typed.gemspec