step_rewrite 0.0.1 → 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.
- data/Gemfile +0 -1
- data/lib/step_rewrite.rb +1 -0
- data/lib/step_rewrite/module.rb +8 -0
- data/lib/step_rewrite/rewriter.rb +0 -2
- data/lib/step_rewrite/step_rewrite.rb +19 -0
- data/lib/step_rewrite/version.rb +1 -1
- data/spec/lib/module_spec.rb +39 -0
- data/spec/{step_rewrite_spec.rb → lib/rewriter_spec.rb} +16 -3
- data/spec/lib/step_rewrite_spec.rb +45 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/{convert_matcher.rb → support/convert_matcher.rb} +0 -0
- metadata +11 -6
data/Gemfile
CHANGED
data/lib/step_rewrite.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
module StepRewrite
|
4
|
+
include Forwardable
|
5
|
+
|
6
|
+
def step(*args, &block)
|
7
|
+
StepRewrite.step(*args, &block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.rewrite(callback_symbol = :_, &block)
|
11
|
+
old_sexp = Sexp.from_array(block.to_sexp)
|
12
|
+
new_sexp = StepRewrite::Rewriter.new(callback_symbol).process(old_sexp)
|
13
|
+
Ruby2Ruby.new.process(new_sexp)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.step(*args, &block)
|
17
|
+
eval(rewrite(*args, &block), block.binding).call
|
18
|
+
end
|
19
|
+
end
|
data/lib/step_rewrite/version.rb
CHANGED
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.expand_path('../../spec_helper.rb', __FILE__)
|
2
|
+
require 'step_rewrite/module'
|
3
|
+
require 'rspec'
|
4
|
+
|
5
|
+
describe Module do
|
6
|
+
class Foo
|
7
|
+
def call_adder(a)
|
8
|
+
yield a
|
9
|
+
end
|
10
|
+
|
11
|
+
def plus_five(b)
|
12
|
+
yield b + 5
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'defines methods where the special callback symbol is rewritten' do
|
17
|
+
class Foo
|
18
|
+
define_step_method :into_itself_plus_five do |a|
|
19
|
+
res = call_adder(a, &_)
|
20
|
+
res_2 = plus_five(res, &_)
|
21
|
+
res_2*res
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
Foo.new.into_itself_plus_five(5).should == 50
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'defines methods allowing configuration of the special callback symbol' do
|
29
|
+
class Foo
|
30
|
+
define_step_method :into_itself_plus_five, :cb do |a|
|
31
|
+
res = call_adder(a, &cb)
|
32
|
+
_ = lambda { |res_2| res_2 * res }
|
33
|
+
plus_five(res, &_)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
Foo.new.into_itself_plus_five(5).should == 50
|
38
|
+
end
|
39
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
require File.expand_path('../../
|
2
|
-
require
|
1
|
+
require File.expand_path('../../spec_helper.rb', __FILE__)
|
2
|
+
require 'rspec'
|
3
3
|
|
4
4
|
describe StepRewrite::Rewriter do
|
5
5
|
describe "#process" do
|
@@ -146,7 +146,7 @@ describe StepRewrite::Rewriter do
|
|
146
146
|
should convert {
|
147
147
|
a, b, c = foo(&_)
|
148
148
|
bar(a, b)
|
149
|
-
|
149
|
+
}.to {
|
150
150
|
foo { |a, b, c| bar(a, b) }
|
151
151
|
}
|
152
152
|
end
|
@@ -186,6 +186,19 @@ describe StepRewrite::Rewriter do
|
|
186
186
|
foo { |a.b.c| bar(a.b) }
|
187
187
|
}
|
188
188
|
end
|
189
|
+
|
190
|
+
it do
|
191
|
+
should convert {
|
192
|
+
foo(bar.map { |v| v*2 }, &_)
|
193
|
+
bar { baz }
|
194
|
+
qux(&grault)
|
195
|
+
}.to {
|
196
|
+
foo(bar.map { |v| v*2 }) do
|
197
|
+
bar { baz }
|
198
|
+
qux(&grault)
|
199
|
+
end
|
200
|
+
}
|
201
|
+
end
|
189
202
|
end
|
190
203
|
end
|
191
204
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path('../../spec_helper.rb', __FILE__)
|
2
|
+
require 'rspec'
|
3
|
+
|
4
|
+
describe StepRewrite do
|
5
|
+
include StepRewrite
|
6
|
+
|
7
|
+
def call_adder(a)
|
8
|
+
yield a
|
9
|
+
end
|
10
|
+
|
11
|
+
def plus_five(b)
|
12
|
+
yield b + 5
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'evaluates blocks after rewriting them around the special symbol _' do
|
16
|
+
step do
|
17
|
+
into_two = lambda {|x| x*2}
|
18
|
+
res = call_adder(5, &_)
|
19
|
+
plus_five(res, &into_two)
|
20
|
+
end.should == 20
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'rewrites blocks using a configurable special symbol instead of _ if one is passed' do
|
24
|
+
step(:cb) do
|
25
|
+
_ = lambda {|x| x*2}
|
26
|
+
res = call_adder(5, &cb)
|
27
|
+
plus_five(res, &_)
|
28
|
+
end.should == 20
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'allows you to rewrite blocks without including the module' do
|
32
|
+
StepRewrite.step do
|
33
|
+
into_two = lambda {|x| x*2}
|
34
|
+
res = call_adder(5, &_)
|
35
|
+
plus_five(res, &into_two)
|
36
|
+
end.should == 20
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'gives you the rewritten code' do
|
40
|
+
StepRewrite.rewrite do
|
41
|
+
foo(&_); bar
|
42
|
+
end.should == "proc { foo { bar } }"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
data/spec/spec_helper.rb
ADDED
File without changes
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: step_rewrite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 0
|
9
8
|
- 1
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Vishnu S Iyengar
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-18 00:00:00 +05:30
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -122,11 +122,16 @@ files:
|
|
122
122
|
- Rakefile
|
123
123
|
- console
|
124
124
|
- lib/step_rewrite.rb
|
125
|
+
- lib/step_rewrite/module.rb
|
125
126
|
- lib/step_rewrite/rewriter.rb
|
126
127
|
- lib/step_rewrite/sexp_utilities.rb
|
128
|
+
- lib/step_rewrite/step_rewrite.rb
|
127
129
|
- lib/step_rewrite/version.rb
|
128
|
-
- spec/
|
129
|
-
- spec/
|
130
|
+
- spec/lib/module_spec.rb
|
131
|
+
- spec/lib/rewriter_spec.rb
|
132
|
+
- spec/lib/step_rewrite_spec.rb
|
133
|
+
- spec/spec_helper.rb
|
134
|
+
- spec/support/convert_matcher.rb
|
130
135
|
- step_rewrite.gemspec
|
131
136
|
has_rdoc: true
|
132
137
|
homepage: http://rubygems.org/gems/step_rewrite
|