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, IfExistCondition, IfOnlyExistCondition, InsertAction, InsertAfterAction, Instance, RemoveAction, ReplaceErbStmtWithExprAction, ReplaceWithAction, Scope, UnlessExistCondition, Warning

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

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

Parameters:

  • name (String)

    name of the rewriter.

  • block (Block)

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



103
104
105
106
107
108
109
110
# File 'lib/synvert/core/rewriter.rb', line 103

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

Instance Attribute Details

#helperArray (readonly)

Returns helper methods.

Returns:

  • (Array)

    helper methods.



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

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

#helpersObject (readonly)

Returns the value of attribute helpers.



95
96
97
# File 'lib/synvert/core/rewriter.rb', line 95

def helpers
  @helpers
end

#nameString (readonly)

Returns the unique name of rewriter.

Returns:

  • (String)

    the unique name of rewriter



95
96
97
# File 'lib/synvert/core/rewriter.rb', line 95

def name
  @name
end

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

Returns all rewriters this rewiter calls.

Returns:



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

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

#warningsObject (readonly)

Returns the value of attribute warnings.



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

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

Class Method Details

.availablesArray<Synvert::Core::Rewriter>

Get all available rewriters

Returns:



77
78
79
# File 'lib/synvert/core/rewriter.rb', line 77

def availables
  @rewriters ? @rewriters.values : []
end

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

Get a registered rewriter by name and process that rewriter.

Parameters:

  • name (String)

    the rewriter name.

Returns:

Raises:



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

def call(name)
  if (rewriter = @rewriters[name])
    rewriter.process
    rewriter
  else
    raise RewriterNotFound.new "Rewriter #{name} not found"
  end
end

.clearObject

Clear all registered rewriters.



82
83
84
# File 'lib/synvert/core/rewriter.rb', line 82

def clear
  @rewriters.clear
end

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

Fetch a rewriter by name.

Parameters:

  • name (String)

    rewrtier name.

Returns:



56
57
58
# File 'lib/synvert/core/rewriter.rb', line 56

def fetch(name)
  @rewriters[name]
end

.register(name, rewriter) ⇒ Object

Register a rewriter with its name.

Parameters:



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

def register(name, rewriter)
  @rewriters ||= {}
  @rewriters[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.



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

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(name) ⇒ Object

Parse add_snippet dsl, it calls anther rewriter.

Parameters:

  • name (String)

    name of another rewriter.



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

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

#add_warning(warning) ⇒ Object

Add a warning.

Parameters:



129
130
131
# File 'lib/synvert/core/rewriter.rb', line 129

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.



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

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.



208
209
210
# File 'lib/synvert/core/rewriter.rb', line 208

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.



155
156
157
# File 'lib/synvert/core/rewriter.rb', line 155

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

#processObject

Process the rewriter. It will call the block.



114
115
116
# File 'lib/synvert/core/rewriter.rb', line 114

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.



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

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.



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

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.



217
218
219
220
221
222
223
# File 'lib/synvert/core/rewriter.rb', line 217

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.



164
165
166
167
168
169
170
# File 'lib/synvert/core/rewriter.rb', line 164

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

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