synvert 0.0.4 → 0.0.5

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: a16d51a6f5e6376390a8d50951f1adaca53f6737
4
- data.tar.gz: 295744d184ca5bbea1cf1faa0fef5428aeb8a402
3
+ metadata.gz: b61c84cfe6968ed5efdacf7d2eecd4c3e5299e53
4
+ data.tar.gz: a0893a9ba00e2266a9b00a598f02bac263931005
5
5
  SHA512:
6
- metadata.gz: 259cbde5e5e77897abcf7f7c3c0557f9ecfe13ce4ba0638035dbf96147c536465818044cdaa10cc648bd487bf54806aca1c8d1208a358c6a4d530ef8c56892c8
7
- data.tar.gz: 52fc74d210b5236e2cccb196d460a485421c99f47d7d825b86d86edb4f6813195cea04b5bd1b5a3ae8ecd2dd6e59fb9250aef407266b12ed0952968d2be8affd
6
+ metadata.gz: c7d1e4f23d7c5492d216f0034c839c0ef1420773f5c83ecebc802b826ca82eeedcea8a1c06031b24d959375c30038c0c290a56532d5d0e03d005c44d4f282f43
7
+ data.tar.gz: f88a50587f6e0807ea8ea8ee5c5e80b066d0c85ebb51df4f13c06ab4b4c6e56338f6a25e6626096d2158ed05f21fdb4a9dcdddf75d60eb96b387c0e23354f194
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.0.5
4
+
5
+ * Add add_snippet dsl for reuse
6
+ * Abstract strong_parameters and convert_dynamic_finders snippet
7
+
3
8
  ## 0.0.4
4
9
 
5
10
  * Test snippets
data/README.md CHANGED
@@ -28,6 +28,24 @@ Currently it supports
28
28
  * convert to FactoryGirl short syntax
29
29
  * upgrade rails from 3.2.x to 4.0.0
30
30
 
31
+ ## Example
32
+
33
+ ```ruby
34
+ Synvert::Rewriter.new "factory_girl_short_syntax", "FactoryGirl uses short syntax" do
35
+ within_file 'sepc/**/*.rb' do
36
+ with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
37
+ replace_with "create({{arguments}})"
38
+ end
39
+ end
40
+ end
41
+ ```
42
+
43
+ This snippet will convert `post = FactoryGirl.create(:post)` to `post =
44
+ create(:post)`.
45
+
46
+ There are more examples [here][1], I will write down the Documents
47
+ later.
48
+
31
49
  ## Contributing
32
50
 
33
51
  1. Fork it
@@ -35,3 +53,6 @@ Currently it supports
35
53
  3. Commit your changes (`git commit -am 'Add some feature'`)
36
54
  4. Push to the branch (`git push origin my-new-feature`)
37
55
  5. Create new Pull Request
56
+
57
+
58
+ [1]: https://github.com/xinminlabs/synvert/tree/master/lib/synvert/snippets
data/lib/synvert/cli.rb CHANGED
@@ -15,9 +15,10 @@ module Synvert
15
15
  paths = optparse.parse(args)
16
16
  Configuration.instance.set :path, paths.first || Dir.pwd
17
17
 
18
- Dir.glob(File.join(File.dirname(__FILE__), 'snippets/**/*.rb')).each do |file|
19
- eval(File.read(file)).process
18
+ rewriters = Dir.glob(File.join(File.dirname(__FILE__), 'snippets/**/*.rb')).map do |file|
19
+ eval(File.read(file))
20
20
  end
21
+ rewriters.map(&:process)
21
22
  end
22
23
  end
23
24
  end
@@ -125,7 +125,7 @@ private
125
125
  actual.to_sym == expected
126
126
  when String
127
127
  if Parser::AST::Node === actual
128
- actual.source(instance) == expected
128
+ actual.source(instance) == expected || actual.source(instance) == ':' + expected
129
129
  else
130
130
  actual.to_s == expected
131
131
  end
@@ -13,6 +13,16 @@ module Synvert
13
13
  end
14
14
  end
15
15
 
16
+ class Rewriter::IfExistCondition < Rewriter::Condition
17
+ def match?
18
+ match = false
19
+ @instance.current_node.recursive_children do |child_node|
20
+ match = match || (child_node && child_node.match?(@instance, @options))
21
+ end
22
+ match
23
+ end
24
+ end
25
+
16
26
  class Rewriter::UnlessExistCondition < Rewriter::Condition
17
27
  def match?
18
28
  match = false
