Class: Synvert::Core::Rewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/synvert/core/rewriter.rb

Overview

Rewriter is the top level namespace in a snippet.

One Rewriter can contain one or many [Synvert::Core::Rewriter::Instance], which define the behavior what files and what codes to detect and rewrite to what code.

Synvert::Rewriter.new 'factory_girl_short_syntax', 'use FactoryGirl short syntax' do
  if_gem 'factory_girl', {gte: '2.0.0'}

  within_files 'spec/**/*.rb' do
    with_node type: 'send', receiver: 'FactoryGirl', message: 'create' do
      replace_with "create({{arguments}})"
    end
  end
end

Defined Under Namespace

Modules: Helper Classes: Action, AppendAction, Condition, GemSpec, GotoScope, IfExistCondition, IfOnlyExistCondition, InsertAction, InsertAfterAction, Instance, RemoveAction, ReplaceErbStmtWithExprAction, ReplaceWithAction, RubyVersion, Scope, UnlessExistCondition, Warning, WithinScope

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, name, &block) ⇒ Synvert::Core::Rewriter

Initialize a rewriter. When a rewriter is initialized, it is also registered.



157
158
159
160
161
162
163
164
165
# File 'lib/synvert/core/rewriter.rb', line 157

def initialize(group, name, &block)
  @group = group
  @name = name
  @block = block
  @helpers = []
  @sub_snippets = []
  @warnings = []
  self.class.register(@group, @name, self)
end

Instance Attribute Details

#groupString (readonly)



148
149
150
# File 'lib/synvert/core/rewriter.rb', line 148

def group
  @group
end

#helperArray (readonly)



148
# File 'lib/synvert/core/rewriter.rb', line 148

attr_reader :group, :name, :sub_snippets, :helpers, :warnings

#helpersObject (readonly)

Returns the value of attribute helpers.



148
149
150
# File 'lib/synvert/core/rewriter.rb', line 148

def helpers
  @helpers
end

#nameString (readonly)



148
# File 'lib/synvert/core/rewriter.rb', line 148

attr_reader :group, :name, :sub_snippets, :helpers, :warnings

#sub_snippetsArray<Synvert::Core::Rewriter> (readonly)



148
# File 'lib/synvert/core/rewriter.rb', line 148

attr_reader :group, :name, :sub_snippets, :helpers, :warnings

#warningsObject (readonly)

Returns the value of attribute warnings.



148
# File 'lib/synvert/core/rewriter.rb', line 148

attr_reader :group, :name, :sub_snippets, :helpers, :warnings

Class Method Details

.availablesHash<String, Hash<String, Rewriter>>

Get all available rewriters



122
123
124
# File 'lib/synvert/core/rewriter.rb', line 122

def availables
  rewriters
end

.call(group, name, sandbox = false) ⇒ Synvert::Core::Rewriter

Get a registered rewriter by group and name, then process that rewriter.

Raises:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/synvert/core/rewriter.rb', line 88

def call(group, name, sandbox = false)
  group = group.to_s
  name = name.to_s
  if exist? group, name
    rewriter = rewriters[group][name]
    if sandbox
      rewriter.process_with_sandbox
    else
      rewriter.process
    end
    rewriter
  else
    raise RewriterNotFound, "Rewriter #{group}/#{name} not found"
  end
end

.clearObject

Clear all registered rewriters.



127
128
129
# File 'lib/synvert/core/rewriter.rb', line 127

def clear
  rewriters.clear
end

.execute(&block) ⇒ Object

Execute the temporary rewriter without group and name.



49
50
51
# File 'lib/synvert/core/rewriter.rb', line 49

def execute(&block)
  Rewriter.new('', '', &block).process
end

.exist?(group, name) ⇒ Boolean

Check if one rewriter exist.



109
110
111
112
113
114
115
116
117
# File 'lib/synvert/core/rewriter.rb', line 109

def exist?(group, name)
  group = group.to_s
  name = name.to_s
  if rewriters[group] && rewriters[group][name]
    true
  else
    false
  end
end

.fetch(group, name) ⇒ Synvert::Core::Rewriter

Fetch a rewriter by group and name.

Raises:



71
72
73
74
75
76
77
78
79
# File 'lib/synvert/core/rewriter.rb', line 71

