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.

Parameters:

  • group (String)

    group of the rewriter.

  • name (String)

    name of the rewriter.

  • block (Block)

    a block defines the behaviors of the rewriter, block code won't be called when initialization.


141
142
143
144
145
146
147
148
149
# File 'lib/synvert/core/rewriter.rb', line 141

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)

Returns the group of rewriter.

Returns:

  • (String)

    the group of rewriter


132
133
134
# File 'lib/synvert/core/rewriter.rb', line 132

def group
  @group
end

#helperArray (readonly)

Returns helper methods.

Returns:

  • (Array)

    helper methods.


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

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

#helpersObject (readonly)

Returns the value of attribute helpers


132
133
134
# File 'lib/synvert/core/rewriter.rb', line 132

def helpers
  @helpers
end

#nameString (readonly)

Returns the unique name of rewriter.

Returns:

  • (String)

    the unique name of rewriter


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

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

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

Returns all rewriters this rewiter calls.

Returns:


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

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

#warningsObject (readonly)

Returns the value of attribute warnings


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

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

Class Method Details

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

Get all available rewriters

Returns:

  • (Hash<String, Hash<String, Rewriter>>)

106
107
108
# File 'lib/synvert/core/rewriter.rb', line 106

def availables
  rewriters
end

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

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

Parameters:

  • group (String)

    the rewriter group.

  • name (String)

    the rewriter name.

Returns:

Raises:


78
79
80
81
82
83
84
85
86
87
# File 'lib/synvert/core/rewriter.rb', line 78

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

.clearObject

Clear all registered rewriters.


111
112
113
# File 'lib/synvert/core/rewriter.rb', line 111

def clear
  rewriters.clear
end

.exist?(group, name) ⇒ Boolean

Check if one rewriter exist.

Parameters:

  • group (String)

    the rewriter group.

  • name (String)

    the rewriter name.

Returns:

  • (Boolean)

    true if the rewriter exist.


94
95
96
97
98
99
100
101
# File 'lib/synvert/core/rewriter.rb', line 94

def exist?(group, name)
  group, name = group.to_s, 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.

Parameters:

  • group (String)

    rewrtier group.

  • name (String)

    rewrtier name.

Returns:

Raises:


63
64
65
66
67
68
69
70
# File 'lib/synvert/core/rewriter.rb', line 63

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

.register(group, name, rewriter) ⇒ Object

Register a rewriter with its group and name.

Parameters:

  • group (String)

    the rewriter group.

  • name (String)

    the unique rewriter name.

  • rewriter (Synvert::Core::Rewriter)

    the rewriter to register.


51
52
53
54
55
# File 'lib/synvert/core/rewriter.rb', line 51

def register(group, name, rewriter)
  group, name = group.to_s, 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.

Parameters:

  • filename (String)

    file name of newly created file.

  • content (String)

    file body of newly created file.


226
227
228
229
230
231
232
# File 'lib/synvert/core/rewriter.rb', line 226

def add_file(filename, content)
  return if @sandbox

  File.open File.join(Configuration.instance.get(:path), filename), 'w' do |file|
    file.write content
  end
end

#add_snippet(group, name) ⇒ Object

Parse add_snippet dsl, it calls anther rewriter.

Parameters:

  • group (String)

    group of another rewriter.

  • name (String)

    name of another rewriter.


248
249
250
# File 'lib/synvert/core/rewriter.rb', line 248

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

#add_warning(warning) ⇒ Object

Add a warning.

Parameters:


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

def add_warning(warning)
  @warnings << warning
end

#description(description = nil) ⇒ Object

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

Parameters:

  • description (String) (defaults to: nil)

    rewriter description.

Returns:

  • rewriter description.


181
182
183
184
185
186
187
# File 'lib/synvert/core/rewriter.rb', line 181

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].

Parameters:

  • name (String)

    helper method name.

  • block (Block)

    helper method block.


256
257
258
# File 'lib/synvert/core/rewriter.rb', line 256

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.

Parameters:

  • name (String)

    gem name.

  • comparator (Hash)

    equal, less than or greater than specified version, e.g. '2.0.0', key can be eq, lt, gt, lte, gte or ne.


201
202
203
# File 'lib/synvert/core/rewriter.rb', line 201

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.

Parameters:

  • version, (String)

    specified ruby version.


192
193
194
# File 'lib/synvert/core/rewriter.rb', line 192

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

#processObject

Process the rewriter. It will call the block.


153
154
155
# File 'lib/synvert/core/rewriter.rb', line 153

def process
  self.instance_eval &@block
end

#process_with_sandboxObject

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


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

def process_with_sandbox
  @sandbox = true
  self.process
  @sandbox = false
end

#remove_file(filename) ⇒ Object

Parses remove_file dsl, it removes a file.

Parameters:

  • filename (String)

    file name.


237
238
239
240
241
242
# File 'lib/synvert/core/rewriter.rb', line 237

def remove_file(filename)
  return if @sandbox

  file_path = File.join(Configuration.instance.get(: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.

Parameters:

  • todo_list (String)

    rewriter todo.

Returns:

  • (String)

    rewriter todo.


265
266
267
268
269
270
271
# File 'lib/synvert/core/rewriter.rb', line 265

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

#within_files(file_pattern, &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.

Parameters:

  • file_pattern (String)

    pattern to find files, e.g. spec/*/_spec.rb

  • block (Block)

    the block to rewrite code in the matching files.


210
211
212
213
214
215
216
217
# File 'lib/synvert/core/rewriter.rb', line 210

def within_files(file_pattern, &block)
  return if @sandbox

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