defmatch 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -0
- data/lib/defmatch/version.rb +1 -1
- data/lib/defmatch.rb +13 -0
- data/spec/defmatch_spec.rb +46 -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: 4d48a7b93d105306536da143e7762fe0184b5dbd
|
4
|
+
data.tar.gz: 982228cc5e465124ea6f6a6745ee729789c214ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2e95091e117a324eee5d93437d5a4a7697779c46168da203a70b4a9f5c9cce9a4f61abc3785735d339998dbd0889eb2e631e6d665b82dcf7f813e28ac4d2a21
|
7
|
+
data.tar.gz: 33f29be2d23840e56ba94f1eb14c693ec277c08a228195621af71ce68e7bb7f7ee4ee2d94d34191ec0ee16e675280cb43b8641cbeb653b884fc2f6425afb445c
|
data/README.md
CHANGED
@@ -61,6 +61,8 @@ class TestMe
|
|
61
61
|
defmatch(:magic,Fixnum,Fixnum) {|a,b| "Found two numbers #{a}:#{b}" }
|
62
62
|
# Run this function when there is a single argument that is equal to "banana" (not a great example as this could be done with a literal)
|
63
63
|
defmatch(:magic,lambda {|arg| arg == "banana" }) {|arg| "I matched using a procedure that made sure \"banana\" == #{arg}" }
|
64
|
+
# Run this function with no arguments
|
65
|
+
defmatch(:magic) { "nifty" }
|
64
66
|
end
|
65
67
|
|
66
68
|
#Now you have an instance method called magic that dispatches what runs based on the patterns you defined and their associated block
|
@@ -70,6 +72,8 @@ x.magic([1,2,3]) # -> Matches the second
|
|
70
72
|
x.magic(:literally) # -> You get the idea
|
71
73
|
x.magic(2,3)
|
72
74
|
x.magic("banana")
|
75
|
+
x.magic()
|
76
|
+
x.magic("I","never","defined","this","to","match") # -> ArgumentError: No function clause matching arguments
|
73
77
|
```
|
74
78
|
|
75
79
|
This can come in very handy, but remember that the order in which you define things matters. Lets say I define my magic function like this:
|
@@ -83,7 +87,26 @@ This can come in very handy, but remember that the order in which you define thi
|
|
83
87
|
|
84
88
|
Even if I run ```x.magic(1)``` I will get ```2``` as the result. The second defmatch will never be matched because there is a more general match case above it. Order matters. Define your most specific matches first.
|
85
89
|
|
90
|
+
If you want to create class methods (yes there are no true class methods in ruby, but it's a convient definition) you can use the ```defclassmatch``` method. It works just like ```defmatch``` but makes a class method instead.
|
91
|
+
|
92
|
+
## A note about inheritance
|
93
|
+
If you've defined a class you intend to inherit from and it uses Defmatch then be warned. Presently Defmatch defines an ```inherited``` method for your class when you extend it. So if you
|
94
|
+
use this method yourself then you'll be overwriting Defmatch's and you'll get odd errors when trying to use the defmatch/defclassmatch defined methods in your subclass. The work around for this is to reference the Defmatch version of the inherited function in your own inherited function.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
class MySuperClass
|
98
|
+
|
99
|
+
def self.inherited(subklass)
|
100
|
+
Defmatch.inherited(self,subklass)
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
It's not a great solution, but it will work until I come up with a better one.
|
107
|
+
|
86
108
|
## Roadmap
|
87
109
|
|
88
110
|
* Add parameter deconstruction
|
111
|
+
* Cleaner way to handle inheritance
|
89
112
|
* Add it to the Kernel so it's available without having to include things. This will require ruby 2.0 and I'm not prepared to kill backwards compatability yet.
|
data/lib/defmatch/version.rb
CHANGED
data/lib/defmatch.rb
CHANGED
@@ -26,6 +26,19 @@ module Defmatch
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
def self.dispatch_clone(dispatch)
|
30
|
+
dispatch.keys.inject({}) {|nd,key| nd[key] = dispatch[key].clone; nd } if dispatch
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.inherited(klass,subklass)
|
34
|
+
subklass.instance_variable_set(:@defmatch_dispatch_info,Defmatch.dispatch_clone(klass.instance_variable_get(:@defmatch_dispatch_info)))
|
35
|
+
subklass.instance_variable_set(:@defclassmatch_dispatch_info,Defmatch.dispatch_clone(klass.instance_variable_get(:@defclassmatch_dispatch_info)))
|
36
|
+
end
|
37
|
+
|
38
|
+
def inherited(subklass)
|
39
|
+
Defmatch.inherited(self,subklass)
|
40
|
+
end
|
41
|
+
|
29
42
|
def defmatch(method,*args,&block)
|
30
43
|
@defmatch_dispatch_info ||= {} # setup the methods in an instance variable
|
31
44
|
@defmatch_dispatch_info[method] ||= [] # setup the ordered array for the method the first time
|
data/spec/defmatch_spec.rb
CHANGED
@@ -18,6 +18,38 @@ describe Defmatch do
|
|
18
18
|
defclassmatch(:cscope) { self }
|
19
19
|
end
|
20
20
|
|
21
|
+
class ATester < Tester
|
22
|
+
defmatch(:times,:not_in_super) { "not in super" }
|
23
|
+
end
|
24
|
+
|
25
|
+
class InheritedTesterBroken
|
26
|
+
extend(Defmatch)
|
27
|
+
|
28
|
+
defmatch(:times,Fixnum) {|num| num * 2 }
|
29
|
+
|
30
|
+
def inherited(subclass)
|
31
|
+
:do_nothing
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
class InheritedTesterWorkaround
|
37
|
+
extend(Defmatch)
|
38
|
+
|
39
|
+
defclassmatch(:times,Fixnum) {|num| num * 2 }
|
40
|
+
|
41
|
+
def inherited(subclass)
|
42
|
+
Defmatch.inherited(self,subclass)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
class InheritedTesterBrokenA < InheritedTesterBroken
|
48
|
+
end
|
49
|
+
|
50
|
+
class InheritedTesterWorkaroundA < InheritedTesterWorkaround
|
51
|
+
end
|
52
|
+
|
21
53
|
it 'should create methods' do
|
22
54
|
expect(Tester).to respond_to(:defmatch)
|
23
55
|
expect(Tester).to respond_to(:defclassmatch)
|
@@ -39,6 +71,20 @@ describe Defmatch do
|
|
39
71
|
expect(Tester.cscope).to equal(Tester)
|
40
72
|
end
|
41
73
|
|
74
|
+
it 'should allow for inheritance' do
|
75
|
+
expect(ATester.cscope).to equal(ATester)
|
76
|
+
x = Tester.new
|
77
|
+
y = ATester.new
|
78
|
+
expect(y.scope).to equal(y)
|
79
|
+
expect(y.times(:not_in_super)).to eq("not in super")
|
80
|
+
expect { x.times(:not_in_super) }.to raise_error(ArgumentError)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should allow for inherited workaround' do
|
84
|
+
expect(InheritedTesterWorkaroundA.times(2)).to eq(4)
|
85
|
+
expect { InheritedTesterBrokenA.times(2) }.to raise_error(NoMethodError)
|
86
|
+
end
|
87
|
+
|
42
88
|
instance = Tester.new
|
43
89
|
|
44
90
|
describe instance do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: defmatch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Warnock
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|