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(group, name, &block) ⇒ Synvert::Core::Rewriter

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



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

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)



101
102
103
# File 'lib/synvert/core/rewriter.rb', line 101

def group
  @group
end

#helperArray (readonly)



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

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

#helpersObject (readonly)

Returns the value of attribute helpers.



101
102
103
# File 'lib/synvert/core/rewriter.rb', line 101

def helpers
  @helpers
end

#nameString (readonly)



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

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

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



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

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

#warningsObject (readonly)

Returns the value of attribute warnings.



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

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

Class Method Details

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

Get all available rewriters



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

def availables
  @rewriters
end

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

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

Raises:



69
70
71
72
73
74
75
76
# File 'lib/synvert/core/rewriter.rb', line 69

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

.clearObject

Clear all registered rewriters.



86
87
88
# File 'lib/synvert/core/rewriter.rb', line 86

def clear
  @rewriters.clear
end

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

Fetch a rewriter by group and name.



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

def fetch(group, name)
  @rewriters[group][name]
end

.register(group, name, rewriter) ⇒ Object

Register a rewriter with its group and name.



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

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

Instance Method Details

#add_file(filename, content) ⇒ Object

Parses add_file dsl, it adds a new file.



187
188
189
190
191
192
193
# File 'lib/synvert/core/rewriter.rb', line 187

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.



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

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

#add_warning(warning) ⇒ Object

Add a warning.



137
138
139
# File 'lib/synvert/core/rewriter.rb', line 137

def add_warning(warning)
  @warnings << warning
end

#description(description = nil) ⇒ Object

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



150
151
152
153
154
155
156
# File 'lib/synvert/core/rewriter.rb', line 150

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



217
218
219
# File 'lib/synvert/core/rewriter.rb', line 217

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.



163
164
165
# File 'lib/synvert/core/rewriter.rb', line 163

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

#processObject

Process the rewriter. It will call the block.



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

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.



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

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

#remove_file(filename) ⇒ Object

Parses remove_file dsl, it removes a file.



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

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.



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

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.



172
173
174
175
176
177
178
# File 'lib/synvert/core/rewriter.rb', line 172

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