def fetch(group, name)
  group = group.to_s
  name = name.to_s
  if exist? group, name
    rewriters[group][name]
  else
    raise RewriterNotFound, "Rewriter #{group} #{name} not found"
  end
end

.register(group, name, rewriter) ⇒ Object

Register a rewriter with its group and name.



58
59
60
61
62
63
# File 'lib/synvert/core/rewriter.rb', line 58

def register(group, name, rewriter)
  group = group.to_s
  name = name.to_s
  rewriters[group] ||= {}
  rewriters[group][name] = rewriter
end

Instance Method Details

#add_file(filename, content) ⇒ Object

Parses add_file dsl, it adds a new file.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/synvert/core/rewriter.rb', line 245

def add_file(filename, content)
  return if @sandbox

  filepath = File.join(Configuration.path, filename)
  if File.exist?(filepath)
    puts "File #{filepath} already exists."
    return
  end

  FileUtils.mkdir_p File.dirname(filepath)
  File.open filepath, 'w' do |file|
    file.write content
  end
end

#add_snippet(group, name) ⇒ Object

Parse add_snippet dsl, it calls anther rewriter.



274
275
276
# File 'lib/synvert/core/rewriter.rb', line 274

def add_snippet(group, name)
  @sub_snippets << self.class.call(group.to_s, name.to_s, @sandbox)
end

#add_warning(warning) ⇒ Object

Add a warning.



187
188
189
# File 'lib/synvert/core/rewriter.rb', line 187

def add_warning(warning)
  @warnings << warning
end

#description(description = nil) ⇒ Object

Parse description dsl, it sets description of the rewrite. Or get description.



200
201
202
203
204
205
206
# File 'lib/synvert/core/rewriter.rb', line 200

def description(description = nil)
  if description
    @description = description
  else
    @description
  end
end

#helper_method(name, &block) ⇒ Object

Parse helper_method dsl, it defines helper method for [Synvert::Core::Rewriter::Instance].



282
283
284
# File 'lib/synvert/core/rewriter.rb', line 282

def helper_method(name, &block)
  @helpers << { name: name, block: block }
end

#if_gem(name, comparator) ⇒ Object

Parse if_gem dsl, it compares version of the specified gem.



220
221
222
# File 'lib/synvert/core/rewriter.rb', line 220

def if_gem(name, comparator)
  @gem_spec = Rewriter::GemSpec.new(name, comparator)
end

#if_ruby(version) ⇒ Object

Parse if_ruby dal, it checks if ruby version if greater than or equal to the specified ruby version.



211
212
213
# File 'lib/synvert/core/rewriter.rb', line 211

def if_ruby(version)
  @ruby_version = Rewriter::RubyVersion.new(version)
end

#processObject

Process the rewriter. It will call the block.



169
170
171
# File 'lib/synvert/core/rewriter.rb', line 169

def process
  instance_eval(&@block)
end

#process_with_sandboxObject

Process rewriter with sandbox mode. It will call the block but doesn’t change any file.



175
176
177
178
179
180
181
182
# File 'lib/synvert/core/rewriter.rb', line 175

def process_with_sandbox
  @sandbox = true
  begin
    process
  ensure
    @sandbox = false
  end
end

#remove_file(filename) ⇒ Object

Parses remove_file dsl, it removes a file.



263
264
265
266
267
268
# File 'lib/synvert/core/rewriter.rb', line 263

def remove_file(filename)
  return if @sandbox

  file_path = File.join(Configuration.path, filename)
  File.delete(file_path) if File.exist?(file_path)
end

#todo(todo = nil) ⇒ String

Parse todo dsl, it sets todo of the rewriter. Or get todo.



291
292
293
294
295
296
297
# File 'lib/synvert/core/rewriter.rb', line 291

def todo(todo = nil)
  if todo
    @todo = todo
  else
    @todo
  end
end

#within_files(file_pattern, options = {}, &block) ⇒ Object Also known as: within_file

Parse within_files dsl, it finds specified files. It creates a [Synvert::Core::Rewriter::Instance] to rewrite code.



230
231
232
233
234
235
236
# File 'lib/synvert/core/rewriter.rb', line 230

def within_files(file_pattern, options = {}, &block)
  return if @sandbox

  if (!@ruby_version || @ruby_version.match?) && (!@gem_spec || @gem_spec.match?)
    Rewriter::Instance.new(self, file_pattern, options, &block).process
  end
end