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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5cfe8f1391a79d44172df87f2d2210b86aa66fc7
4
- data.tar.gz: ccaee3b2c26c04a208fc45b0881ed6fe97c7dc26
3
+ metadata.gz: d975637a4c55f40bdf6a33590aa958cffdbf0aa3
4
+ data.tar.gz: 9a077af69a97c8ced3fc0ff08be393079464aa17
5
5
  SHA512:
6
- metadata.gz: 2aa379e9a76ab47b01a8fb51da01c56a966ac7fac9b046264aefeb412249ebfa3e55717e93e9d45d39322d4a9b2887a58e0521210a4358f11e051aadbff6a405
7
- data.tar.gz: d411a875eaced3f0f10f809639229f6a762b1b11800cc124d24349474b8ab27f0919357f1a922ce780a56f7ac4f3f49fc1971d09aebe610d999ed2254be85762
6
+ metadata.gz: dc40bf40542e8ab5cffa4cf1eb918a9be09b9bea5292d0c0840c41b659f411c364a015cb8a317741c622c99b15b891ced694457e9b7c076f435e31c7cb6b8e57
7
+ data.tar.gz: a5bbe28efda4a1d9bc0d68d5cc87819daa0e7db3cef8270fd46e1dc84aa1f58ad52d4339488519cf407880e2986af96f41621168ff28c0a83572dece96b156ef
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
- ruby-multidispatch
1
+ multidispatch
2
2
  ==================
3
3
 
4
- Very basic multidispatch in Ruby using natural syntax.
5
- It solves only one problem: converts
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 'ruby-multidispatch'
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 ruby-multidispatch
62
+ $ gem install multidispatch
62
63
 
63
64
  ## Usage
64
65
 
65
- See samples:
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
- Currently supports overloads by count of mandatory parameters.
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
@@ -1,3 +1,3 @@
1
1
  module Multidispatch
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  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
@@ -0,0 +1,4 @@
1
+ # A sample Gemfile
2
+ source "https://rubygems.org"
3
+
4
+ gem "multidispatch"
data/sample/main.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'multidispatch'
2
+ require './morning'
3
+
4
+ morning = Morning.new
5
+ morning.say
6
+ morning.say("Foo")
7
+ morning.say("Foo", 5)
data/sample/morning.rb ADDED
@@ -0,0 +1,15 @@
1
+ class Morning
2
+ include Multidispatch
3
+
4
+ def say
5
+ puts "Good morning!"
6
+ end
7
+
8
+ def say(name)
9
+ puts "Good morning, #{name}!"
10
+ end
11
+
12
+ def say(name, n)
13
+ n.times { |i| puts "#{i+1}. Good morning, #{name}!" }
14
+ end
15
+ end
@@ -1,31 +1,39 @@
1
+ require "spec_helper"
2
+
1
3
  describe "integration test" do
2
4
 
3
- it "should not overwrite methods of previous classes" do
5
+ context "when 2 classes have method with same name" do
4
6
  class Intersect1
5
7
  include Multidispatch
6
- def foo() "foo"; end
8
+ def foo() 1; end
7
9
  end
8
10
 
9
11
  class Intersect2
10
12
  include Multidispatch
11
- def foo() "bar"; end
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
- Intersect1.new.foo.should == "foo"
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
- it "should work" do
18
- class Foo
19
- include Multidispatch
25
+ class Bar
26
+ include Multidispatch
20
27
 
21
- def foo() "foo"; end
22
- def foo(a) "foo #{a}"; end
23
- def foo(n, a) "#{n} foo #{a}"; end
24
- end
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
- f = Foo.new
27
- f.foo().should == "foo"
28
- f.foo(5).should == "foo 5"
29
- f.foo(0,1).should == "0 foo 1"
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.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-01 00:00:00.000000000 Z
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