step_rewrite 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,5 +1,4 @@
1
1
  source :gemcutter
2
- source 'http://gems.github.com'
3
2
 
4
3
  # Specify your gem's dependencies in step_rewrite.gemspec
5
4
  gemspec
@@ -8,3 +8,4 @@ $:.unshift(File.dirname(__FILE__)) unless
8
8
 
9
9
  require "step_rewrite/sexp_utilities"
10
10
  require "step_rewrite/rewriter"
11
+ require 'step_rewrite/step_rewrite'
@@ -0,0 +1,8 @@
1
+ require 'step_rewrite'
2
+
3
+ class Module
4
+ def define_step_method(name, *args, &block)
5
+ rewritten = StepRewrite.rewrite(*args, &block)
6
+ instance_eval "define_method(#{name.inspect}, &#{rewritten})"
7
+ end
8
+ end
@@ -56,6 +56,4 @@ module StepRewrite
56
56
  return :attrasgn if exp.first == :attrasgn && special_call?(exp[3][1])
57
57
  end
58
58
  end
59
-
60
-
61
59
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module StepRewrite
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -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('../../lib/step_rewrite', __FILE__)
2
- require File.expand_path('../convert_matcher', __FILE__)
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
- }.to {
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
+
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','lib','step_rewrite'))
2
+ Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
3
+
4
+
5
+
6
+
7
+
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: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
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-16 00:00:00 +05:30
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/convert_matcher.rb
129
- - spec/step_rewrite_spec.rb
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