detach 0.0.5 → 0.1.0

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/detach.rb +17 -3
  3. data/test/test_detach.rb +30 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5abfdee74c072d0045db1449206a1981f76fb981
4
- data.tar.gz: 9d734361d833db8179dc65aa230ec7d0554c523d
3
+ metadata.gz: 70d08bcd4bfbae5a1aab1faa9900e26b0397ee1e
4
+ data.tar.gz: fcae820f173e8053c35e82c23f53fcac47e77668
5
5
  SHA512:
6
- metadata.gz: 8237013c771426b631c600eeda141b69a93be5d00322a79d48ee790c26ad2e6dba6fa077fff37afd93c378640aafad44fcb39e031ddf6ace6ba3dc07893590e1
7
- data.tar.gz: 05b0f1515d5ac61a891686be779f10c5b29cbfb63a5a92e0f15377fe2eb2bce3ca2e444d48d1c6274b8760654eb4e4b1da7376fffc883b15008f5d33bc0d1b2d
6
+ metadata.gz: 7742a36cc469786dc36c3aeea32a379f9d95fe6ce7c49bc072c1b331224baec0619e717a74a747b10bae20836bc10a11f2aa17776f3d786bcf429a98204c2e4a
7
+ data.tar.gz: 548716816c73bd3719737768c0f2f2bb3a43e78a17a37ba9b2c76ae072b02438569478d8500f368be37e21eb6ed906b6ab87dec5d49613196ebe249ca800c423
data/lib/detach.rb CHANGED
@@ -44,11 +44,12 @@ module Detach
44
44
 
45
45
  (score,best) = (public_methods+protected_methods+private_methods).grep(/^#{Regexp.escape(name)}\(/).collect {|candidate|
46
46
  # extract paramters
47
- params = /\((.*)\)/.match(candidate.to_s)[1].scan(/(\w+)-([\w:)]+)/).collect {|s,t|
47
+ params = /\((.*)\)/.match(candidate.to_s)[1].scan(/(\w+)-([\w:\)]+)/).collect {|s,t|
48
48
  [s.to_sym, t.split(/::/).inject(Kernel) {|m,c| m = m.const_get(c)}]
49
49
  }
50
50
  # form the list of all required argument classes
51
51
  ctypes = params.values_at(*params.each_index.select {|i| params[i].first == :req}).map(&:last)
52
+ nreq = ctypes.size
52
53
 
53
54
  # NOTE: ruby only allows a single *args, or a list of a=1, b=2--not both together--
54
55
  # only one of the following will execute
@@ -68,12 +69,21 @@ module Detach
68
69
  elsif ctypes.size == args.size
69
70
  score = args.map(&:class).zip(ctypes).inject(0) {|s,t|
70
71
  # apply each class comparison and require nonzero matches
71
- s and ->(n) {s += n if n > 0}[ [ :==, :<=, ].select {|op| ->(a,&b) {b[*a]}[t, &op]}.size ]
72
+ if s
73
+ if t[0].ancestors.include?(t[1])
74
+ s += t[1].ancestors.size
75
+ else
76
+ s = nil
77
+ end
78
+ end
79
+
72
80
  } || 0
73
81
  else
74
82
  score = 0
75
83
  end
76
84
 
85
+ score += (1 + nreq) if args.size == params.size
86
+
77
87
  [ score, candidate ]
78
88
 
79
89
  }.max {|a,b| a[0] <=> b[0]}
@@ -96,10 +106,14 @@ module Detach
96
106
  def taking
97
107
  self
98
108
  end
99
- # :stopdoc:
109
+ # Provides list of type names to decorator.
100
110
  def [](*types)
101
111
  @@types = types.flatten
102
112
  end
113
+ # Provides load-time method aliasing.
114
+ #
115
+ # All methods added to a class which are decorated as taking specified types
116
+ # are aliased in a form known and searched at run-time.
103
117
  def method_added(name)
104
118
  return unless @@types
105
119
 
data/test/test_detach.rb CHANGED
@@ -19,7 +19,6 @@ class TestDetach < Test::Unit::TestCase
19
19
  def testLists
20
20
  assert_equal "Object List [\"one\", 2, 3.0, \"4\"]", Foo.new.foo('one', 2, 3.0, '4')
21
21
  assert_equal "String List [\"one\", \"two\", \"three\", \"four\"]", Foo.new.foo('one', 'two', 'three', 'four')
22
-
23
22
  end
24
23
 
25
24
  def testMixedLists
@@ -45,6 +44,12 @@ class TestDetach < Test::Unit::TestCase
45
44
  assert_equal "11", Wow.new(5, 6).to_s
46
45
  end
47
46
 
47
+ def testDefinitionOrder
48
+ assert_equal "Nonce", Junk.new.foo
49
+ assert_equal "String one", Junk.new.foo('one')
50
+ assert_equal "String List [\"one\", \"two\", \"three\", \"four\"]", Junk.new.foo('one', 'two', 'three', 'four')
51
+ end
52
+
48
53
 
49
54
  #
50
55
  # helper definitions
@@ -149,6 +154,30 @@ class TestDetach < Test::Unit::TestCase
149
154
  end
150
155
  end
151
156
 
157
+ class Junk
158
+ include Detach
159
+
160
+ taking[Object]
161
+ def foo(*a)
162
+ "Object List #{a}"
163
+ end
164
+
165
+ taking[String]
166
+ def foo(*a)
167
+ "String List #{a}"
168
+ end
169
+
170
+ taking[]
171
+ def foo
172
+ "Nonce"
173
+ end
174
+
175
+ taking[String]
176
+ def foo(a)
177
+ "String #{a}"
178
+ end
179
+
180
+ end
152
181
 
153
182
  end
154
183
 
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.5
4
+ version: 0.1.0
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-20 00:00:00.000000000 Z
11
+ date: 2014-08-22 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.