Class: Synvert::Core::Rewriter
- Inherits:
-
Object
- Object
- Synvert::Core::Rewriter
- 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
-
#group ⇒ String
readonly
The group of rewriter.
-
#helper ⇒ Array
readonly
Helper methods.
-
#helpers ⇒ Object
readonly
Returns the value of attribute helpers.
-
#name ⇒ String
readonly
The unique name of rewriter.
-
#sub_snippets ⇒ Array<Synvert::Core::Rewriter>
readonly
All rewriters this rewiter calls.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Class Method Summary collapse
-
.availables ⇒ Hash<String, Hash<String, Rewriter>>
Get all available rewriters.
-
.call(group, name) ⇒ Synvert::Core::Rewriter
Get a registered rewriter by group and name, then process that rewriter.
-
.clear ⇒ Object
Clear all registered rewriters.
-
.exist?(group, name) ⇒ Boolean
Check if one rewriter exist.
-
.fetch(group, name) ⇒ Synvert::Core::Rewriter
Fetch a rewriter by group and name.
-
.register(group, name, rewriter) ⇒ Object
Register a rewriter with its group and name.
Instance Method Summary collapse
-
#add_file(filename, content) ⇒ Object
Parses add_file dsl, it adds a new file.
-
#add_snippet(group, name) ⇒ Object
Parse add_snippet dsl, it calls anther rewriter.
-
#add_warning(warning) ⇒ Object
Add a warning.
-
#description(description = nil) ⇒ Object
Parse description dsl, it sets description of the rewrite.
-
#helper_method(name, &block) ⇒ Object
Parse helper_method dsl, it defines helper method for [Synvert::Core::Rewriter::Instance].
-
#if_gem(name, comparator) ⇒ Object
Parse if_gem dsl, it compares version of the specified gem.
-
#initialize(group, name, &block) ⇒ Synvert::Core::Rewriter
constructor
Initialize a rewriter.
-
#process ⇒ Object
Process the rewriter.
-
#process_with_sandbox ⇒ Object
Process rewriter with sandbox mode.
-
#remove_file(filename) ⇒ Object
Parses remove_file dsl, it removes a file.
-
#todo(todo = nil) ⇒ String
Parse todo dsl, it sets todo of the rewriter.
-
#within_files(file_pattern, &block) ⇒ Object
(also: #within_file)
Parse within_files dsl, it finds specified files.
Constructor Details
#initialize(group, name, &block) ⇒ Synvert::Core::Rewriter
Initialize a rewriter. When a rewriter is initialized, it is also registered.
133 134 135 136 137 138 139 140 141 |
# File 'lib/synvert/core/rewriter.rb', line 133 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
#group ⇒ String (readonly)
Returns the group of rewriter.
124 125 126 |
# File 'lib/synvert/core/rewriter.rb', line 124 def group @group end |
#helper ⇒ Array (readonly)
Returns helper methods.
124 |
# File 'lib/synvert/core/rewriter.rb', line 124 attr_reader :group, :name, :sub_snippets, :helpers, :warnings |
#helpers ⇒ Object (readonly)
Returns the value of attribute helpers.
124 125 126 |
# File 'lib/synvert/core/rewriter.rb', line 124 def helpers @helpers end |
#name ⇒ String (readonly)
Returns the unique name of rewriter.
124 |
# File 'lib/synvert/core/rewriter.rb', line 124 attr_reader :group, :name, :sub_snippets, :helpers, :warnings |
#sub_snippets ⇒ Array<Synvert::Core::Rewriter> (readonly)
Returns all rewriters this rewiter calls.
124 |
# File 'lib/synvert/core/rewriter.rb', line 124 attr_reader :group, :name, :sub_snippets, :helpers, :warnings |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
124 |
# File 'lib/synvert/core/rewriter.rb', line 124 attr_reader :group, :name, :sub_snippets, :helpers, :warnings |
Class Method Details
.availables ⇒ Hash<String, Hash<String, Rewriter>>
Get all available rewriters
104 105 106 |
# File 'lib/synvert/core/rewriter.rb', line 104 def availables @rewriters end |
.call(group, name) ⇒ Synvert::Core::Rewriter
Get a registered rewriter by group and name, then process that rewriter.
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/synvert/core/rewriter.rb', line 76 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 |
.clear ⇒ Object
Clear all registered rewriters.
109 110 111 |
# File 'lib/synvert/core/rewriter.rb', line 109 def clear @rewriters.clear if @rewriters end |
.exist?(group, name) ⇒ Boolean
Check if one rewriter exist.
92 93 94 95 96 97 98 99 |
# File 'lib/synvert/core/rewriter.rb', line 92 def exist?(group, name) group, name = group.to_s, name.to_s if @rewriters && @rewriters[group] && @rewriters[group][name] true else false end end |
.fetch(group, name) ⇒ Synvert::Core::Rewriter
Fetch a rewriter by group and name.
61 62 63 64 65 66 67 68 |
# File 'lib/synvert/core/rewriter.rb', line 61 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.
48 49 50 51 52 53 |
# File 'lib/synvert/core/rewriter.rb', line 48 def register(group, name, rewriter) group, name = group.to_s, name.to_s @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.
210 211 212 213 214 215 216 |
# File 'lib/synvert/core/rewriter.rb', line 210 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.
232 233 234 |
# File 'lib/synvert/core/rewriter.rb', line 232 def add_snippet(group, name) @sub_snippets << self.class.call(group.to_s, name.to_s) end |
#add_warning(warning) ⇒ Object
Add a warning.
160 161 162 |
# File 'lib/synvert/core/rewriter.rb', line 160 def add_warning(warning) @warnings << warning end |
#description(description = nil) ⇒ Object
Parse description dsl, it sets description of the rewrite. Or get description.
173 174 175 176 177 178 179 |
# File 'lib/synvert/core/rewriter.rb', line 173 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].
240 241 242 |
# File 'lib/synvert/core/rewriter.rb', line 240 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.
186 187 188 |
# File 'lib/synvert/core/rewriter.rb', line 186 def if_gem(name, comparator) @gem_spec = Rewriter::GemSpec.new(name, comparator) end |
#process ⇒ Object
Process the rewriter. It will call the block.
145 146 147 |
# File 'lib/synvert/core/rewriter.rb', line 145 def process self.instance_eval &@block end |
#process_with_sandbox ⇒ Object
Process rewriter with sandbox mode. It will call the block but doesn’t change any file.
151 152 153 154 155 |
# File 'lib/synvert/core/rewriter.rb', line 151 def process_with_sandbox @sandbox = true self.process @sandbox = false end |
#remove_file(filename) ⇒ Object
Parses remove_file dsl, it removes a file.
221 222 223 224 225 226 |
# File 'lib/synvert/core/rewriter.rb', line 221 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.
249 250 251 252 253 254 255 |
# File 'lib/synvert/core/rewriter.rb', line 249 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.
195 196 197 198 199 200 201 |
# File 'lib/synvert/core/rewriter.rb', line 195 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 |