must 0.2.6 → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +1 -0
- data/lib/must/rule.rb +10 -0
- data/lib/must/version.rb +1 -1
- data/test/duck_test.rb +14 -0
- metadata +4 -4
data/README
CHANGED
@@ -49,6 +49,7 @@ Duck Methods
|
|
49
49
|
duck(".foo") : same above
|
50
50
|
duck("#foo") : check whether object has "foo" instance method. (tested only in class/module)
|
51
51
|
duck?(...) : acts same as "duck", but this returns a just boolean
|
52
|
+
duck!("foo") : if foo exists, call it. otherwise raises Invalid
|
52
53
|
|
53
54
|
|
54
55
|
Struct Methods
|
data/lib/must/rule.rb
CHANGED
@@ -89,6 +89,16 @@ module Must
|
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
|
+
def duck!(method_name, &block)
|
93
|
+
if duck?(method_name)
|
94
|
+
@object.__send__(method_name)
|
95
|
+
elsif block_given?
|
96
|
+
block.call
|
97
|
+
else
|
98
|
+
raise Invalid, "#{method_name} is not defined"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
92
102
|
def struct?(target)
|
93
103
|
Must::StructInfo.new(@object).same?(target)
|
94
104
|
end
|
data/lib/must/version.rb
CHANGED
data/test/duck_test.rb
CHANGED
@@ -80,4 +80,18 @@ class DuckTest < Test::Unit::TestCase
|
|
80
80
|
assert_equal false, ClassFoo.must.duck?("#bar")
|
81
81
|
assert_equal false, ModuleFoo.must.duck?("#bar")
|
82
82
|
end
|
83
|
+
|
84
|
+
def test_duck_bang
|
85
|
+
assert_equal "1", 1.must.duck!("to_s")
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_duck_bang_raises_if_not_defined
|
89
|
+
assert_raise(Must::Invalid) {
|
90
|
+
ClassFoo.must.duck!("foo")
|
91
|
+
}
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_duck_bang_calls_block_if_not_defined
|
95
|
+
assert_equal 2, 1.must.duck!("to_xxx") { 2 }
|
96
|
+
end
|
83
97
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: must
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
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-
|
18
|
+
date: 2012-04-27 00:00:00 +09:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|