shuber-interface 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/interface.rb +1 -4
- data/lib/interface/abstract.rb +11 -5
- data/lib/interface/version.rb +1 -1
- data/test/interface_test.rb +12 -0
- metadata +3 -3
data/lib/interface.rb
CHANGED
@@ -45,10 +45,7 @@ module Interface
|
|
45
45
|
|
46
46
|
# Returns an array of methods from the specified <tt>interface</tt> that the current object does not implement
|
47
47
|
def unimplemented_methods_for(interface)
|
48
|
-
interface.instance_methods(false).reject
|
49
|
-
method = method.to_sym
|
50
|
-
(respond_to?(method, true) && self.method(method).owner != interface) || respond_to_missing?(method, true)
|
51
|
-
end.sort
|
48
|
+
interface.instance_methods(false).reject { |method| respond_to_missing?(method.to_sym, true) || self.method(method.to_sym).owner != interface }.sort
|
52
49
|
end
|
53
50
|
|
54
51
|
# <tt>Object#respond_to_missing?</tt> wasn't implemented until ruby version 1.9
|
data/lib/interface/abstract.rb
CHANGED
@@ -1,14 +1,20 @@
|
|
1
1
|
module Interface
|
2
|
-
# When extended, this
|
2
|
+
# When extended, this module will re-define an interface's methods to raise <tt>NotImplementedError</tt> when called (unless handled by <tt>method_missing</tt>)
|
3
3
|
module Abstract
|
4
|
-
def self.extended(
|
5
|
-
|
4
|
+
def self.extended(interface) # :nodoc:
|
5
|
+
interface.class_eval do
|
6
6
|
instance_methods(false).each do |method|
|
7
7
|
define_method(method) do |*args, &block|
|
8
|
+
methods = [:super, :method_missing]
|
8
9
|
begin
|
9
|
-
|
10
|
+
send(methods.shift, *args, &block)
|
10
11
|
rescue NoMethodError
|
11
|
-
|
12
|
+
if methods.empty?
|
13
|
+
raise NotImplementedError.new("#{self.class} needs to implement '#{method}' for interface #{interface}")
|
14
|
+
else
|
15
|
+
args.unshift(method.to_sym)
|
16
|
+
retry
|
17
|
+
end
|
12
18
|
end
|
13
19
|
end
|
14
20
|
end
|
data/lib/interface/version.rb
CHANGED
data/test/interface_test.rb
CHANGED
@@ -7,11 +7,18 @@ module Remote
|
|
7
7
|
def on
|
8
8
|
end
|
9
9
|
|
10
|
+
def on?
|
11
|
+
end
|
12
|
+
|
10
13
|
def off
|
11
14
|
end
|
12
15
|
end
|
13
16
|
|
14
17
|
class BrokenDevice
|
18
|
+
def on?
|
19
|
+
!!@power
|
20
|
+
end
|
21
|
+
|
15
22
|
implements Remote, MockInterface
|
16
23
|
end
|
17
24
|
|
@@ -87,4 +94,9 @@ class InterfaceTest < Test::Unit::TestCase
|
|
87
94
|
assert respond_to?(:respond_to_missing?)
|
88
95
|
end
|
89
96
|
|
97
|
+
def test_should_call_super
|
98
|
+
assert !BrokenDevice.new.on?
|
99
|
+
assert !Device.new.on?
|
100
|
+
end
|
101
|
+
|
90
102
|
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: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sean Huber
|