@@ -4,8 +4,7 @@ module Synvert
4
4
  class Rewriter::Instance
5
5
  attr_accessor :current_node, :current_source, :current_file
6
6
 
7
- def initialize(rewriter, file_pattern, &block)
8
- @rewriter = rewriter
7
+ def initialize(file_pattern, &block)
9
8
  @actions = []
10
9
  @file_pattern = file_pattern
11
10
  @block = block
@@ -48,6 +47,10 @@ module Synvert
48
47
 
49
48
  alias with_node within_node
50
49
 
50
+ def if_exist_node(options, &block)
51
+ Rewriter::IfExistCondition.new(self, options, &block).process
52
+ end
53
+
51
54
  def unless_exist_node(options, &block)
52
55
  Rewriter::UnlessExistCondition.new(self, options, &block).process
53
56
  end
@@ -76,14 +79,6 @@ module Synvert
76
79
  @actions << Rewriter::RemoveAction.new(self)
77
80
  end
78
81
 
79
- def assign(name, key, value)
80
- @rewriter.set name, key, value
81
- end
82
-
83
- def fetch(name, key)
84
- @rewriter.get name, key
85
- end
86
-
87
82
  private
88
83
 
89
84
  def remove_code_or_whole_line(source, line)
@@ -12,26 +12,32 @@ module Synvert
12
12
  autoload :Scope, 'synvert/rewriter/scope'
13
13
 
14
14
  autoload :Condition, 'synvert/rewriter/condition'
15
+ autoload :IfExistCondition, 'synvert/rewriter/condition'
15
16
  autoload :UnlessExistCondition, 'synvert/rewriter/condition'
16
17
  autoload :IfOnlyExistCondition, 'synvert/rewriter/condition'
17
18
 
18
19
  autoload :GemSpec, 'synvert/rewriter/gem_spec'
19
20
 
20
- attr_reader :description
21
-
22
- def initialize(description, &block)
23
- @description = description
24
- @block = block
25
- @assignments = {}
26
- end
21
+ class <<self
22
+ def register(name, rewriter)
23
+ @rewriters ||= {}
24
+ @rewriters[name.to_s] = rewriter
25
+ end
27
26
 
28
- def set(name, key, value)
29
- @assignments[name] ||= {}
30
- @assignments[name][key] = value
27
+ def call(name)
28
+ if @rewriters[name.to_s]
29
+ @rewriters[name.to_s].process
30
+ else
31
+ raise RewriterNotFound.new "Rewriter #{name} not found"
32
+ end
33
+ end
31
34
  end
32
35
 
33
- def get(name, key)
34
- @assignments[name] and @assignments[name][key]
36
+ def initialize(name, description, &block)
37
+ @name = name
38
+ @description = description
39
+ @block = block
40
+ self.class.register(name, self)
35
41
  end
36
42
 
37
43
  def process
@@ -43,11 +49,15 @@ module Synvert
43
49
  end
44
50
 
45
51
  def within_file(file_pattern, &block)
46
- if @gem_spec.match?
47
- Rewriter::Instance.new(self, file_pattern, &block).process
52
+ if !@gem_spec || @gem_spec.match?
53
+ Rewriter::Instance.new(file_pattern, &block).process
48
54
  end
49
55
  end
50
56
 
51
57
  alias within_files within_file
58
+
59
+ def add_snippet(name)
60
+ self.class.call(name)
61
+ end
52
62
  end
53
63
  end
@@ -0,0 +1,4 @@
1
+ module Synvert
2
+ class RewriterNotFound < Exception
3
+ end
4
+ end
@@ -1,4 +1,4 @@
1
- Synvert::Rewriter.new "FactoryGirl uses short syntax" do
1
+ Synvert::Rewriter.new "factory_girl_short_syntax", "FactoryGirl uses short syntax" do
2
2
  gem_spec 'factory_girl', '2.0.0'
3
3
 
4
4
  # insert include FactoryGirl::Syntax::Methods
