detach 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/detach.rb +15 -2
- data/test/test_detach.rb +13 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5abfdee74c072d0045db1449206a1981f76fb981
|
4
|
+
data.tar.gz: 9d734361d833db8179dc65aa230ec7d0554c523d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8237013c771426b631c600eeda141b69a93be5d00322a79d48ee790c26ad2e6dba6fa077fff37afd93c378640aafad44fcb39e031ddf6ace6ba3dc07893590e1
|
7
|
+
data.tar.gz: 05b0f1515d5ac61a891686be779f10c5b29cbfb63a5a92e0f15377fe2eb2bce3ca2e444d48d1c6274b8760654eb4e4b1da7376fffc883b15008f5d33bc0d1b2d
|
data/lib/detach.rb
CHANGED
@@ -29,7 +29,20 @@ module Detach
|
|
29
29
|
# Varargs and default values are interpolated with actual values. Predefined classes
|
30
30
|
# are compared to actual classes using equality and inheritence checks.
|
31
31
|
def method_missing(name, *args, &block)
|
32
|
-
|
32
|
+
obj_method_missing = -> {
|
33
|
+
BasicObject.instance_method(:method_missing).bind(self).call(name, *args, &block)
|
34
|
+
}
|
35
|
+
|
36
|
+
# some early returns are necessary when Detach has been mixed into
|
37
|
+
# the class of object 'main'
|
38
|
+
case name
|
39
|
+
when :to_ary
|
40
|
+
return obj_method_missing.call
|
41
|
+
when :taking
|
42
|
+
return Types.instance_method(:taking).bind(self.class).call if self.class == Object
|
43
|
+
end
|
44
|
+
|
45
|
+
(score,best) = (public_methods+protected_methods+private_methods).grep(/^#{Regexp.escape(name)}\(/).collect {|candidate|
|
33
46
|
# extract paramters
|
34
47
|
params = /\((.*)\)/.match(candidate.to_s)[1].scan(/(\w+)-([\w:)]+)/).collect {|s,t|
|
35
48
|
[s.to_sym, t.split(/::/).inject(Kernel) {|m,c| m = m.const_get(c)}]
|
@@ -65,7 +78,7 @@ module Detach
|
|
65
78
|
|
66
79
|
}.max {|a,b| a[0] <=> b[0]}
|
67
80
|
|
68
|
-
(not score or score == 0) ?
|
81
|
+
(not score or score == 0) ? obj_method_missing.call : method(best)[*args, &block]
|
69
82
|
end
|
70
83
|
|
71
84
|
# The Detach::Types module is inserted as a parent of the class which includes the
|
data/test/test_detach.rb
CHANGED
@@ -36,6 +36,10 @@ class TestDetach < Test::Unit::TestCase
|
|
36
36
|
assert_equal "P::M 42", Foo.new.foo(P::M.new)
|
37
37
|
end
|
38
38
|
|
39
|
+
def testMethodNamedTaking
|
40
|
+
assert_equal "hello", Bar.new.taking("hello")
|
41
|
+
end
|
42
|
+
|
39
43
|
def testConstructor
|
40
44
|
assert_equal "hello world", Wow.new("hello", "world").to_s
|
41
45
|
assert_equal "11", Wow.new(5, 6).to_s
|
@@ -118,6 +122,15 @@ class TestDetach < Test::Unit::TestCase
|
|
118
122
|
end
|
119
123
|
end
|
120
124
|
|
125
|
+
class Bar
|
126
|
+
include Detach
|
127
|
+
|
128
|
+
taking[String]
|
129
|
+
def taking(s)
|
130
|
+
@s = s
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
121
134
|
class Wow
|
122
135
|
include Detach
|
123
136
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: detach
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Calhoun
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A mixin which separates method definitions by argument types, effectively
|
14
14
|
allowing C++ or Java style overloading.
|