shuber-interface 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/interface/abstract.rb +8 -2
- data/lib/interface/version.rb +1 -1
- data/test/interface_test.rb +12 -1
- metadata +3 -3
data/lib/interface/abstract.rb
CHANGED
@@ -1,10 +1,16 @@
|
|
1
1
|
module Interface
|
2
|
-
# When extended, this abstract interface will re-define an object's methods to raise <tt>NotImplementedError</tt> when called
|
2
|
+
# When extended, this abstract interface will re-define an object's methods to raise <tt>NotImplementedError</tt> when called (unless handled by <tt>method_missing</tt>)
|
3
3
|
module Abstract
|
4
4
|
def self.extended(base) # :nodoc:
|
5
5
|
base.class_eval do
|
6
6
|
instance_methods(false).each do |method|
|
7
|
-
define_method(method)
|
7
|
+
define_method(method) do |*args|
|
8
|
+
begin
|
9
|
+
method_missing(method.to_sym, *args)
|
10
|
+
rescue NoMethodError
|
11
|
+
raise NotImplementedError.new("#{self.class} needs to implement '#{method}' for interface #{base}")
|
12
|
+
end
|
13
|
+
end
|
8
14
|
end
|
9
15
|
end
|
10
16
|
end
|
data/lib/interface/version.rb
CHANGED
data/test/interface_test.rb
CHANGED
@@ -6,6 +6,9 @@ module MockInterface end
|
|
6
6
|
module Remote
|
7
7
|
def on
|
8
8
|
end
|
9
|
+
|
10
|
+
def off
|
11
|
+
end
|
9
12
|
end
|
10
13
|
|
11
14
|
class BrokenDevice
|
@@ -18,6 +21,10 @@ class Device < BrokenDevice
|
|
18
21
|
def on
|
19
22
|
@power = true
|
20
23
|
end
|
24
|
+
|
25
|
+
def method_missing(method, *args)
|
26
|
+
method == :off ? @power = false : super
|
27
|
+
end
|
21
28
|
end
|
22
29
|
|
23
30
|
class InterfaceTest < Test::Unit::TestCase
|
@@ -34,10 +41,14 @@ class InterfaceTest < Test::Unit::TestCase
|
|
34
41
|
assert_raises(NotImplementedError) { BrokenDevice.new.on }
|
35
42
|
end
|
36
43
|
|
37
|
-
def
|
44
|
+
def test_should_not_raise_not_implemented_error_if_method_is_defined
|
38
45
|
assert Device.new.on
|
39
46
|
end
|
40
47
|
|
48
|
+
def test_should_not_raise_not_implemented_error_if_method_is_defined_by_method_missing
|
49
|
+
assert !Device.new.off
|
50
|
+
end
|
51
|
+
|
41
52
|
def test_should_return_interfaces
|
42
53
|
assert_equal [Remote, MockInterface], Device.interfaces
|
43
54
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shuber-interface
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sean Huber
|