@@ -0,0 +1,60 @@
1
+ require 'securerandom'
2
+
3
+ class Synvert::Rewriter::Instance
4
+ def dynamic_finder_to_hash(node, prefix)
5
+ fields = node.message.to_s[prefix.length..-1].split("_and_")
6
+ fields.length.times.map { |i|
7
+ fields[i] + ": " + node.arguments[i].source(self)
8
+ }.join(", ")
9
+ end
10
+ end
11
+
12
+ Synvert::Rewriter.new "convert_dynamic_finders", "Convert dynamic finders" do
13
+ within_files '**/*.rb' do
14
+ # find_all_by_... => where(...)
15
+ with_node type: 'send', message: /find_all_by_(.*)/ do
16
+ hash_params = dynamic_finder_to_hash(node, "find_all_by_")
17
+ replace_with "{{receiver}}.where(#{hash_params})"
18
+ end
19
+ end
20
+
21
+ within_files '**/*.rb' do
22
+ # find_by_... => where(...).first
23
+ with_node type: 'send', message: /find_by_(.*)/ do
24
+ hash_params = dynamic_finder_to_hash(node, "find_by_")
25
+ replace_with "{{receiver}}.where(#{hash_params}).first"
26
+ end
27
+ end
28
+
29
+ within_files '**/*.rb' do
30
+ # find_last_by_... => where(...).last
31
+ with_node type: 'send', message: /find_last_by_(.*)/ do
32
+ hash_params = dynamic_finder_to_hash(node, "find_last_by_")
33
+ replace_with "{{receiver}}.where(#{hash_params}).last"
34
+ end
35
+ end
36
+
37
+ within_files '**/*.rb' do
38
+ # scoped_by_... => where(...)
39
+ with_node type: 'send', message: /scoped_by_(.*)/ do
40
+ hash_params = dynamic_finder_to_hash(node, "scoped_by_")
41
+ replace_with "{{receiver}}.where(#{hash_params})"
42
+ end
43
+ end
44
+
45
+ within_files '**/*.rb' do
46
+ # find_or_initialize_by_... => find_or_initialize_by(...)
47
+ with_node type: 'send', message: /find_or_initialize_by_(.*)/ do
48
+ hash_params = dynamic_finder_to_hash(node, "find_or_initialize_by_")
49
+ replace_with "{{receiver}}.find_or_initialize_by(#{hash_params})"
50
+ end
51
+ end
52
+
53
+ within_files '**/*.rb' do
54
+ # find_or_create_by_... => find_or_create_by(...)
55
+ with_node type: 'send', message: /find_or_create_by_(.*)/ do
56
+ hash_params = dynamic_finder_to_hash(node, "find_or_create_by_")
57
+ replace_with "{{receiver}}.find_or_create_by(#{hash_params})"
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,44 @@
1
+ Synvert::Rewriter.new "strong_parameters", "use strong_parameters syntax" do
2
+ within_files 'config/**/*.rb' do
3
+ # remove config.active_record.whitelist_attributes = ...
4
+ with_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'active_record'}, message: 'whitelist_attributes=' do
5
+ remove
6
+ end
7
+ end
8
+
9
+ parameters = {}
10
+ within_files 'app/models/**/*.rb' do
11
+ # assign and remove attr_accessible ...
12
+ within_node type: 'class' do
13
+ object_name = node.name.source(self).underscore
14
+ with_node type: 'send', message: 'attr_accessible' do
15
+ parameters[object_name] = node.arguments.map { |key| key.source(self) }.join(', ')
16
+ remove
17
+ end
18
+ end
19
+ end
20
+
21
+ within_file 'app/controllers/**/*.rb' do
22
+ within_node type: 'class' do
23
+ object_name = node.name.source(self).sub('Controller', '').singularize.underscore
24
+ if_exist_node type: 'send', receiver: 'params', message: '[]', arguments: [object_name] do
25
+ if parameters[object_name]
26
+ # append def xxx_params; ...; end
27
+ unless_exist_node type: 'def', name: "#{object_name}_params" do
28
+ append """def #{object_name}_params
29
+ params.require(:#{object_name}).permit(#{parameters[object_name]})
30
+ end"""
31
+ end
32
+
33
+ # params[:xxx] => xxx_params
34
+ with_node type: 'send', receiver: 'params', message: '[]' do
35
+ object_name = eval(node.arguments.first.source(self)).to_s
36
+ if parameters[object_name]
37
+ replace_with "#{object_name}_params"
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,15 +1,6 @@
1
1
  require 'securerandom'
2
2
 
3
- class Synvert::Rewriter::Instance
4
- def dynamic_finder_to_hash(node, prefix)
5
- fields = node.message.to_s[prefix.length..-1].split("_and_")
6
- fields.length.times.map { |i|
7
- fields[i] + ": " + node.arguments[i].source(self)
8
- }.join(", ")
9
- end
10
- end
11
-
12
- Synvert::Rewriter.new "Upgrade rails from 3.2 to 4.0" do
3
+ Synvert::Rewriter.new "upgrade_rails_3_2_to_4_0", "Upgrade rails from 3.2 to 4.0" do
13
4
  gem_spec 'rails', '3.2.0'
