multidispatch 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -12
- data/lib/multidispatch/store.rb +43 -0
- data/lib/multidispatch/version.rb +1 -1
- data/lib/multidispatch.rb +1 -40
- data/sample/Gemfile +4 -0
- data/sample/main.rb +7 -0
- data/sample/morning.rb +15 -0
- data/spec/integration_spec.rb +23 -15
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d975637a4c55f40bdf6a33590aa958cffdbf0aa3
|
4
|
+
data.tar.gz: 9a077af69a97c8ced3fc0ff08be393079464aa17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc40bf40542e8ab5cffa4cf1eb918a9be09b9bea5292d0c0840c41b659f411c364a015cb8a317741c622c99b15b891ced694457e9b7c076f435e31c7cb6b8e57
|
7
|
+
data.tar.gz: a5bbe28efda4a1d9bc0d68d5cc87819daa0e7db3cef8270fd46e1dc84aa1f58ad52d4339488519cf407880e2986af96f41621168ff28c0a83572dece96b156ef
|
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
-
|
1
|
+
multidispatch
|
2
2
|
==================
|
3
3
|
|
4
|
-
Very basic multidispatch in Ruby using natural syntax.
|
5
|
-
|
4
|
+
Very basic multidispatch in **Ruby** using natural syntax.
|
5
|
+
Currently supports overloads by count of mandatory parameters.
|
6
|
+
So, it solves only one problem. Converts the following code:
|
6
7
|
|
7
8
|
```ruby
|
8
9
|
class BoilerplateClass
|
@@ -23,7 +24,7 @@ class BoilerplateClass
|
|
23
24
|
end
|
24
25
|
```
|
25
26
|
|
26
|
-
to
|
27
|
+
to this nice one:
|
27
28
|
|
28
29
|
```ruby
|
29
30
|
class MultidispatchClass
|
@@ -50,7 +51,7 @@ end
|
|
50
51
|
|
51
52
|
Add this line to your application's Gemfile:
|
52
53
|
|
53
|
-
gem '
|
54
|
+
gem 'multidispatch'
|
54
55
|
|
55
56
|
And then execute:
|
56
57
|
|
@@ -58,17 +59,15 @@ And then execute:
|
|
58
59
|
|
59
60
|
Or install it yourself as:
|
60
61
|
|
61
|
-
$ gem install
|
62
|
+
$ gem install multidispatch
|
62
63
|
|
63
64
|
## Usage
|
64
65
|
|
65
|
-
See
|
66
|
-
|
67
|
-
* multidispatch.rb - the module itself
|
68
|
-
* morning.rb, night.rb - sample classes using multidispatch module
|
69
|
-
* main.rb - quick CLI test for **Morning** and **Night**
|
66
|
+
See **sample/** folder:
|
70
67
|
|
71
|
-
|
68
|
+
* morning.rb - sample class using **multidispatch** module
|
69
|
+
* main.rb - quick CLI test for **Morning**
|
70
|
+
* Gemfile - sample gemfile
|
72
71
|
|
73
72
|
## Contributing
|
74
73
|
|
@@ -78,4 +77,5 @@ Currently supports overloads by count of mandatory parameters.
|
|
78
77
|
4. Push to the branch (`git push origin my-new-feature`)
|
79
78
|
5. Create new Pull Request
|
80
79
|
|
80
|
+
... do not forget tests
|
81
81
|
... and Happy Hacking!
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Multidispatch
|
2
|
+
|
3
|
+
class Store
|
4
|
+
attr_reader :store
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@store = {}
|
8
|
+
@locked = nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def get(klass, method_name, arity)
|
12
|
+
class_methods = @store[klass.name] || {}
|
13
|
+
methods = class_methods[method_name] || {}
|
14
|
+
method_body = methods[-1] || methods[arity]
|
15
|
+
raise NoMethodError unless method_body
|
16
|
+
method_body
|
17
|
+
end
|
18
|
+
|
19
|
+
def set(klass, method_name)
|
20
|
+
return nil if locked?
|
21
|
+
|
22
|
+
class_methods = @store[klass.name] ||= {}
|
23
|
+
methods = class_methods[method_name] ||= {}
|
24
|
+
method = klass.instance_method(method_name)
|
25
|
+
methods[method.arity] = method
|
26
|
+
end
|
27
|
+
|
28
|
+
def locked?
|
29
|
+
@locked
|
30
|
+
end
|
31
|
+
|
32
|
+
def lock!
|
33
|
+
@locked = true
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def release!
|
38
|
+
@locked = nil
|
39
|
+
self
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
data/lib/multidispatch.rb
CHANGED
@@ -1,47 +1,8 @@
|
|
1
|
+
require "multidispatch/store"
|
1
2
|
require "multidispatch/version"
|
2
3
|
|
3
4
|
module Multidispatch
|
4
5
|
|
5
|
-
class Store
|
6
|
-
attr_reader :store
|
7
|
-
|
8
|
-
def initialize
|
9
|
-
@store = {}
|
10
|
-
@locked = nil
|
11
|
-
end
|
12
|
-
|
13
|
-
def get(klass, method_name, arity)
|
14
|
-
class_methods = @store[klass.name] || {}
|
15
|
-
methods = class_methods[method_name] || {}
|
16
|
-
method_body = methods[-1] || methods[arity]
|
17
|
-
raise NoMethodError unless method_body
|
18
|
-
method_body
|
19
|
-
end
|
20
|
-
|
21
|
-
def set(klass, method_name)
|
22
|
-
return nil if locked?
|
23
|
-
|
24
|
-
class_methods = @store[klass.name] ||= {}
|
25
|
-
methods = class_methods[method_name] ||= {}
|
26
|
-
method = klass.instance_method(method_name)
|
27
|
-
methods[method.arity] = method
|
28
|
-
end
|
29
|
-
|
30
|
-
def locked?
|
31
|
-
@locked
|
32
|
-
end
|
33
|
-
|
34
|
-
def lock!
|
35
|
-
@locked = true
|
36
|
-
self
|
37
|
-
end
|
38
|
-
|
39
|
-
def release!
|
40
|
-
@locked = nil
|
41
|
-
self
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
6
|
def self.included(base)
|
46
7
|
base.extend(ClassMethods)
|
47
8
|
end
|
data/sample/Gemfile
ADDED
data/sample/main.rb
ADDED
data/sample/morning.rb
ADDED
data/spec/integration_spec.rb
CHANGED
@@ -1,31 +1,39 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
1
3
|
describe "integration test" do
|
2
4
|
|
3
|
-
|
5
|
+
context "when 2 classes have method with same name" do
|
4
6
|
class Intersect1
|
5
7
|
include Multidispatch
|
6
|
-
def foo()
|
8
|
+
def foo() 1; end
|
7
9
|
end
|
8
10
|
|
9
11
|
class Intersect2
|
10
12
|
include Multidispatch
|
11
|
-
def foo()
|
13
|
+
def foo() 2; end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "does not overwrite previously defined method" do
|
17
|
+
Intersect1.new.foo.should == 1
|
12
18
|
end
|
13
19
|
|
14
|
-
|
20
|
+
it "allows to define next method with same name" do
|
21
|
+
Intersect2.new.foo.should == 2
|
22
|
+
end
|
15
23
|
end
|
16
24
|
|
17
|
-
|
18
|
-
|
19
|
-
include Multidispatch
|
25
|
+
class Bar
|
26
|
+
include Multidispatch
|
20
27
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
28
|
+
def foo() "foo"; end
|
29
|
+
def foo(a) "foo #{a}"; end
|
30
|
+
def foo(n, a) "#{n} foo #{a}"; end
|
31
|
+
end
|
25
32
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
33
|
+
describe Bar do
|
34
|
+
it { subject.foo.should == "foo" }
|
35
|
+
it { subject.foo(5).should == "foo 5" }
|
36
|
+
it { subject.foo(0,1).should == "0 foo 1" }
|
30
37
|
end
|
38
|
+
|
31
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multidispatch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Iaroslav Sergieiev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -67,8 +67,12 @@ files:
|
|
67
67
|
- README.md
|
68
68
|
- Rakefile
|
69
69
|
- lib/multidispatch.rb
|
70
|
+
- lib/multidispatch/store.rb
|
70
71
|
- lib/multidispatch/version.rb
|
71
72
|
- multidispatch.gemspec
|
73
|
+
- sample/Gemfile
|
74
|
+
- sample/main.rb
|
75
|
+
- sample/morning.rb
|
72
76
|
- spec/integration_spec.rb
|
73
77
|
- spec/multidispatch_spec.rb
|
74
78
|
- spec/spec_helper.rb
|