patternmatching 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +44 -7
- data/Rakefile +1 -1
- data/examples/match_inside_class.rb +11 -4
- data/lib/patternmatching/pattern.rb +32 -19
- data/lib/patternmatching/version.rb +2 -2
- data/spec/patternmatching_spec.rb +12 -5
- data/website/index.html +43 -5
- data/website/index.txt +43 -4
- metadata +1 -1
data/README.txt
CHANGED
@@ -23,9 +23,7 @@ Note: Default equivalence used in structured pattern matching is
|
|
23
23
|
based on "pattern === data",
|
24
24
|
so "foo(Numeric)" matches "foo(100)".
|
25
25
|
|
26
|
-
|
27
|
-
Need the receiver object(NOT an argument) calling pattern matching
|
28
|
-
synchronized when multi-threaded access.
|
26
|
+
From ver. 0.2.0, thread unsafeness restriction is removed.
|
29
27
|
|
30
28
|
== Demonstration of usage
|
31
29
|
|
@@ -142,6 +140,45 @@ to the following block when the pattern matched.
|
|
142
140
|
end
|
143
141
|
end # => "He is Jiro"
|
144
142
|
|
143
|
+
=== Field Access Example
|
144
|
+
It is ver. 0.2.0 API
|
145
|
+
|
146
|
+
require "patternmatching"
|
147
|
+
|
148
|
+
include PatternMatching
|
149
|
+
|
150
|
+
class Foo
|
151
|
+
def initialize
|
152
|
+
@name = "Foo"
|
153
|
+
end
|
154
|
+
attr :name
|
155
|
+
def foo
|
156
|
+
end
|
157
|
+
|
158
|
+
def bar
|
159
|
+
make "bar" do
|
160
|
+
seems as {:val} do
|
161
|
+
foo
|
162
|
+
# To access fields like this.name or this.name = ...
|
163
|
+
the.name = val
|
164
|
+
# To access self as this
|
165
|
+
this
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
func(:buzz).seems as {:val} do
|
171
|
+
the.name = val
|
172
|
+
this
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
o = Foo.new
|
177
|
+
p o.bar == o #=> true
|
178
|
+
p o.name #=> "bar"
|
179
|
+
p o.buzz("buzz") == o #=> true
|
180
|
+
p o.name #=> "buzz"
|
181
|
+
|
145
182
|
== Forum
|
146
183
|
|
147
184
|
Visit the project forum in RubyForge.
|
@@ -158,11 +195,11 @@ for anonymous access.
|
|
158
195
|
|
159
196
|
This code is free to use under the terms of the MIT license.
|
160
197
|
|
161
|
-
==
|
198
|
+
== Links
|
162
199
|
|
163
|
-
* Web Site
|
164
|
-
* Project Page
|
165
|
-
* API Doc
|
200
|
+
* {Web Site}[http://patternmatching.rubyforge.org/]
|
201
|
+
* {Project Page}[http://rubyforge.org/projects/patternmatching/]
|
202
|
+
* {API Doc}[http://patternmatching.rubyforge.org/rdoc/]
|
166
203
|
|
167
204
|
== Contact
|
168
205
|
|
data/Rakefile
CHANGED
@@ -107,7 +107,7 @@ end
|
|
107
107
|
|
108
108
|
desc 'Generate and upload website files'
|
109
109
|
# task :website => [:website_generate]
|
110
|
-
task :website => [:website_generate, :website_upload]
|
110
|
+
task :website => [:website_generate, :website_upload, :publish_docs]
|
111
111
|
|
112
112
|
desc 'Release the website and new gem version'
|
113
113
|
task :deploy => [:check_version, :website, :release] do
|
@@ -7,22 +7,29 @@ class Foo
|
|
7
7
|
@name = "Foo"
|
8
8
|
end
|
9
9
|
attr :name
|
10
|
+
def foo
|
11
|
+
end
|
10
12
|
|
11
13
|
def bar
|
12
14
|
make "bar" do
|
13
15
|
seems as {:val} do
|
14
|
-
|
16
|
+
foo
|
17
|
+
# To access fields like this.name or this.name = ...
|
18
|
+
the.name = val
|
19
|
+
# To access self as this
|
20
|
+
this
|
15
21
|
end
|
16
22
|
end
|
17
23
|
end
|
18
24
|
|
19
25
|
func(:buzz).seems as {:val} do
|
20
|
-
|
26
|
+
the.name = val
|
27
|
+
this
|
21
28
|
end
|
22
29
|
end
|
23
30
|
|
24
31
|
o = Foo.new
|
25
|
-
o.bar
|
32
|
+
p o.bar == o #=> true
|
26
33
|
p o.name #=> "bar"
|
27
|
-
o.buzz("buzz")
|
34
|
+
p o.buzz("buzz") == o #=> true
|
28
35
|
p o.name #=> "buzz"
|
@@ -117,34 +117,47 @@ module PatternMatching
|
|
117
117
|
rescue NotMatched
|
118
118
|
next
|
119
119
|
end
|
120
|
-
return ExecuteAs.new(args, receiver).
|
120
|
+
return ExecuteAs.new(args, receiver).instance_eval(&action)
|
121
121
|
end
|
122
122
|
end
|
123
123
|
|
124
|
+
# Private class to access instance valiables of the receiver
|
125
|
+
class InstanceVariableAccessor
|
126
|
+
def initialize(receiver)
|
127
|
+
@receiver = receiver
|
128
|
+
end
|
129
|
+
def method_missing(name, *args)
|
130
|
+
begin
|
131
|
+
@receiver.send(name, *args)
|
132
|
+
rescue NameError
|
133
|
+
if name.to_s[-1,1] == "="
|
134
|
+
field = "@" + name.to_s[0...-1]
|
135
|
+
@receiver.instance_variable_set(field, args[0])
|
136
|
+
else
|
137
|
+
field = "@" + name.to_s
|
138
|
+
@receiver.instance_variable_get(field, args[0])
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
124
144
|
#Private class enabling to use the name of symbols in patterns
|
125
145
|
#like a local variables in action blocks
|
126
146
|
class ExecuteAs
|
127
147
|
def initialize(args, receiver)
|
128
|
-
@
|
148
|
+
@receiver = receiver
|
149
|
+
@wrapper = InstanceVariableAccessor.new receiver
|
129
150
|
@args = args
|
130
151
|
end
|
131
|
-
def
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
# this block is not thread safe
|
141
|
-
@this.extend mod
|
142
|
-
result = @this.instance_eval(&action)
|
143
|
-
mod.instance_eval do
|
144
|
-
remove_method(:method_missing)
|
145
|
-
end
|
146
|
-
#
|
147
|
-
result
|
152
|
+
def this
|
153
|
+
@receiver
|
154
|
+
end
|
155
|
+
def the
|
156
|
+
@wrapper
|
157
|
+
end
|
158
|
+
def method_missing(name, *args)
|
159
|
+
return @args[name] if @args.key?(name)
|
160
|
+
@receiver.send(name, *args)
|
148
161
|
end
|
149
162
|
end
|
150
163
|
end
|
@@ -48,17 +48,24 @@ class PartialStyleDefs
|
|
48
48
|
@name = "Foo"
|
49
49
|
end
|
50
50
|
attr :name
|
51
|
-
|
51
|
+
def foo
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
52
55
|
def bar
|
53
56
|
make "bar" do
|
54
57
|
seems as {:val} do
|
55
|
-
|
58
|
+
foo
|
59
|
+
the.name = val
|
60
|
+
this
|
56
61
|
end
|
57
62
|
end
|
58
63
|
end
|
59
64
|
|
60
65
|
func(:buzz).seems as {:val} do
|
61
|
-
|
66
|
+
foo
|
67
|
+
the.name = val
|
68
|
+
this
|
62
69
|
end
|
63
70
|
end
|
64
71
|
|
@@ -183,9 +190,9 @@ describe "PatternMatching from Example" do
|
|
183
190
|
it "should update fields from block" do
|
184
191
|
o = PartialStyleDefs.new
|
185
192
|
o.name.should == "Foo"
|
186
|
-
o.bar
|
193
|
+
o.bar.should == o
|
187
194
|
o.name.should == "bar"
|
188
|
-
o.buzz("buzz")
|
195
|
+
o.buzz("buzz").should == o
|
189
196
|
o.name.should == "buzz"
|
190
197
|
end
|
191
198
|
end
|
data/website/index.html
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
<h1>PatternMatching module</h1>
|
34
34
|
<div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/patternmatching"; return false'>
|
35
35
|
Get Version
|
36
|
-
<a href="http://rubyforge.org/projects/patternmatching" class="numbers">0.
|
36
|
+
<a href="http://rubyforge.org/projects/patternmatching" class="numbers">0.2.0</a>
|
37
37
|
</div>
|
38
38
|
<h1>→ ‘patternmatching’</h1>
|
39
39
|
|
@@ -71,9 +71,7 @@ based on “<code>pattern === data</code>”,
|
|
71
71
|
so “<code>foo(Numeric)</code>” matches “<code>foo(100)</code>”.</p>
|
72
72
|
|
73
73
|
|
74
|
-
<p>
|
75
|
-
Need the receiver object(NOT an argument) calling pattern matching
|
76
|
-
synchronized when multi-threaded access.</p>
|
74
|
+
<p>From ver. 0.2.0, thread unsafeness restriction is removed.</p>
|
77
75
|
|
78
76
|
|
79
77
|
<h2>Demonstration of usage</h2>
|
@@ -197,6 +195,46 @@ make person do
|
|
197
195
|
puts "no name"
|
198
196
|
end
|
199
197
|
end # => "He is Jiro"
|
198
|
+
</pre>
|
199
|
+
|
200
|
+
<h3>Field Access Example</h3>
|
201
|
+
It is ver 0.2.0 <span class="caps">API</span>
|
202
|
+
<pre>
|
203
|
+
require "patternmatching"
|
204
|
+
|
205
|
+
include PatternMatching
|
206
|
+
|
207
|
+
class Foo
|
208
|
+
def initialize
|
209
|
+
@name = "Foo"
|
210
|
+
end
|
211
|
+
attr :name
|
212
|
+
def foo
|
213
|
+
end
|
214
|
+
|
215
|
+
def bar
|
216
|
+
make "bar" do
|
217
|
+
seems as {:val} do
|
218
|
+
foo
|
219
|
+
# To access fields like this.name or this.name = ...
|
220
|
+
the.name = val
|
221
|
+
# To access self as this
|
222
|
+
this
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
func(:buzz).seems as {:val} do
|
228
|
+
the.name = val
|
229
|
+
this
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
o = Foo.new
|
234
|
+
p o.bar == o #=> true
|
235
|
+
p o.name #=> "bar"
|
236
|
+
p o.buzz("buzz") == o #=> true
|
237
|
+
p o.name #=> "buzz"
|
200
238
|
</pre>
|
201
239
|
|
202
240
|
<h2>Forum</h2>
|
@@ -220,7 +258,7 @@ end # => "He is Jiro"
|
|
220
258
|
<p>This code is free to use under the terms of the <span class="caps">MIT</span> license.</p>
|
221
259
|
|
222
260
|
|
223
|
-
<h2>
|
261
|
+
<h2>Links</h2>
|
224
262
|
|
225
263
|
|
226
264
|
<ul>
|
data/website/index.txt
CHANGED
@@ -24,9 +24,7 @@ Note: Default equivalence used in structured pattern matching is
|
|
24
24
|
based on "<code>pattern === data</code>",
|
25
25
|
so "<code>foo(Numeric)</code>" matches "<code>foo(100)</code>".
|
26
26
|
|
27
|
-
|
28
|
-
Need the receiver object(NOT an argument) calling pattern matching
|
29
|
-
synchronized when multi-threaded access.
|
27
|
+
From ver. 0.2.0, thread unsafeness restriction is removed.
|
30
28
|
|
31
29
|
h2. Demonstration of usage
|
32
30
|
|
@@ -149,6 +147,47 @@ make person do
|
|
149
147
|
end # => "He is Jiro"
|
150
148
|
</pre>
|
151
149
|
|
150
|
+
<h3>Field Access Example</h3>
|
151
|
+
It is ver 0.2.0 API
|
152
|
+
<pre>
|
153
|
+
require "patternmatching"
|
154
|
+
|
155
|
+
include PatternMatching
|
156
|
+
|
157
|
+
class Foo
|
158
|
+
def initialize
|
159
|
+
@name = "Foo"
|
160
|
+
end
|
161
|
+
attr :name
|
162
|
+
def foo
|
163
|
+
end
|
164
|
+
|
165
|
+
def bar
|
166
|
+
make "bar" do
|
167
|
+
seems as {:val} do
|
168
|
+
foo
|
169
|
+
# To access fields like this.name or this.name = ...
|
170
|
+
the.name = val
|
171
|
+
# To access self as this
|
172
|
+
this
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
func(:buzz).seems as {:val} do
|
178
|
+
the.name = val
|
179
|
+
this
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
o = Foo.new
|
184
|
+
p o.bar == o #=> true
|
185
|
+
p o.name #=> "bar"
|
186
|
+
p o.buzz("buzz") == o #=> true
|
187
|
+
p o.name #=> "buzz"
|
188
|
+
</pre>
|
189
|
+
|
190
|
+
|
152
191
|
h2. Forum
|
153
192
|
|
154
193
|
Visit RubyForge project forum.
|
@@ -163,7 +202,7 @@ h2. License
|
|
163
202
|
|
164
203
|
This code is free to use under the terms of the MIT license.
|
165
204
|
|
166
|
-
h2.
|
205
|
+
h2. Links
|
167
206
|
|
168
207
|
* "Web Site":http://patternmatching.rubyforge.org/
|
169
208
|
* "Project Page":http://rubyforge.org/projects/patternmatching/
|
metadata
CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: patternmatching
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
6
|
+
version: 0.2.0
|
7
7
|
date: 2007-06-05 00:00:00 +09:00
|
8
8
|
summary: Provide a pure ruby module that can build structured objects easily, can enable pattern match of objects, and can define method as a partial function style.
|
9
9
|
require_paths:
|