14
5
 
15
6
  within_file 'config/application.rb' do
@@ -55,13 +46,6 @@ Synvert::Rewriter.new "Upgrade rails from 3.2 to 4.0" do
55
46
  end
56
47
  end
57
48
 
58
- within_files 'config/**/*.rb' do
59
- # remove config.active_record.whitelist_attributes = ...
60
- with_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'active_record'}, message: 'whitelist_attributes=' do
61
- remove
62
- end
63
- end
64
-
65
49
  within_file 'config/environments/production.rb' do
66
50
  # insert config.eager_load = true
67
51
  unless_exist_node type: 'send', message: 'eager_load=' do
@@ -132,54 +116,6 @@ Synvert::Rewriter.new "Upgrade rails from 3.2 to 4.0" do
132
116
  end
133
117
  end
134
118
 
135
- within_files 'app/**/*.rb' do
136
- # find_all_by_... => where(...)
137
- with_node type: 'send', message: /find_all_by_(.*)/ do
138
- hash_params = dynamic_finder_to_hash(node, "find_all_by_")
139
- replace_with "{{receiver}}.where(#{hash_params})"
140
- end
141
- end
142
-
143
- within_files 'app/**/*.rb' do
144
- # find_by_... => where(...).first
145
- with_node type: 'send', message: /find_by_(.*)/ do
146
- hash_params = dynamic_finder_to_hash(node, "find_by_")
147
- replace_with "{{receiver}}.where(#{hash_params}).first"
148
- end
149
- end
150
-
151
- within_files 'app/**/*.rb' do
152
- # find_last_by_... => where(...).last
153
- with_node type: 'send', message: /find_last_by_(.*)/ do
154
- hash_params = dynamic_finder_to_hash(node, "find_last_by_")
155
- replace_with "{{receiver}}.where(#{hash_params}).last"
156
- end
157
- end
158
-
159
- within_files 'app/**/*.rb' do
160
- # scoped_by_... => where(...)
161
- with_node type: 'send', message: /scoped_by_(.*)/ do
162
- hash_params = dynamic_finder_to_hash(node, "scoped_by_")
163
- replace_with "{{receiver}}.where(#{hash_params})"
164
- end
165
- end
166
-
167
- within_files 'app/**/*.rb' do
168
- # find_or_initialize_by_... => find_or_initialize_by(...)
169
- with_node type: 'send', message: /find_or_initialize_by_(.*)/ do
170
- hash_params = dynamic_finder_to_hash(node, "find_or_initialize_by_")
171
- replace_with "{{receiver}}.find_or_initialize_by(#{hash_params})"
172
- end
173
- end
174
-
175
- within_files 'app/**/*.rb' do
176
- # find_or_create_by_... => find_or_create_by(...)
177
- with_node type: 'send', message: /find_or_create_by_(.*)/ do
178
- hash_params = dynamic_finder_to_hash(node, "find_or_create_by_")
179
- replace_with "{{receiver}}.find_or_create_by(#{hash_params})"
180
- end
181
- end
182
-
183
119
  {'ActionController::Integration' => 'ActionDispatch::Integration',
184
120
  'ActionController::IntegrationTest' => 'ActionDispatch::IntegrationTest',
185
121
  'ActionController::PerformanceTest' => 'ActionDispatch::PerformanceTest',
@@ -195,39 +131,6 @@ Synvert::Rewriter.new "Upgrade rails from 3.2 to 4.0" do
195
131
  end
196
132
  end
197
133
 
198
- #####################
199
- # strong_parameters #
200
- #####################
201
- within_files 'app/models/**/*.rb' do
202
- # assign and remove attr_accessible ...
203
- within_node type: 'class' do
204
- class_node = node
205
- with_node type: 'send', message: 'attr_accessible' do
206
- assign 'parameters', class_node.name.source(self).underscore, node.arguments.map { |key| key.source(self) }.join(', ')
207
- remove
208
- end
209
- end
210
- end
211
-
212
- within_file 'app/controllers/**/*.rb' do
213
- within_node type: 'class' do
214
- # insert def xxx_params; ...; end
215
- object_name = node.name.source(self).sub('Controller', '').singularize.underscore
216
- if fetch('parameters', object_name)
217
- unless_exist_node type: 'def', name: "#{object_name}_params" do
218
- append """def #{object_name}_params
219
- params.require(:#{object_name}).permit(#{fetch 'parameters', object_name})
220
- end"""
221
- end
222
-
223
- # params[:xxx] => xxx_params
224
- with_node type: 'send', receiver: 'params', message: '[]' do
225
- object_name = eval(node.arguments.first.source(self)).to_s
226
- if fetch('parameters', object_name)
227
- replace_with "#{object_name}_params"
228
- end
229
- end
230
- end
231
- end
232
- end
134
+ add_snippet 'convert_dynamic_finders'
135
+ add_snippet 'strong_parameters'
233
136
  end
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
2
 
3
3
  module Synvert
4
- VERSION = "0.0.4"
4
+ VERSION = "0.0.5"
5
5
  end
data/lib/synvert.rb CHANGED
@@ -11,4 +11,5 @@ module Synvert
11
11
  autoload :CheckingVisitor, 'synvert/checking_visitor'
12
12
  autoload :Configuration, 'synvert/configuration'
13
13
  autoload :Rewriter, 'synvert/rewriter'
14
+ autoload :RewriterNotFound, 'synvert/rewriter_not_found'
14
15
  end
@@ -1,6 +1,39 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module Synvert
4
+ describe Rewriter::IfExistCondition do
5
+ let(:source) {
6
+ """
7
+ RSpec.configure do |config|
8
+ config.include EmailSpec::Helpers
9
+ config.include EmailSpec::Methods
10
+ end
11
+ """
12
+ }
13
+ let(:node) { Parser::CurrentRuby.parse(source) }
14
+ let(:instance) { double(:current_node => node, :current_source => source) }
15
+
16
+ describe '#process' do
17
+ it 'call block if match anything' do
18
+ run = false
19
+ condition = Rewriter::IfExistCondition.new instance, type: 'send', message: 'include', arguments: ['EmailSpec::Helpers'] do
20
+ run = true
21
+ end
22
+ condition.process
23
+ expect(run).to be_true
24
+ end
25
+
26
+ it 'not call block if not match anything' do
27
+ run = false
28
+ condition = Rewriter::IfExistCondition.new instance, type: 'send', message: 'include', arguments: ['FactoryGirl::SyntaxMethods'] do
29
+ run = true
30
+ end
31
+ condition.process
32
+ expect(run).to be_false
33
+ end
34
+ end
35
+ end
36
+
4
37
  describe Rewriter::UnlessExistCondition do
5
38
  let(:source) {
6
39
  """
@@ -2,8 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  module Synvert
4
4
  describe Rewriter::Instance do
5
- let(:rewriter) { Rewriter.new 'description' do; end }
6
- let(:instance) { Rewriter::Instance.new(rewriter, 'file pattern') }
5
+ let(:instance) { Rewriter::Instance.new('file pattern') }
7
6
 
8
7
  it 'parses within_node' do
9
8
  scope = double()
@@ -21,6 +20,14 @@ module Synvert
21
20
  instance.with_node(type: 'send', message: 'create', &block)
22
21
  end
23
22
 
23
+ it 'parses if_exist_node' do
24
+ condition = double()
25
+ block = Proc.new {}
26
+ expect(Rewriter::IfExistCondition).to receive(:new).with(instance, type: 'send', message: 'create', &block).and_return(condition)
27
+ expect(condition).to receive(:process)
28
+ instance.if_exist_node(type: 'send', message: 'create', &block)
29
+ end
30
+
24
31
  it 'parses unless_exist_node' do
25
32
  condition = double()
26
33
  block = Proc.new {}
@@ -62,16 +69,11 @@ module Synvert
62
69
  instance.remove
63
70
  end
64
71
 
65
- it 'parses assign / fetch' do
66
- instance.assign 'parameters', 'user', 'email'
67
- expect(instance.fetch 'parameters', 'user').to eq 'email'
68
- end
69
-
70
72
  describe '#process' do
71
73
  before { Configuration.instance.set :path, '.' }
72
74
 
73
75
  it 'FactoryGirl uses short syntax' do
74
- instance = Rewriter::Instance.new rewriter, 'spec/**/*_spec.rb' do
76
+ instance = Rewriter::Instance.new 'spec/**/*_spec.rb' do
75
77
  with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
76
78
  replace_with 'create {{arguments}}'
77
79
  end
@@ -97,7 +99,7 @@ end
97
99
  end
98
100
 
99
101
  it 'includes FactoryGirl::Syntax::Methods' do
100
- instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb' do
102
+ instance = Rewriter::Instance.new 'spec/spec_helper.rb' do
101
103
  with_node type: 'block', caller: {receiver: 'RSpec', message: 'configure'} do
102
104
  unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
103
105
  insert "{{arguments.first}}.include FactoryGirl::Syntax::Methods"
@@ -120,7 +122,7 @@ end
120
122
  end
121
123
 
122
124
  it 'does not include FactoryGirl::Syntax::Methods' do
123
- instance = Rewriter::Instance.new rewriter, 'spec/spec_helper.rb' do
125
+ instance = Rewriter::Instance.new 'spec/spec_helper.rb' do
124
126
  with_node type: 'block', caller: {receiver: 'RSpec', message: 'configure'} do
125
127
  unless_exist_node type: 'send', message: 'include', arguments: ['FactoryGirl::Syntax::Methods'] do
126
128
  insert "{{arguments.first}}.include FactoryGirl::Syntax::Methods"
@@ -144,7 +146,7 @@ end
144
146
  end
145
147
 
146
148
  it 'process nested send nodes' do
147
- instance = Rewriter::Instance.new rewriter, 'config/*.rb' do
149
+ instance = Rewriter::Instance.new 'config/*.rb' do
148
150
  with_node type: 'send', receiver: {type: 'send', receiver: {type: 'send', message: 'config'}, message: 'active_record'}, message: 'identity_map=' do
149
151
  remove
150
152
  end
@@ -2,39 +2,60 @@ require 'spec_helper'
2
2
 
3
3
  module Synvert
4
4
  describe Rewriter do
5
- it 'sets description' do
6
- rewriter = Rewriter.new 'this is description' do; end
7
- expect(rewriter.description).to eq 'this is description'
8
- end
9
-
10
- describe 'get / set' do
11
- it 'set and get parameters' do
12
- rewriter = Rewriter.new 'this is description' do; end
13
- rewriter.set 'parameter', 'post', ':title, :description'
14
- expect(rewriter.get 'parameter', 'post').to eq ':title, :description'
5
+ it 'parses gem_spec' do
6
+ expect(Rewriter::GemSpec).to receive(:new).with('synvert', '1.0.0')
7
+ rewriter = Rewriter.new 'name', 'description' do
8
+ gem_spec 'synvert', '1.0.0'
15
9
  end
10
+ rewriter.process
16
11
  end
17
12
 
18
- describe '#process' do
13
+ describe 'parses within_file' do
19
14
  it 'does nothing if gem_spec not match' do
20
15
  expect_any_instance_of(Rewriter::GemSpec).to receive(:match?).and_return(false)
21
16
  expect_any_instance_of(Rewriter::Instance).not_to receive(:process)
22
- rewriter = Rewriter.new 'description' do
17
+ rewriter = Rewriter.new 'name', 'description' do
23
18
  gem_spec 'synvert', '1.0.0'
24
19
  within_file 'config/routes.rb' do; end
25
20
  end
26
21
  rewriter.process
27
22
  end
28
23
 
29
- it 'delegates process to instances' do
24
+ it 'delegates process to instances if gem_spec not exist' do
25
+ expect_any_instance_of(Rewriter::Instance).to receive(:process)
26
+ rewriter = Rewriter.new 'name', 'description' do
27
+ within_file 'config/routes.rb' do; end
28
+ end
29
+ rewriter.process
30
+ end
31
+
32
+ it 'delegates process to instances if gem_spec matches' do
30
33
  expect_any_instance_of(Rewriter::GemSpec).to receive(:match?).and_return(true)
31
34
  expect_any_instance_of(Rewriter::Instance).to receive(:process)
32
- rewriter = Rewriter.new 'description' do
35
+ rewriter = Rewriter.new 'name', 'description' do
33
36
  gem_spec 'synvert', '1.0.0'
34
37
  within_file 'config/routes.rb' do; end
35
38
  end
36
39
  rewriter.process
37
40
  end
38
41
  end
42
+
43
+ describe 'parses add_snippet' do
44
+ it 'process the rewritter' do
45
+ rewriter1 = Synvert::Rewriter.new 'rewriter1', 'description1'
46
+ rewriter2 = Synvert::Rewriter.new 'rewriter2', 'description2' do
47
+ add_snippet :rewriter1
48
+ end
49
+ expect(rewriter1).to receive(:process)
50
+ rewriter2.process
51
+ end
52
+
53
+ it 'raises RewriterNotFound' do
54
+ rewriter = Synvert::Rewriter.new 'name', 'description' do
55
+ add_snippet :not_exist
56
+ end
57
+ expect { rewriter.process }.to raise_error(Synvert::RewriterNotFound)
58
+ end
59
+ end
39
60
  end
40
61
  end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Convert dynamic finders' do
4
+ before do
5
+ Synvert::Configuration.instance.set :path, '.'
6
+ rewriter_path = File.join(File.dirname(__FILE__), '../../../../lib/synvert/snippets/rails/convert_dynamic_finders.rb')
7
+ @rewriter = eval(File.read(rewriter_path))
8
+ allow_any_instance_of(Synvert::Rewriter::GemSpec).to receive(:match?).and_return(true)
9
+ end
10
+
11
+ describe 'with fakefs', fakefs: true do
12
+ let(:post_model_content) {'''
13
+ class Post < ActiveRecord::Base
14
+ def active_users_by_email(email)
15
+ User.find_all_by_email_and_active(email, true)
16
+ end
17
+
18
+ def first_active_user_by_email(email)
19
+ User.find_by_email_and_active(email, true)
20
+ end
21
+
22
+ def last_active_user_by_email(email)
23
+ User.find_last_by_email_and_active(email, true)
24
+ end
25
+
26
+ def scoped_active_user_by_email(email)
27
+ User.scoped_by_email_and_active(email, true)
28
+ end
29
+ end
30
+ '''}
31
+ let(:post_model_rewritten_content) {'''
32
+ class Post < ActiveRecord::Base
33
+ def active_users_by_email(email)
34
+ User.where(email: email, active: true)
35
+ end
36
+
37
+ def first_active_user_by_email(email)
38
+ User.where(email: email, active: true).first
39
+ end
40
+
41
+ def last_active_user_by_email(email)
42
+ User.where(email: email, active: true).last
43
+ end
44
+
45
+ def scoped_active_user_by_email(email)
46
+ User.where(email: email, active: true)
47
+ end
48
+ end
49
+ '''}
50
+ let(:users_controller_content) {'''
51
+ class UsersController < ApplicationController
52
+ def new
53
+ @user = User.find_or_initialize_by_login_and_email(params[:user][:login], params[:user][:email])
54
+ end
55
+
56
+ def create
57
+ @user = User.find_or_create_by_login_and_email(params[:user][:login], params[:user][:email])
58
+ end
59
+ end
60
+ '''}
61
+ let(:users_controller_rewritten_content) {'''
62
+ class UsersController < ApplicationController
63
+ def new
64
+ @user = User.find_or_initialize_by(login: params[:user][:login], email: params[:user][:email])
65
+ end
66
+
67
+ def create
68
+ @user = User.find_or_create_by(login: params[:user][:login], email: params[:user][:email])
69
+ end
70
+ end
71
+ '''}
72
+
73
+ it 'process' do
74
+ FileUtils.mkdir_p 'app/models'
75
+ FileUtils.mkdir_p 'app/controllers'
76
+ File.write 'app/models/post.rb', post_model_content
77
+ File.write 'app/controllers/users_controller.rb', users_controller_content
78
+ @rewriter.process
79
+ expect(File.read 'app/models/post.rb').to eq post_model_rewritten_content
80
+ expect(File.read 'app/controllers/users_controller.rb').to eq users_controller_rewritten_content
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'rails strong_parameters snippet' do
4
+ before do
5
+ Synvert::Configuration.instance.set :path, '.'
6
+ rewriter_path = File.join(File.dirname(__FILE__), '../../../../lib/synvert/snippets/rails/strong_parameters.rb')
7
+ @rewriter = eval(File.read(rewriter_path))
8
+ allow_any_instance_of(Synvert::Rewriter::GemSpec).to receive(:match?).and_return(true)
9
+ end
10
+
11
+ describe 'with fakefs', fakefs: true do
12
+ let(:application_content) {'''
13
+ module Synvert
14
+ class Application < Rails::Application
15
+ config.active_record.whitelist_attributes = true
16
+ end
17
+ end
18
+ '''}
19
+ let(:application_rewritten_content) {'''
20
+ module Synvert
21
+ class Application < Rails::Application
22
+ end
23
+ end
24
+ '''}
25
+ let(:post_model_content) {'''
26
+ class Post < ActiveRecord::Base
27
+ attr_accessible :title, :description
28
+ end
29
+ '''}
30
+ let(:post_model_rewritten_content) {'''
31
+ class Post < ActiveRecord::Base
32
+ end
33
+ '''}
34
+ let(:posts_controller_content) {'''
35
+ class PostsController < ApplicationController
36
+ def update
37
+ @post = Post.find(params[:id])
38
+ if @post.update_attributes params[:post]
39
+ redirect_to post_path(@post)
40
+ else
41
+ render :action => :edit
42
+ end
43
+ end
44
+ end
45
+ '''}
46
+ let(:posts_controller_rewritten_content) {'''
47
+ class PostsController < ApplicationController
48
+ def update
49
+ @post = Post.find(params[:id])
50
+ if @post.update_attributes post_params
51
+ redirect_to post_path(@post)
52
+ else
53
+ render :action => :edit
54
+ end
55
+ end
56
+
57
+ def post_params
58
+ params.require(:post).permit(:title, :description)
59
+ end
60
+ end
61
+ '''}
62
+
63
+ it 'process' do
64
+ FileUtils.mkdir_p 'config'
65
+ FileUtils.mkdir_p 'app/models'
66
+ FileUtils.mkdir_p 'app/controllers'
67
+ File.write 'config/application.rb', application_content
68
+ File.write 'app/models/post.rb', post_model_content
69
+ File.write 'app/controllers/posts_controller.rb', posts_controller_content
70
+ @rewriter.process
71
+ expect(File.read 'config/application.rb').to eq application_rewritten_content
72
+ expect(File.read 'app/models/post.rb').to eq post_model_rewritten_content
73
+ expect(File.read 'app/controllers/posts_controller.rb').to eq posts_controller_rewritten_content
74
+ end
75
+ end
76
+ end
@@ -6,6 +6,10 @@ describe 'Upgrade rails from 3.2 to 4.0' do
6
6
  rewriter_path = File.join(File.dirname(__FILE__), '../../../../lib/synvert/snippets/rails/upgrade_3_2_to_4_0.rb')
7
7
  @rewriter = eval(File.read(rewriter_path))
8
8
  allow_any_instance_of(Synvert::Rewriter::GemSpec).to receive(:match?).and_return(true)
9
+ strong_parameters_rewriter_path = File.join(File.dirname(__FILE__), '../../../../lib/synvert/snippets/rails/strong_parameters.rb')
10
+ eval(File.read(strong_parameters_rewriter_path))
11
+ convert_dynamic_finders_rewriter_path = File.join(File.dirname(__FILE__), '../../../../lib/synvert/snippets/rails/convert_dynamic_finders.rb')
12
+ eval(File.read(convert_dynamic_finders_rewriter_path))
9
13
  expect(SecureRandom).to receive(:hex).with(64).and_return("bf4f3f46924ecd9adcb6515681c78144545bba454420973a274d7021ff946b8ef043a95ca1a15a9d1b75f9fbdf85d1a3afaf22f4e3c2f3f78e24a0a188b581df")
10
14
  end
11
15
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synvert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-16 00:00:00.000000000 Z
11
+ date: 2014-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -120,7 +120,10 @@ files:
120
120
  - lib/synvert/rewriter/gem_spec.rb
121
121
  - lib/synvert/rewriter/instance.rb
122
122
  - lib/synvert/rewriter/scope.rb
123
+ - lib/synvert/rewriter_not_found.rb
123
124
  - lib/synvert/snippets/factory_girl/syntax_methods.rb
125
+ - lib/synvert/snippets/rails/convert_dynamic_finders.rb
126
+ - lib/synvert/snippets/rails/strong_parameters.rb
124
127
  - lib/synvert/snippets/rails/upgrade_3_2_to_4_0.rb
125
128
  - lib/synvert/version.rb
126
129
  - spec/spec_helper.rb
@@ -133,6 +136,8 @@ files:
133
136
  - spec/synvert/rewriter/scope_spec.rb
134
137
  - spec/synvert/rewriter_spec.rb
135
138
  - spec/synvert/snippets/factory_girl/syntax_methods_spec.rb
139
+ - spec/synvert/snippets/rails/convert_dynamic_finders_spec.rb
140
+ - spec/synvert/snippets/rails/strong_parameters_spec.rb
136
141
  - spec/synvert/snippets/rails/upgrade_3_2_to_4_0_spec.rb
137
142
  - synvert.gemspec
138
143
  homepage: ''
@@ -170,4 +175,6 @@ test_files:
170
175
  - spec/synvert/rewriter/scope_spec.rb
171
176
  - spec/synvert/rewriter_spec.rb
172
177
  - spec/synvert/snippets/factory_girl/syntax_methods_spec.rb
178
+ - spec/synvert/snippets/rails/convert_dynamic_finders_spec.rb
179
+ - spec/synvert/snippets/rails/strong_parameters_spec.rb
173
180
  - spec/synvert/snippets/rails/upgrade_3_2_to_4_0_spec.rb