Class: Fast::Experiment
- Inherits:
-
Object
- Object
- Fast::Experiment
- Defined in:
- lib/fast/experiment.rb
Overview
Fast experiment allow the user to combine single replacements and make multiple changes at the same time. Defining a policy is possible to check if the experiment was successfull and keep changing the file using a specific search.
The experiment have a combination algorithm that recursively check what combinations work with what combinations. It can delay years and because of that it tries a first replacement targeting all the cases in a single file.
You can define experiments and build experimental files to improve some code in an automated way. Let’s create a hook to check if a ‘before` or `after` block is useless in a specific spec:
Instance Attribute Summary collapse
-
#expression ⇒ Object
readonly
Returns the value of attribute expression.
-
#files ⇒ Array<String>
With files from #lookup expression.
-
#files_or_folders ⇒ Object
readonly
Returns the value of attribute files_or_folders.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ok_if ⇒ Object
readonly
Returns the value of attribute ok_if.
-
#replacement ⇒ Object
readonly
Returns the value of attribute replacement.
Instance Method Summary collapse
-
#edit(&block) ⇒ Object
instance context of a [Fast::Rewriter].
-
#initialize(name, &block) ⇒ Experiment
constructor
A new instance of Experiment.
- #lookup(files_or_folders) ⇒ Object
-
#policy(&block) ⇒ Object
It calls the block after the replacement and use the result to drive the Fast::ExperimentFile#ok_experiments and Fast::ExperimentFile#fail_experiments.
- #run ⇒ void
-
#run_with(file) ⇒ Object
It combines current experiment with Fast::ExperimentFile#run.
- #search(expression) ⇒ Object
Constructor Details
#initialize(name, &block) ⇒ Experiment
Returns a new instance of Experiment.
95 96 97 98 99 |
# File 'lib/fast/experiment.rb', line 95 def initialize(name, &block) @name = name puts "\nStarting experiment: #{name}" instance_exec(&block) end |
Instance Attribute Details
#expression ⇒ Object (readonly)
Returns the value of attribute expression.
93 94 95 |
# File 'lib/fast/experiment.rb', line 93 def expression @expression end |
#files ⇒ Array<String>
Returns with files from #lookup expression.
131 132 133 |
# File 'lib/fast/experiment.rb', line 131 def files @files ||= Fast.ruby_files_from(@files_or_folders) end |
#files_or_folders ⇒ Object (readonly)
Returns the value of attribute files_or_folders.
93 94 95 |
# File 'lib/fast/experiment.rb', line 93 def files_or_folders @files_or_folders end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
93 94 95 |
# File 'lib/fast/experiment.rb', line 93 def name @name end |
#ok_if ⇒ Object (readonly)
Returns the value of attribute ok_if.
93 94 95 |
# File 'lib/fast/experiment.rb', line 93 def ok_if @ok_if end |
#replacement ⇒ Object (readonly)
Returns the value of attribute replacement.
93 94 95 |
# File 'lib/fast/experiment.rb', line 93 def replacement @replacement end |
Instance Method Details
#edit(&block) ⇒ Object
instance context of a [Fast::Rewriter]
114 115 116 |
# File 'lib/fast/experiment.rb', line 114 def edit(&block) @replacement = block end |
#lookup(files_or_folders) ⇒ Object
119 120 121 |
# File 'lib/fast/experiment.rb', line 119 def lookup(files_or_folders) @files_or_folders = files_or_folders end |
#policy(&block) ⇒ Object
It calls the block after the replacement and use the result to drive the Fast::ExperimentFile#ok_experiments and Fast::ExperimentFile#fail_experiments.
126 127 128 |
# File 'lib/fast/experiment.rb', line 126 def policy(&block) @ok_if = block end |
#run ⇒ void
137 138 139 |
# File 'lib/fast/experiment.rb', line 137 def run files.map(&method(:run_with)) end |
#run_with(file) ⇒ Object
It combines current experiment with Fast::ExperimentFile#run
103 104 105 |
# File 'lib/fast/experiment.rb', line 103 def run_with(file) ExperimentFile.new(file, self).run end |
#search(expression) ⇒ Object
108 109 110 |
# File 'lib/fast/experiment.rb', line 108 def search(expression) @expression = expression end |